Show Database Records in Multi Page

Written by @m_k_amin 20 November 2012

Sometimes you face to many database records that must be shown to the users. It doesn't look good to list them all in one page and, therefor, you must have separated pages for showing them. This script categorize database records in pages limited with 10 records in each page. It also has automatic database setup and doesn't need further configuration except entering database connection data at top. There also prepared a button to fill the database with 40 new data. It lets you test the script and making new pages without need to enter data manually to make the next page. Just Enjoy!

Code Snippet:

                                                
                                                <!-- this script is provided by www.phpfreecode.com coded by: Kerixa Inc. -->
<!-- This Script is from www.phpfreecpde.com, Coded by: Kerixa Inc-->
<?php
	$host="localhost"; // Host name
	$username="root"; // Mysql username
	$password=""; // Mysql password

function setup(){
	echo('
	<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup is started...</p>
	');
	global $link;
	$sql= 'CREATE DATABASE info';
	if (!mysql_query ($sql, $link)) die('
	<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to 
	create database! </span><br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
	');
	$sql = "CREATE TABLE `info`.`info1` (
	`ID` INT NOT NULL ,
	`information` TEXT NOT NULL
	) ENGINE = InnoDB CHARACTER SET utf8 COLLATE utf8_general_ci;";
	mysql_query($sql) or die('Setup Failed');
	echo('
	<p style="color: #008000;	text-align: left;	font-size: 15pt;"">-Automatic setup completed successfully. Your Database is ready!</p>
	');
}

function FillData(){
	$sql2="SELECT * FROM info1";
	$result2=mysql_query($sql2)or die(mysql_error());
	$count=mysql_num_rows($result2);
	for($id=$count+1;$id-$count<=40;$id++){
		$info="Filled Data NO.$id";
		$sql3="INSERT INTO info1(ID,information)VALUES('$id', '$info')";
		$result3=mysql_query($sql3);
		if (!$result3) die (mysql_error());
	}
}

$link=mysql_connect($host, $username, $password);
if(!$link) die('
<p style="text-align: center;	font-size: 20pt;"><span style="color: #FF0000;">Failed to connect to the database! </span>
<br><span style="font-size: 12pt;">&gt;&gt;Please check the parameters and database server&lt;&lt;</span></p>
');

$db_name="info"; 
$result=mysql_select_db($db_name);
if(!$result){
	setup();
}
if (isset($_GET['type'])&& $_GET['type']=='Add') FillData()
?>


<table style="border: 1px solid #000000;width: 600" align="center">
	<tr>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;width: 151px;color: #2214B9;border-style: solid;border-width: 1px;"><strong>ID</strong></td>
		<td style="font-family: 'Times New Roman', Times, serif;font-size: 17pt;text-align: center;color: #2214B9;border-style: solid;border-width: 1px;">
			<strong>Information</strong></td>
	</tr>
<?php
$sort=0;
if (isset($_GET['sort'])) $sort=$_GET['sort'];
$final=$sort+9;
$sql="SELECT * FROM info1 WHERE ID >= $sort AND ID <= $final";
$result=mysql_query($sql);
$cntr=1;
while($rows=@mysql_fetch_array($result)){
	$cntr++;
?>
	<tr>
		<td style="width: 151px;border-style: solid;border-width: 1px;text-align: center; height: 39px; font-size: 14pt;">
		<strong><?php echo $rows['ID'] ?></strong></td>
		<td style="border-style: solid;border-width: 1px; height: 39px;padding-left: 8px"><?php echo $rows['information'] ?></td>
	</tr>
<?php } ?>
	<tr><form method="post" action="<?php echo $_SERVER['PHP_SELF']."?type=Add" ?>">
		<td style="border-style: solid;border-width: 1px;text-align: center;width: 151px; height: 51px; font-size: 14pt;">
		<strong><?php echo $cntr?></strong></td>
		<td style="border-style: solid;border-width: 1px;padding-left: 8px; height: 51px; text-align: center;">
			&nbsp;&nbsp;
			<input name="Sub1" type="submit" value="Add 40 Data to the Database" style="width: 260px; height: 40px"></td>
	</form></tr>
</table>

<div style="text-align: center; height: 15px">
	<strong>Pgaes:
<?php 
$sql4="SELECT * FROM info1";
$result4=mysql_query($sql4);
$count= mysql_num_rows($result4);
$pages= $count / 10;
if ($count % 10 <>0) $pages++;
if ($sort !=0){
	$p=$sort-10;
	$me= $_SERVER['PHP_SELF']."?sort=$p" ;
	echo ("
	 <b><a href=$me><<</a>&nbsp;</b>
	 ");
	 }
for ($i=1;$i<=$pages;$i++){
	$p=($i-1)*10;
	$me= $_SERVER['PHP_SELF']."?sort=$p" ;
	if ($p<>$sort){
		echo ("
		 <b><a href=$me>$i</a>&nbsp;</b>
		 ");
	 }else{
	 	echo ("
		 <b>$i&nbsp;</b>
		 ");
	 }
}
if ($sort/10 <=$pages-2){
	$p=$sort+20;
	$me= $_SERVER['PHP_SELF']."?sort=$p" ;
	echo ("
	 <b><a href=$me>>></a>&nbsp;</b>
	 ");
	 }

?><br>
	</strong>
<br><font face="Tahoma"><a target="_blank" href="http://www.phpfreecode.com/"><span style="font-size: 8pt; text-decoration: none">PHP Free Code</span></a></font></div>
<a target='_blank' href='www.phpfreecode.com' style='font-size: 8pt; text-decoration: none'>Php Best Codes</a>                                                
                                            

Example:


About @m_k_amin

This user is pending a biography.

M
TRENDING POST
1
2
3
4
5
VISITORS
Online Users: 12
Recent Members: Manaakividuinfo.com, karticksv, sava, tinatina, sandiegostrippersinsandiego
advertisement 2