Category: PHP

A complete database class in PHP

In this code, we present a database class that can be easily used to perform any operation on database, e.g., select, insert, update, delete, etc. The following is the code of database class: filename: include/functions.php <?php include("db/opendb.php"); $msg = NULL; /// STARTING CODE OF Database Class ///////// class Database { public $pstmt=NULL; function FetchRecord($query) {

ajax based nested drop down and searching

In this example, we present two uses of ajax. In first use, we created an ajax based nested drop downs which are also databased enabled. In second use, we create a keyword list suggestion in ajax (as we see in google search box). For this example, we used the following database: ajaxexample.sql SET SQL_MODE =

ajax based input form in php

In this example, we create an ajax based input form. The form is not submitted to the server, rather, its values are stored in the database using ajax without submitting form. default.php <html> <head> <title></title> <script type="text/javascript" src="ajax_javascript.js"></script> </head> <body> <form id="frm" name="frm" method="post" action=""> <table border="1"> <tr> <td>City</td> <td><input id="txtcity" name="txtcity" type="text"></td> </tr> <tr>

simple ajax example in php

AJAX stands for Asynchronous Javascript And XML. With ajax, we do need to submit whole form to php, rather, we can send information from a portion of form and receive the response. In this way, the communication cost and delay in submitting the form and receiving response can be significantly reduced. The following program uses

performing encryption in php

Often we pass ID values in URLs which can pose a great security risk in php applications. It is always better to encrypt such information before sending from one page to other. Here is an example. The student.php page is encrypting the information. The studentview.php page is comparing the information with its encrypted value: student.php

cropping and creating thumbnail image in php

When we upload image, it is better to crop the image so that the image easily fits in the space provided. In the following example, an image is cropped and its thumbnail is created. Cropped image is stored in its directory whereas thumbnail image is stored in its directory. To test the example, you need

file/directory operations in php

We can do numerous file directory operations in PHP. To delete a file “unlink” function is used. In below program, we can see different operations, we can uncomment code to see the behavior. <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php $dir = "tasks/"; // to delete a single file unlink("tasks/temp.txt"); //

import/export a csv file in php

To import a CSV file in php, we can use the following code: <html> <body> <?php $fcontents = file ("uploads/marks.csv"); //A:\AAA\www\web\uploads\marks.csv ?> <table border="1"> <?php for($i=0; $i<sizeof($fcontents); $i++) { $line = trim($fcontents[$i]); $arr = explode(",", $line); ?> <tr> <td><?php echo $arr[0]; ?></td> <td><?php echo $arr[1]; ?></td> <td><?php echo $arr[2]; ?></td> <td><?php echo $arr[3]; ?></td> </tr>

using a list data structure in php

Sometimes it is handy to use “list” in php. Here is the code: <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php function small_numbers() { $myarray = array(0,1,2); return $myarray; // return array(0,1,2); } list($num1, $num2, $num3) = small_numbers(); echo $num1 . " " . $num2 . " " . $num3 . "<br>";

uploading multiple files in php

To upload multiple files in php, we can use the following code. You need to set the path of upload directory $uploaddir in the code <!doctype html> <html> <head> <meta charset="utf-8"> <title>Untitled Document</title> </head> <body> <?php if( isset($_POST['btnsubmit']) ) { //print_r($_FILES); $uploaddir = 'A:/AAA/www/web/uploads/'; foreach($_FILES["userfile"]["error"] as $key => $error) { if( $error == UPLOAD_ERR_OK) {