Any Lessons and Tips Through Regular Posts


Showing posts with label Programming Tips. Show all posts
Showing posts with label Programming Tips. Show all posts

Sunday, September 11, 2016

PHP Functions - array_merge() and array_combine()

PHP has lots of built-in functions. So that you may not remember all the the functions. Among those functions, array_merge and array_combine are 2 very useful php functions that are very useful for core php programmer.

array_merge() function:

array_merge() is a built-in php function that is used to merge two or more arrays in one place. Unlike array_combine, it does not need equal number of elements in the each array. Here is a simple example of array_merge();

$data1 = array("id"=>"21","name"=>"RKA","contact"=>"9843XXXXXX");
$data2 = array("address"=>"Kathmandu, Nepal");
$arrayMerge = array_merge($data1,$data2);


Array ( [id] => 21 [name] => RKA [contact] => 9843XXXXXX [address] => Kathmandu, Nepal )

array_combine() function: 

array_combine() is also a built-in php function which is used for combine only 2 arrays. And both the array of parameters must have equal number of elements. In fact, array_combine function combines 2 array together and generates index array.

$data3 = array("1","3","5","7");
$data4 = array("a","c","e","g");
$arrayCombine1 = array_combine($data3,$data4);

Array ( [1] => a [3] => c [5] => e [7] => g )

Saturday, August 13, 2016

Username availability checking in database using php with jquery ajax method

Username checker in jquery ajax
Jquery ajax database username checker 
Jquery ajax is the best way to check whether the username is available or not. Only using core ajax code is too long and difficult rather than ajax jquery. So use, jquery ajax method for ajax operation. Actually, ajax helps to get or retrieve and post the data from and into the database without reloading the pages.

Lets follow these core coding of jquery ajax which helps you to check the availability of username or any other data.

First of all you need different 3 codes:

  • Database table
  • Simple HTML Form with Jquery Ajax method
  • PHP coding "check_username.php"

Database Table

Create a table name users in database name test using like following codes:
id int(11) not null auto_increment
username varchar(255) UNIQUE KEY,
password varchar(255),

Simple HTML Form

<!doctype html>
<script src="<ADD JQUERY LIBRARY URL HERE>"></script>
<form method="post" name="form" action="check_username.php">
username<input type="text" name="username" id="username"/><br/>
<p id="res"></p>password<input type="password" name="password" id="password" />
<input type="submit" value="submit" name="submit" name="submit"/>


          var uname = $(this).val();
                    dataType:'text' ,
                     data:{username:uname}, //key value pairing
                  $('#res').html('<div style="color:red">Needs more than 5 characters</div>');

check_username.php PHP file

//create database connection first
$dbConn = new mysqli("localhost","root","password","test");
//check whether the connection is ready or not
      die("Not connected with database, try again");
$username = $_POST['username'];
$sqlQuery = "SELECT USERNAME FROM users WHERE username = '$username' ";
$res = $dbConn->query($sqlQuery)
       echo "Username Already Taken";
       echo "Username Available. Register Now";

I hope, the above code will help you to understand basic about .ajax() method of jquery. This is very much easy way to check username availability in database using jquery ajax method and php. 

If you have any difficulties to understand the above code, feel free to ask me. I will do my best to help you. Keep coding, Thank you.

Tuesday, February 23, 2016

Some unusual demerits of using of frameworks in programming

Using of framework making programmers lazy
Programming Frameworks
I never deny the goodness of use of MVC architecture in programming field. However, I am always against the use of frameworks to bring MVC architecture in your apps. Of course, using of frameworks provide number of advantages while doing coding. It saves the coding time, which also makes easy to build apps. But you have to know some unusual demerits of using of frameworks which completely destroys and makes your apps clumsy. Nowadays, many programmer or html/css designers, they use frameworks for the simplicity in working. But they even do not know a simple core coding of any programming language.

You may think using of framework is better than doing core coding, but I don't think it is good to use framework. Here are some malfunctions of using of frameworks:-

1) Too much unnecessary codes
If you have used either of frameworks like laravel, codeigniter, cakephp (PHP) or spring (Java) or bootstap (HTML/CSS), you can understand how much unnecessary codes, you have to apply for a simple task or apps.

2) You may forget the core coding techniques
When you use frameworks, most of the core coding techniques are not used here, you may forget core coding in that particular programming language. Suppose, you are a php programmer, you uses wordpress framework to build a website or cake php to build a php application, then you must follow their coding syntax ditto. Hence you will be decreasing your creation ability.

3) Slow Application
The another unusual and most important demerits of using of frame is consuming of too much time for loading of application since it uses too much unnecessary codes which also must be loaded first along with your apps codes.

4) More Threats of Hacking
Most programmers use frameworks for easy coding, easy MVC architecture and fast apps development. Therefore it has high threat of attack of hackers because they properly know the coding techniques, flow of your application's codes and also the weakness of your apps. In some cases of use of bootstrap, you can steal the whole website. Because you properly know that the site is using bootstrap. So you do not have to be worry about the css and jquery which are using in that website.

5) Deficiency of programmers creation ability
Using of Frameworks in programming decrease the new creation of programmers. It is one of the most considerable shortcoming of framework use. Programmers mostly waste their time in the following of coding syntax of frameworks. They mostly forget to use their own creation.

Therefore, I only suggest to use frameworks to those programmer who properly know about the use of frameworks. Programming Frameworks are actually for huge projects/application which consume lots of time and money. Frameworks shorten the codes that is why you must use framework if you are doing a project which demands highly core coding.