next / previous row ...?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
perik
Forum Newbie
Posts: 6
Joined: Tue May 10, 2005 5:18 am

next / previous row ...?

Post by perik »

Hi!

Ive been trying to paginating my articles. First Ive been trying with this script:

Code: Select all

$goin_back = $sub_action - 1;
		$goin_ahead = $sub_action + 1;

		$NEXT = "<a href='content.php?article.$goin_ahead'>next article >></a> ";
		$PREV = "<a href='content.php?article.$goin_back'><< previous article</a> ";

		echo "$PREV";
		echo "$NEXT";
$sub_action is the article_id for example article.4

this works but the problem is that the article_id:s arn't like: 1,2,3,4,5,6,7 ...
some of the id:s are missing so the articles_id:s are more like: 1,3,6,8,11,15 ...

therefor ive been thinking that there should be a way of calling for the next and previous row in the database instead of the previous and next article_id.

How can I do this?

Anyone knows a code, maybe similiar to mine above but selecting the next row instead of next article_id???

Also how can make the next link dissapear when you have reached the last row in the databastable and same thing with previouslink when you reach the first row?

Something like:

Code: Select all

if($sub_action < FIRST ROW IN THE TABLE){
$PREV =  "";
}
if($sub_action > LAST ROW IN THE TABLE){
$NEXT =  "";
}
Thanks for your help!

Per
User avatar
gurjit
Forum Contributor
Posts: 314
Joined: Thu May 15, 2003 11:53 am
Location: UK

Post by gurjit »

hope this helps, replace the query and results with your stuff

Code: Select all

&lt;?PHP

// set how many records you want to display per page
if (!isset($lim)) {
$limit = 10;
} else {
$limit=$lim; 
}

if (!isset($start_row)) {
$start_row=0;
}

// query with a limit
$sql_enquiryP = &quote;SELECT * FROM TBL_DB LIMIT $start_row, $limit&quote;;
$mysql_result_enquiryP = mysql_query($sql_enquiryP,$conn);
$num_rowsP = mysql_num_rows($mysql_result_enquiryP);

// same query as above with out limits to get number of pages
$sql_enquiryP2 = &quote;SELECT * FROM TBL_DB&quote;;
$mysql_result_enquiryP2 = mysql_query($sql_enquiryP2,$conn);
$num_rowsP2 = mysql_num_rows($mysql_result_enquiryP2);


// count the number of pages
$no_of_pages = ceil($num_rowsP2/$limit);


?&gt;
&lt;html&gt;
&lt;head&gt;
	&lt;title&gt;Untitled&lt;/title&gt;
&lt;/head&gt;

&lt;body&gt;

&lt;?php
// do you loop
while ($row = mysql_fetch_array($mysql_result_enquiryP))
							{
		
							$id = $row&#1111;&quote;id&quote;];
							
							echo $id;
							
							 }
							 

// previous link

if ((isset($start_row)) &amp;&amp; ($start_row!=0)) {
							$back = $start_row - $limit;
							if (isset($lim)) {
							echo &quote;&lt;a href=\&quote;enquiry_list.php?start_row=$back&amp;lim=$limit\&quote; class=\&quote;NandP\&quote;&gt;&amp;lt;&amp;lt; prev&lt;/a&gt;&lt;span class=\&quote;pipes\&quote; valign=\&quote;middle\&quote;&gt; &lt;/span&gt;&quote;;
							} else { 
							echo &quote;&lt;a href=\&quote;enquiry_list.php?start_row=$back&amp;lim=$limit\&quote; class=\&quote;NandP\&quote;&gt;&amp;lt;&amp;lt; prev&lt;/a&gt;&lt;span class=\&quote;pipes\&quote; valign=\&quote;middle\&quote;&gt; &lt;/span&gt;&quote;;
							}
							}
							
							
// page display 

if ((!isset($start_row)) || ($start_row==0)) {
							$page = 1;
							} else {
							$page = (($start_row+$limit)/$limit);
							}
							if ($no_of_pages==0) {
							$no_of_pages=1;
							}
							echo &quote;Page &quote;.$page.&quote; of &quote;. $no_of_pages;
							
							
// next link

$end_row = (($no_of_pages*$limit)-$limit);
							if ((isset($start_row)) &amp;&amp; ($start_row!=$end_row) &amp;&amp; ($no_of_pages!=0)) {
							$next = $start_row + $limit;
							if (isset($lim)) {
							echo &quote;&lt;span class=\&quote;pipes\&quote; valign=\&quote;middle\&quote;&gt; &lt;/span&gt;&lt;a href=\&quote;enquiry_list.php?start_row=$next&amp;lim=$limit\&quote; class=\&quote;NandP\&quote;&gt;next &amp;gt;&amp;gt;&lt;/a&gt;&quote;;
							} else {
							echo &quote;&lt;span class=\&quote;pipes\&quote; valign=\&quote;middle\&quote;&gt; &lt;/span&gt;&lt;a href=\&quote;enquiry_list.php?start_row=$next&amp;lim=$limit\&quote; class=\&quote;NandP\&quote;&gt;next &amp;gt;&amp;gt;&lt;/a&gt;&quote;;
							}
							}


?&gt;

&lt;/body&gt;
&lt;/html&gt;
Post Reply