Home  • Programming • PHP

How to make pagination with php?

if(isset($_GET['pg'])){
                     $pg=(int)$_GET['pg'];
						}else{
					 $pg=1;
						 }
	
				 // i want to show 16 row on a page, so the limit is also 16		
	            $start_from=($pg-1)*16;
                   			   

					$img_sql="SELECT * 
							 FROM image_table 
							 WHERE collection_ID=$cid 
							 LIMIT $start_from,16";
                                        // your sql query here

					$res=mysql_query($img_sql) 
                                        // query execution, use mysqli_query() 
                                        //  instead of mysql_query();
					or die("Selection failed !".mysql_error());

					$count_query=mysql_query("SELECT COUNT(*) 
											FROM image_table 
											WHERE collection_ID=$cid") 
								or die("Count query failed !".mysql_error());
                                       // find out the total existing record on database table

					$total_row=mysql_fetch_array($count_query);
					$total_record=$total_row[0];
					 $total_pages=ceil($total_record/16);

			               // for showing the pagination link on your page this code 
                                        //will visulize the link
                                   
                                             echo "<div style='float:right; margin:-8px 25px 0px auto;'>"; 

		                             for ($i=1; $i<=$total_pages; $i++) {
                 
                echo "<a style='color:orange' 
                href='newcollection.php?pg=".$i."&cid=".$cid."'>                                                   
                <strong>".$i."</strong>
                  </a> ";

                                                };
			echo "</div>";									

 ?>

Comments 5


write your code within [code] ... [/code] tag
And your make post title with [h1] ... [/h1] to [h6] ... [/h6]
make section title with [b]...[/b]
You need to post your photo to blackboard for photo verification that to be displayed as your profile photo
many many thanks @ Fahad. i'm really happy.

Share

About Author
Md Fahad
Copyright © 2024. Powered by Intellect Software Ltd