Paging in PHP for Image Gallery
In this tutorial , we are learning that how to create paging in Php for Image Gallery.
Paging means that it is showing query result in multiple pages. It is possible with set limit in SQL Query.
In this example , there are some functions which is used to create paging in php.
- basename() : It is return the filename from the path.
- mysql_query() : for sending MySQL Query.
- mysql_fetch_array() : It is used to fetch result as associative and numeric array.
- mysql_num_rows() : It is used to fetch result as enumerated array.
- ceil() : used to round up figure.
File Name : index.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | <HTML> <BODY> <FORM METHOD="POST" ACTION="index2.php" enctype="multipart/form-data"> <pre> Image Upload<br> No :- <INPUT TYPE='TEXT' NAME='NO' required> <input type="hidden" name="MAX_FILE_SIZE" value="1000000" /> Image :- <input name="uploadedfile" type="file" required/> <br> <INPUT TYPE="SUBMIT" VALUE="Insert"> <INPUT TYPE="RESET" VALUE="CLEAR"> </pre> </FORM> </BODY> </HTML> |
File Name : index2.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 | <?php $vno=$_POST['NO']; $cn=mysql_connect("localhost","root",""); mysql_select_db("dbtest"); $fn=basename($_FILES['uploadedfile']['name']); $target_path = "./images/"; $target_path = $target_path.$fn; $tfn=$_FILES['uploadedfile']['tmp_name']; $x=move_uploaded_file($tfn,$target_path); if($x) { echo "<img src='images/".basename($_FILES['uploadedfile']['name'])."'</img> "; echo "<br>The file <u>[".basename($_FILES['uploadedfile']['name'])."</u>] has been uploaded<br>"; } else { echo "There was an error uploading the file"; die; } $s1="insert into img values($vno,'$fn')"; mysql_query($s1) or die("INSERT ".mysql_error()); mysql_query("commit") or die("commit fail ".mysql_error()); echo "<br>Record Successfully Added"; ?> <a href="paging.php">View Gallery</a> |
File Name : paging.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 | <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> <html xmlns="http://www.w3.org/1999/xhtml"> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8" /> <title>Paging Demo for Image Gallery</title> </head> <body> <?php $conn = mysql_connect("localhost","root",""); mysql_select_db("dbtest");//database name $page=0; if(isset($_POST["page"])) { $page=$_POST["page"]; $page=($page*9)-9;//counting for 9 image is displayed in one page } $res = mysql_query("select *from img limit $page,9");//set limit to display 9 images while($row=mysql_fetch_array($res)) { ?> <img src="<?php echo "./images/".$row["path"];?>" height="300" width="440px"/> <?php } $res1 = mysql_query("select *from img"); $count=mysql_num_rows($res1);//use for count how many images in database $a=$count/9; $a=ceil($a);//ceil function is used to round up figure echo "<br><br>"; ?> <form method="post"> <?php for($b=1;$b<=$a;$b++) { ?> <input type="submit" value="<?php echo $b;?>" name="page"> <?php }?> </body> </html> |
Note :- Database Name : dbtest
Table Name : img
Column Name : id , path
Image Gallery Pagination example, Image Gallery Pagination in php, Image Gallery Paging tutorial, pagination for gallery in php, Paging in php code, Paging in php example, Php Tutorial
0 comments:
Post a Comment