Page 1 of 1

Is this code is efficient?

Posted: Sat Jan 22, 2011 11:32 am
by phazorRise
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.

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 '&nbsp;&nbsp;'; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>"); 
}
else
{
echo("<A HREF='$PHP_SELF?stop2=$stop2&&start2=$start2'>" . "Previous</A>"); print '&nbsp;&nbsp;'; echo("<A HREF='$PHP_SELF?stop=$stop&&start=$start'>" . "Next</A>"); 
}
?>
$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.

Re: Is this code is efficient?

Posted: Sun Jan 23, 2011 9:24 am
by phazorRise
plz someone tell me is above code is efficient???
i want to use it but before i want to confirm.

Re: Is this code is efficient?

Posted: Sun Jan 23, 2011 9:40 am
by VladSun
No, it's not. In fact, it's very inefficient.

Take a look at LIMIT and OFFSET MySQL keywords.

Also, google for PHP pagination class

Re: Is this code is efficient?

Posted: Sun Jan 23, 2011 10:06 am
by phazorRise
VladSun wrote:No, it's not. In fact, it's very inefficient.

Take a look at LIMIT and OFFSET MySQL keywords.

Also, google for PHP pagination class

thank you. i spent hours writting this code. but hell, it's of no use !