Display the highest number data from MySql Database
<?php
//protected area orig3 file
$query = "SELECT MAX(ttravelid) FROM travelid";
$resultx = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($resultx))
{
//echo "The most highest reservation id is ".$row['MAX(reservationid)'];
$ttravelid1 = $row['MAX(ttravelid)'];
//echo $ttraveliddd;
}
//this is the max number
echo $ttravelid1;
?>
or you can use sql, this will display the highest id number of the personal data.
<?php
$con = mysql_connect("localhost","root");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("my_db", $con);
$result = mysql_query("SELECT * FROM person order by id DESC limit 1");
echo "<table border='1'>
<tr>
<th>Firstname</th>
<th>Lastname</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
Search This Blog
Wednesday, October 27, 2010
Thursday, October 7, 2010
PHP FOR NEWS LETTER
code to paste:
<table align="center" border="0">
<form method="post" action="addnews.php">
<tr>
<td align="right"><font face="tahoma" size="2"><b>News Title</b></font></td>
<td align="left"><input type="text" name="newstitle"></td>
</tr>
<tr>
<td align="right"><font face="tahoma" size="2"><b>Message</b></font></td>
<td align="left">
<textarea rows="10" cols="30" wrap="physical" name="message">
Enter Comments Here
</textarea>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="submit">
<input type="reset" value="reset">
</td>
</td>
</form>
</table>
sample output:
$con = mysql_connect("localhost","root","youpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("newsletter", $con);
$result = mysql_query("SELECT * FROM news order by newsid DESC");
echo "<table border='0' width='500' align='center'>
<tr>
<td align='center'><font color='#2E57A5' size='5'><b>Latest News</b></font></td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><b><font face='tahoma' size='2'>" . $row['newstitle'] . "</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='justify'><font face='tahoma' size='2'>" . $row['message'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
note: this is only DUMMY sample , try to add date for the newsletter.
newsentry.php
<table align="center" border="0">
<form method="post" action="addnews.php">
<tr>
<td align="right"><font face="tahoma" size="2"><b>News Title</b></font></td>
<td align="left"><input type="text" name="newstitle"></td>
</tr>
<tr>
<td align="right"><font face="tahoma" size="2"><b>Message</b></font></td>
<td align="left">
<textarea rows="10" cols="30" wrap="physical" name="message">
Enter Comments Here
</textarea>
</td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" value="submit">
<input type="reset" value="reset">
</td>
</td>
</form>
</table>
sample output:
Then insert/save data to mysql database. See below.
addnews.php
<html>
<body>
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("newsletter", $con);
$newstitle=$_REQUEST['newstitle'];
$message=$_REQUEST['message'];
$sql="INSERT INTO news (newstitle, message)
VALUES
('$newstitle','$message')";
if (!mysql_query($sql,$con))
{
die('Error: ' . mysql_error());
}
echo "1 record added";
mysql_close($con)
?>
Then dipslay newsletter.
latestnews.php
$con = mysql_connect("localhost","root","youpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("newsletter", $con);
$result = mysql_query("SELECT * FROM news order by newsid DESC");
echo "<table border='0' width='500' align='center'>
<tr>
<td align='center'><font color='#2E57A5' size='5'><b>Latest News</b></font></td>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td> </td>";
echo "</tr>";
echo "<tr>";
echo "<td><b><font face='tahoma' size='2'>" . $row['newstitle'] . "</b></td>";
echo "</tr>";
echo "<tr>";
echo "<td align='justify'><font face='tahoma' size='2'>" . $row['message'] . "</font></td>";
echo "</tr>";
}
echo "</table>";
mysql_close($con);
?>
note: this is only DUMMY sample , try to add date for the newsletter.
output:
PHP readmore using strip-tags
code to paste:
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM jos_content where catid='5' order by ID DESC limit 2");
echo "<table border='0' width='215' align='center' cellpadding='0' cellspacing='0'>";
while($row = mysql_fetch_array($result))
{
$dateformatted_a = $row['created'];
//$dateformatted_1 = date("Y-m-d",strtotime($dateformatted_a));
$string= strip_tags($row['introtext']);
$readmorea = substr($string, 0, 50);
echo "<tr>";
echo "<td align='center' valign='top' width='0' align='left'><br><img src='images/stories/". $row['metadesc'] ."' width='70' height='54'></td>";
echo "<td width='214' valign='top' align='left'>";
echo "<table border='0' align='center' width='100%' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<td><br><b>" .$dateformatted_1. "</b><br>" .$readmorea. "...</td>";
echo "</tr>";
echo "<tr>";
echo '<td align="right"><br><b><a href="id='.$row['id'].'">» View Articles</a></td>';
echo "</tr>";
echo "<tr>";
echo "<td align='right'> </td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
$result = mysql_query("SELECT * FROM jos_content where catid='5' order by ID DESC limit 2");
echo "<table border='0' width='215' align='center' cellpadding='0' cellspacing='0'>";
while($row = mysql_fetch_array($result))
{
$dateformatted_a = $row['created'];
//$dateformatted_1 = date("Y-m-d",strtotime($dateformatted_a));
$string= strip_tags($row['introtext']);
$readmorea = substr($string, 0, 50);
echo "<tr>";
echo "<td align='center' valign='top' width='0' align='left'><br><img src='images/stories/". $row['metadesc'] ."' width='70' height='54'></td>";
echo "<td width='214' valign='top' align='left'>";
echo "<table border='0' align='center' width='100%' cellspacing='0' cellpadding='0'>";
echo "<tr>";
echo "<td><br><b>" .$dateformatted_1. "</b><br>" .$readmorea. "...</td>";
echo "</tr>";
echo "<tr>";
echo '<td align="right"><br><b><a href="id='.$row['id'].'">» View Articles</a></td>';
echo "</tr>";
echo "<tr>";
echo "<td align='right'> </td>";
echo "</tr>";
echo "</table>";
echo "</td>";
echo "</tr>";
}
echo "</table>";
?>
Wednesday, October 6, 2010
PHP Delete Record Entry script
Sample delete record php script
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hotelreservation", $con);
$hotelid=$_POST['hotelid'];
$accountpassword=$_POST['accountpassword'];
$resultx = mysql_query("SELECT * FROM location where locationpassword='$accountpassword'");
$count=mysql_num_rows($resultx);
if($count==1)
{
mysql_query("delete from room where hotelid='$hotelid'");
echo "record deleted";
}
else
{
echo"<br><br><br><br><br><br>";
echo "<p align='center'><b>Your id is invalid.</b></p>";
}
mysql_close($con)
?>
or you can use this one to delete record
delete.php
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hotelreservation", $con);
// Get value first from form.
$id=$_GET['id'];
// create for delete statement
mysql_query("delete from name where id='$id'");
// Close the datebase connection
mysql_close();
// Redirect to recordelet_info.php
header("location:recordelete_info.php");
mysql_close($con)
?>
recordelete_info.php
<?php
echo "record deleted";
?>
or
delete.php this one to delete record
<?php
Note: This will delete all the records that have number id=3
mysql_query("delete from personelinfo where idno like '3'");
echo"<br><br><br><br><br><br>";
echo "<p align='center'><b>Record deleted.</b></p>";
?>
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hotelreservation", $con);
$hotelid=$_POST['hotelid'];
$accountpassword=$_POST['accountpassword'];
$resultx = mysql_query("SELECT * FROM location where locationpassword='$accountpassword'");
$count=mysql_num_rows($resultx);
if($count==1)
{
mysql_query("delete from room where hotelid='$hotelid'");
echo "record deleted";
}
else
{
echo"<br><br><br><br><br><br>";
echo "<p align='center'><b>Your id is invalid.</b></p>";
}
mysql_close($con)
?>
or you can use this one to delete record
delete.php
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("hotelreservation", $con);
// Get value first from form.
$id=$_GET['id'];
// create for delete statement
mysql_query("delete from name where id='$id'");
// Close the datebase connection
mysql_close();
// Redirect to recordelet_info.php
header("location:recordelete_info.php");
mysql_close($con)
?>
recordelete_info.php
<?php
echo "record deleted";
?>
or
delete.php this one to delete record
<?php
Note: This will delete all the records that have number id=3
mysql_query("delete from personelinfo where idno like '3'");
echo"<br><br><br><br><br><br>";
echo "<p align='center'><b>Record deleted.</b></p>";
?>
Round off a number
<?php
$number = "28";
$number = number_format($number, 2);
echo $number; // equals "28.00"
$average = $totalrank_totaling / 3;
$average_rounded =round($average, 2);
echo $number;
echo "<br>";
echo $average_rounded;
?>
$number = "28";
$number = number_format($number, 2);
echo $number; // equals "28.00"
$average = $totalrank_totaling / 3;
$average_rounded =round($average, 2);
echo $number;
echo "<br>";
echo $average_rounded;
?>
Formatting Date
code to format date:
for example, after search date from mysql database, then you can format date.
Here is the sample code.
$created = $row['created'];
$created_crea = date("Y-m-d",strtotime($created));
for example, after search date from mysql database, then you can format date.
Here is the sample code.
$created = $row['created'];
$created_crea = date("Y-m-d",strtotime($created));
PHP sub query
PHP sub query 1
code to paste:
<?php
$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample", $con);
$result = mysql_query("SELECT * FROM subjectentry order by coursecode");
echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))
{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";
//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
while($rowx = mysql_fetch_array($resultx))
{
echo "<p>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<p>";
}
echo "</td>";
//Sub query type like as your data shaping
echo "</tr>";
}
echo "</table>";
mysql_close($con)
?>
output:
PHP sub query 2
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("students", $con);
$result = mysql_query("SELECT * FROM subjectentry order by coursecode");
echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))
{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";
//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
echo '<table border="1">';
while($rowx = mysql_fetch_array($resultx))
{
echo '<tr>';
echo "<td>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<td>";
echo '</tr>';
}
echo '</table>';
?>
output:
code to paste:
<?php
$con = mysql_connect("localhost","root","mypassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("sample", $con);
$result = mysql_query("SELECT * FROM subjectentry order by coursecode");
echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))
{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";
//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
while($rowx = mysql_fetch_array($resultx))
{
echo "<p>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<p>";
}
echo "</td>";
//Sub query type like as your data shaping
echo "</tr>";
}
echo "</table>";
mysql_close($con)
?>
output:
PHP sub query 2
<?php
$con = mysql_connect("localhost","root","yourpassword");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("students", $con);
$result = mysql_query("SELECT * FROM subjectentry order by coursecode");
echo "<table border='1'>
<tr>
<th><b>Coursecode</b></th>
<th><b>Students</b></th>
</tr>";
while($row = mysql_fetch_array($result))
{
$shape = $row['coursecode'];
echo "<tr>";
echo "<td valign='top'><b>" . $row['coursecode'] . "</b></td>";
//Sub query type serves as your data shaping
$resultx = mysql_query("SELECT * FROM subjectlist where coursecode = '$shape' order by completename");
echo "<td>";
echo '<table border="1">';
while($rowx = mysql_fetch_array($resultx))
{
echo '<tr>';
echo "<td>";
echo $rowx['completename'] . " " . $rowx['address'];
echo "<td>";
echo '</tr>';
}
echo '</table>';
?>
output:
PHP Basic computation with formatting number
code to paste:
<?php
$rate_per_day = 250;
$days_of_work = 10;
$cash_advance = 100;
$gross_salaryy = $rate_per_day * $days_of_work;
$gross_salary = number_format($gross_salaryy, 2, '.', ',');
$net_payy = $gross_salaryy - $cash_advance;
$net_pay = number_format($net_payy, 2, '.', ',');
echo '<br><br><p align="center">Your rate per day is:'.$rate_per_day.'</p>';
echo '<p align="center">Your days work is:'.$days_of_work.'</p>';
echo '<p align="center">Your cash advance is:'.$cash_advance.'</p>';
echo '<p align="center">Your gross pay is:'.$gross_salary.'</p>';
echo '<p align="center">Your net pay is:'.$net_pay.'</p>';
?>
<?php
$rate_per_day = 250;
$days_of_work = 10;
$cash_advance = 100;
$gross_salaryy = $rate_per_day * $days_of_work;
$gross_salary = number_format($gross_salaryy, 2, '.', ',');
$net_payy = $gross_salaryy - $cash_advance;
$net_pay = number_format($net_payy, 2, '.', ',');
echo '<br><br><p align="center">Your rate per day is:'.$rate_per_day.'</p>';
echo '<p align="center">Your days work is:'.$days_of_work.'</p>';
echo '<p align="center">Your cash advance is:'.$cash_advance.'</p>';
echo '<p align="center">Your gross pay is:'.$gross_salary.'</p>';
echo '<p align="center">Your net pay is:'.$net_pay.'</p>';
?>
SQL display the highest number Function using MAX() Function
The purpose of this is to display highest number value from specific field of a table's Msql Database.
code to paste:
//protected area orig3 file
$query = "SELECT MAX(ttravelid) FROM travelid";
$resultx = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($resultx))
{
//echo "The most highest reservation id is ".$row['MAX(reservationid)'];
$ttravelid1 = $row['MAX(ttravelid)'];
//or your can display this side echo $ttraveliddd;
}
echo "the highest number is:<br>";
echo $ttravelid1;
code to paste:
//protected area orig3 file
$query = "SELECT MAX(ttravelid) FROM travelid";
$resultx = mysql_query($query) or die(mysql_error());
while($row = mysql_fetch_array($resultx))
{
//echo "The most highest reservation id is ".$row['MAX(reservationid)'];
$ttravelid1 = $row['MAX(ttravelid)'];
//or your can display this side echo $ttraveliddd;
}
echo "the highest number is:<br>";
echo $ttravelid1;
Tuesday, October 5, 2010
Pagination with alternate background color, complete pagination First, Last, Next & Previous using While Loop
note: you can hide other pagination.
code to paste:
<?php
$conn=mysql_connect('localhost', 'root', 'yourpassword') or die(mysql_error());
mysql_select_db('cebuproperties', $conn);
//orig mysql_select_db('onlinereseravation', $conn);
$sql = "SELECT COUNT(*) FROM jos_iproperty";
//orig $sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
/////////////////////////////
$page;
$totResults;
$maxResults=4;
if(isset($_GET['page'])) {
$page=$_GET['page'];
}
else {
$page=1;
}
// get the info from the db
$sql = "SELECT * FROM jos_iproperty LIMIT $offset, $rowsperpage";
//orig $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$i=1;
//
$RowCounter=0;
//
echo '<table border="1" align="center">';
while($row = mysql_fetch_array($result))
{
if($RowCounter % 2)
{
$RowColor="bgcolor='red'";
}
else
{
$RowColor="bgcolor='yellow'";
}
if ($i==1)
echo "<tr ".$RowColor.">";
echo "<td align='center'>";
echo "<table border='1' width='1' align='center'>";
echo "<tr>";
echo "<td align='center'>".$row['title']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==1)
{
$i=0;
echo "</tr>";
}
$i ;
$RowCounter ;
}
echo "</table> \n <p class='style3'> <center>";
//
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage $range) 1); $x ) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>
sample output:
code to paste:
<?php
$conn=mysql_connect('localhost', 'root', 'yourpassword') or die(mysql_error());
mysql_select_db('cebuproperties', $conn);
//orig mysql_select_db('onlinereseravation', $conn);
$sql = "SELECT COUNT(*) FROM jos_iproperty";
//orig $sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 5;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
/////////////////////////////
$page;
$totResults;
$maxResults=4;
if(isset($_GET['page'])) {
$page=$_GET['page'];
}
else {
$page=1;
}
// get the info from the db
$sql = "SELECT * FROM jos_iproperty LIMIT $offset, $rowsperpage";
//orig $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$i=1;
//
$RowCounter=0;
//
echo '<table border="1" align="center">';
while($row = mysql_fetch_array($result))
{
if($RowCounter % 2)
{
$RowColor="bgcolor='red'";
}
else
{
$RowColor="bgcolor='yellow'";
}
if ($i==1)
echo "<tr ".$RowColor.">";
echo "<td align='center'>";
echo "<table border='1' width='1' align='center'>";
echo "<tr>";
echo "<td align='center'>".$row['title']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==1)
{
$i=0;
echo "</tr>";
}
$i ;
$RowCounter ;
}
echo "</table> \n <p class='style3'> <center>";
//
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage $range) 1); $x ) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>
sample output:
Saturday, October 2, 2010
PHP Complete Pagination, first, previous, next and last record/Photo Gallery pagination
code to paste:
note: insert this code that you want to have pagination page:
<html>
<body>
<br>
<table width="500" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center" colspan="3"><font face="tahoma" size="4">sample photo gallery with paging</font></td>
</tr>
//start php
$conn=mysql_connect('localhost', 'root', 'yourpassword') or die(mysql_error());
mysql_select_db('photogallery', $conn);
//orig mysql_select_db('onlinereseravation', $conn);
////////////////////////////////////////////////
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM table1";
//orig $sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 4;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
/////////////////////////////
$page;
$totResults;
$maxResults=4;
if(isset($_GET['page'])) {
$page=$_GET['page'];
}
else {
$page=1;
}
// get the info from the db
$sql = "SELECT lastname, imagepath FROM table1 LIMIT $offset, $rowsperpage";
//orig $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$i=1;
while($row = mysql_fetch_array($result))
{
if ($i==1)
echo "<tr>";
echo "<td align='center'>";
echo "<table border='0' width='1' align='center'>";
echo "<tr><td align='center'><img src=".$row['imagepath']." width='100' height='100'></td></tr>";
echo "<tr>";
echo "<td align='center'>".$row['imagepath']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==2)
{
$i=0;
echo "</tr>";
}
$i ;
}
echo "</table> \n <p class='style3'> <center>";
//
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage $range) 1); $x ) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>
</body>
</html>
output:
note: insert this code that you want to have pagination page:
<html>
<body>
<br>
<table width="500" border="1" cellspacing="0" cellpadding="0" align="center">
<tr>
<td align="center" colspan="3"><font face="tahoma" size="4">sample photo gallery with paging</font></td>
</tr>
//start php
$conn=mysql_connect('localhost', 'root', 'yourpassword') or die(mysql_error());
mysql_select_db('photogallery', $conn);
//orig mysql_select_db('onlinereseravation', $conn);
////////////////////////////////////////////////
// find out how many rows are in the table
$sql = "SELECT COUNT(*) FROM table1";
//orig $sql = "SELECT COUNT(*) FROM numbers";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$r = mysql_fetch_row($result);
$numrows = $r[0];
// number of rows to show per page
$rowsperpage = 4;
// find out total pages
$totalpages = ceil($numrows / $rowsperpage);
// get the current page or set a default
if (isset($_GET['currentpage']) && is_numeric($_GET['currentpage'])) {
// cast var as int
$currentpage = (int) $_GET['currentpage'];
} else {
// default page num
$currentpage = 1;
} // end if
// if current page is greater than total pages...
if ($currentpage > $totalpages) {
// set current page to last page
$currentpage = $totalpages;
} // end if
// if current page is less than first page...
if ($currentpage < 1) {
// set current page to first page
$currentpage = 1;
} // end if
// the offset of the list, based on current page
$offset = ($currentpage - 1) * $rowsperpage;
/////////////////////////////
$page;
$totResults;
$maxResults=4;
if(isset($_GET['page'])) {
$page=$_GET['page'];
}
else {
$page=1;
}
// get the info from the db
$sql = "SELECT lastname, imagepath FROM table1 LIMIT $offset, $rowsperpage";
//orig $sql = "SELECT id, number FROM numbers LIMIT $offset, $rowsperpage";
$result = mysql_query($sql, $conn) or trigger_error("SQL", E_USER_ERROR);
$i=1;
while($row = mysql_fetch_array($result))
{
if ($i==1)
echo "<tr>";
echo "<td align='center'>";
echo "<table border='0' width='1' align='center'>";
echo "<tr><td align='center'><img src=".$row['imagepath']." width='100' height='100'></td></tr>";
echo "<tr>";
echo "<td align='center'>".$row['imagepath']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==2)
{
$i=0;
echo "</tr>";
}
$i ;
}
echo "</table> \n <p class='style3'> <center>";
//
/****** build the pagination links ******/
// range of num links to show
$range = 3;
// if not on page 1, don't show back links
if ($currentpage > 1) {
// show << link to go back to page 1
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=1'>First</a> ";
// get previous page num
$prevpage = $currentpage - 1;
// show < link to go back to 1 page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$prevpage'>Previous</a> ";
} // end if
// loop to show links to range of pages around current page
for ($x = ($currentpage - $range); $x < (($currentpage $range) 1); $x ) {
// if it's a valid page number...
if (($x > 0) && ($x <= $totalpages)) {
// if we're on current page...
if ($x == $currentpage) {
// 'highlight' it but don't make a link
echo " [<b>$x</b>] ";
// if not current page...
} else {
// make it a link
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$x'>$x</a> ";
} // end else
} // end if
} // end for
// if not on last page, show forward and last page links
if ($currentpage != $totalpages) {
// get next page
$nextpage = $currentpage 1;
// echo forward link for next page
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$nextpage'>Next</a> ";
// echo forward link for lastpage
echo " <a href='{$_SERVER['PHP_SELF']}?currentpage=$totalpages'>Last</a> ";
} // end if
/****** end build pagination links ******/
?>
</body>
</html>
output:
Simple Photo Gallery
photo_gallery.php
<?php
// Connect database.
mysql_connect("localhost","root","yourpassword");
mysql_select_db("sample");
echo '<table align="center" border="0"><tr><td align="center" colspan="3"><font face="tahoma" size="2">Gallery</font></td></tr>';
$sql = mysql_query("SELECT * FROM table1");
$i=1;
while($row = mysql_fetch_array($sql))
{
if ($i==1)
echo "<tr>";
echo "<td align='center'>";
echo "<table border='0' width='1' align='center'>";
echo "<tr><td align='center'><img src=".$row['photo']." width='100' height='100'></td></tr>";
echo "<tr>";
echo "<td align='center'>".$row['photo']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==3)
{
$i=0;
echo "</tr>";
}
$i ;
}
?>
Sample Output:
<?php
// Connect database.
mysql_connect("localhost","root","yourpassword");
mysql_select_db("sample");
echo '<table align="center" border="0"><tr><td align="center" colspan="3"><font face="tahoma" size="2">Gallery</font></td></tr>';
$sql = mysql_query("SELECT * FROM table1");
$i=1;
while($row = mysql_fetch_array($sql))
{
if ($i==1)
echo "<tr>";
echo "<td align='center'>";
echo "<table border='0' width='1' align='center'>";
echo "<tr><td align='center'><img src=".$row['photo']." width='100' height='100'></td></tr>";
echo "<tr>";
echo "<td align='center'>".$row['photo']."</td>";
echo "</tr>";
echo "</table>";
echo "</td>";
if ($i==3)
{
$i=0;
echo "</tr>";
}
$i ;
}
?>
Sample Output:
Subscribe to:
Posts (Atom)