Is this code is efficient?
Posted: Sat Jan 22, 2011 11:32 am
Hey y'all,
I have a table consisting of records that are many in numbers. And I want to display all the records.
As number of records are more, displaying on single page is not a good way to go to.
So I wrote this code that displays records pagewise.
$start and $stop are passed to display next 3 records while $start2 and $stop2 are passed to display previous 3 records.
No of records on single page are decided as 3.
Thank you. I appreciate your help.
I have a table consisting of records that are many in numbers. And I want to display all the records.
As number of records are more, displaying on single page is not a good way to go to.
So I wrote this code that displays records pagewise.
Code: Select all
<?php
mysql_connect("localhost","phazorRise","password")or die("Couldn't make connection");
if (! @mysql_select_db("global") ) {
echo( "<P>Unable to locate the " ."database at this time.</P>" );
exit(); }
$result=mysql_query("select text from test");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit(); }
$no_of_rows=(int) mysql_num_rows($result);
if(isset($stop))
{
$start=disp($start,$stop,$no_of_rows,$start2,$stop2);
}
else
{
$start=disp(0,3,$no_of_rows,$start2,$stop2);
}
function disp($start,$stop,$no_of_rows,$start2,$stop2)
{
if (! @mysql_select_db("global") ) {
echo( "<P>Unable to locate the joke " ."database at this time.</P>" );
exit(); }
$result=mysql_query("select text from test");
if (!$result) {
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit(); }
while($start<$stop)
{
if($start<$no_of_rows)
{
$check=mysql_data_seek ($result,$start);
if(!$check){
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit(); }
$final=mysql_fetch_array($result);
if(!$final){
echo("<P>Error performing query: " .
mysql_error() . "</P>");
exit(); }
print $start.$final["text"].'<br>';
$start++;
}
}
return $start;
}
$stop=$stop+6;
$start2=$start-3;
$stop2=$stop-3;
if($start2==0 && $stop2==3)
{
echo("<u>Previous</u>"); print ' '; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>");
}
else
{
echo("<A HREF='$PHP_SELF?stop2=$stop2&&start2=$start2'>" . "Previous</A>"); print ' '; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>");
}
?>
No of records on single page are decided as 3.
Thank you. I appreciate your help.