pagination - could someone have a look at my code please

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
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

pagination - could someone have a look at my code please

Post by mohson »

Hi after taking into consideration the advice I have been given so far by people on this forum I have now got to the stage with code that the previous next links appear as links but when they are clicked the records stay the same and dont move on to the next 10, could someone please have a look at my code and tell me where I am going wrong?

lines 9-23 and 126-166 are the most significant, I would be so greatful if someone could help me.

Code: Select all

<?php



/* Connecting, selecting database */
$link = mysql_connect("******", "****", "*******")
   or die("Could not connect : " . mysql_error());
echo "";
mysql_select_db("contact_management_system") or die("Could not select database");


    $limit          = 10;               
    $query_count    = "SELECT count(*) FROM people";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count);

    
$page = (!empty($_REQUEST['page'])) ? $_REQUEST['page'] : 1; 

 $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM people ORDER BY firstname LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error());

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    } 
   

// Begin your table outside of the array
echo "<table width="50%" border="0" cellpadding="2" cellspacing="2">
    <tr>
        <td width="110"><b><small>ID</small></b></td>
        <td><b></b></td>
		<td><b><small>First Name</small></b></td>
		<td><b><small>Surname</small></b></td>
		<td><b><small>Organisation</small></b></td>
		<td><b><center><small>Role</small></center></b></td>
		<td><b><small>Address(1)</small></b></td>
		<td><b><small>Address(2)</small></b></td>
		<td><b><small>City</small></b></td>
		<td><b><small>Post Code</small></b></td>
		<td><b><small>Telephone</small></b></td>
		<td><b><small>Mobile</small></b></td>
		<td><b><small>Fax</small></b></td>
		<td><b><small>Last Contact</small></b></td>
		<td><b><small>Contact Again</small></b></td>
		<td><b><small>Notes</small></b></td>
		
    </tr>"; 
	
// Define your colors for the alternating rows

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$row_count = 0; 
	




/* Printing results in HTML */

while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
	$person_id = $line["person_id"];
    $salutation = $line["salutation"];
	$firstname = $line["firstname"];
    $surname = $line["surname"]; 
	$organisation = $line["organisation"];
	$role = $line["role"];
	$address1 = $line["address1"];
	$address2 = $line["address2"];
	$city = $line["city"];
	$postcode = $line["postcode"];
	$telephone = $line["telephone"];
	$mobile = $line["mobile"];
	$fax = $line["fax"];
	$dateoflastcontact = $line["dateoflastcontact"];
	$datecontactagain = $line["datecontactagain"];
	$notes = $line["notes"];
	$email = $line["email"];
	
	
	/* Now we do this small line which is basically going to tell
    PHP to alternate the colors between the two colors we defined above. */

    $row_color = ($row_count % 2) ? $color1 : $color2; 
	
	
	 // Echo your table row and table data that you want to be looped over and over here. 
  
echo "<tr>
    <td width="20" bgcolor="$row_color" 	 nowrap><a href ="Upeople.html">$person_id 
    </td>
    <td bgcolor="$row_color">$salutation </td>
	<td width="110" bgcolor="$row_color" nowrap><a href="mailto: $email">$firstname 
    </td>
	<td width="100" bgcolor="$row_color" nowrap><a href="mailto: $email">$surname 
    </td>
	<td width="100" bgcolor="$row_color" nowrap>$organisation 
    </td>
	<td bgcolor="$row_color">$role</td>
	<td width="100" bgcolor="$row_color" nowrap>$address1
    </td>
	<td width="100" bgcolor="$row_color" nowrap>$address2
    </td>
	<td bgcolor="$row_color">$city</td>
	<td width="110" bgcolor="$row_color" nowrap>$postcode
    </td>
	<td bgcolor="$row_color">$telephone</td>
	<td bgcolor="$row_color">$mobile</td>
	<td bgcolor="$row_color">$fax</td>
	<td width="100" bgcolor="$row_color" nowrap>$dateoflastcontact
    </td>
	<td width="100" bgcolor="$row_color" nowrap>$datecontactagain
    </td>
	<td width="300" bgcolor="$row_color" nowrap>$notes 
    </td>
	
	
	
	</tr>"; 
	
	    // Add 1 to the row count

    $row_count++;
}
 
//break before paging
 echo "<br />";

 // next we need to do the links to other results
 
if ($s>=1) { // bypass PREV link if s is 0
 $prevs=($s-$limit);
 print "&nbsp;<a href="$PHP_SELF?s=$prevs"><<
 Prev 10</a>&nbsp&nbsp;";
 }

// calculate number of pages needing links
 
$pages=intval($numrows/$limit);

// $pages now contains int of pages needed unless there is a remainder from division

 if ($numrows%$limit) {

 // has remainder so add one page
 
$pages++;
 }

// check to see if last page
 
if (!((($s+$limit)/$limit)==$pages) && $pages!=1) {

 // not last page so give NEXT link
 
$news=$s+$limit;

 echo "&nbsp;<a href="$PHP_SELF?s=$news">Next 10 >></a>";
 }

$a = $s + ($limit);
 if ($a > $numrows) { $a = $numrows; }
 $b = $s + 1;
 echo "<p>Showing results $b to $a of $numrows</p>";
    
    mysql_free_result($result);

/* Free resultset */
mysql_free_result($result);

/* Closing connection */
mysql_close($link);



?>
</table>
Last edited by mohson on Wed Dec 08, 2004 4:23 am, edited 4 times in total.
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

try putting

Code: Select all

$page = $_REQUEST&#1111;'page']
on line 12

or better still replace lines 13 - 15 with

Code: Select all

$page = (!empty($_REQUEST&#1111;'page'])) ? $_REQUEST&#1111;'page'] : 1;
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Thanks for your help - but I think im facing a more basic problem - as the PREV NEXT LINKS appear on my screen but only as text not as a hyperlink - this is what I find really weird I think the error is in the <a h ref... part of the code...

Can you help?????
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

use PEAR Paging package

pagination with stagination
Last edited by harsha on Tue Dec 07, 2004 6:31 am, edited 1 time in total.
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

sorry I dont understand the reply
User avatar
potsed
Forum Commoner
Posts: 50
Joined: Sat Oct 09, 2004 12:00 pm
Location: - South Africa

Post by potsed »

This worked on my setup... However you might want to look at cleaning up the code.

The other thing i noticed was that in your prev & next links you had a

Code: Select all

&q=$var

as there is no q or $var in the script, i dont know if it is an extra thats not needed or if you have any other reason to use it, if not i would delete it from the lines.

Code: Select all

<?php
/* Connecting, selecting database */
$link = mysql_connect("***", '***', '***')
   or die("Could not connect : " . mysql_error());
   
mysql_select_db("contact_management_system") or die("Could not select database");


    $limit          = 10;               
    $query_count    = "SELECT * FROM people";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count);
    $totalpages = ceil($totalrows/$limit);
    
    $page = (!empty($_REQUEST['page'])) ? $_REQUEST['page'] + 1 : 1; 
    $s = $page;
	
    $limitvalue = $page * $limit - ($limit);
    $query  = "SELECT * FROM people ORDER BY firstname LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error());
	$numrows = mysql_num_rows($result);
    if($numrows == 0){
        echo("Nothing to Display!");
    } 
   

// Begin your table outside of the array
echo "<table width="50%" border="0" cellpadding="2" cellspacing="2">
    <tr>
        <td width="110"><b><small>ID</small></b></td>
        <td><b></b></td>
        <td><b><small>First Name</small></b></td>
        <td><b><small>Surname</small></b></td>
        <td><b><small>Organisation</small></b></td>
        <td><b><center><small>Role</small></center></b></td>
        <td><b><small>Address(1)</small></b></td>
        <td><b><small>Address(2)</small></b></td>
        <td><b><small>City</small></b></td>
        <td><b><small>Post Code</small></b></td>
        <td><b><small>Telephone</small></b></td>
        <td><b><small>Mobile</small></b></td>
        <td><b><small>Fax</small></b></td>
        <td><b><small>Last Contact</small></b></td>
        <td><b><small>Contact Again</small></b></td>
        <td><b><small>Notes</small></b></td>
        
    </tr>"; 
    
// Define your colors for the alternating rows

$color1 = "#ADD8E6";
$color2 = "#E0FFFF";
$row_count = 0; 
    




/* Printing results in HTML */


while ($line = mysql_fetch_array($result, MYSQL_ASSOC)) {
    $person_id = $line["person_id"];
    $salutation = $line["salutation"];
    $firstname = $line["firstname"];
    $surname = $line["surname"]; 
    $organisation = $line["organisation"];
    $role = $line["role"];
    $address1 = $line["address1"];
    $address2 = $line["address2"];
    $city = $line["city"];
    $postcode = $line["postcode"];
    $telephone = $line["telephone"];
    $mobile = $line["mobile"];
    $fax = $line["fax"];
    $dateoflastcontact = $line["dateoflastcontact"];
    $datecontactagain = $line["datecontactagain"];
    $notes = $line["notes"];
    $email = $line["email"];
    
    
    /* Now we do this small line which is basically going to tell
    PHP to alternate the colors between the two colors we defined above. */

    $row_color = ($row_count % 2) ? $color1 : $color2; 
    
    
     // Echo your table row and table data that you want to be looped over and over here. 
  
echo "<tr>
    <td width="20" bgcolor="$row_color"      nowrap><a href ="Upeople.html">$person_id 
    </td>
    <td bgcolor="$row_color">$salutation </td>
    <td width="110" bgcolor="$row_color" nowrap><a href="mailto: $email">$firstname 
    </td>
    <td width="100" bgcolor="$row_color" nowrap><a href="mailto: $email">$surname 
    </td>
    <td width="100" bgcolor="$row_color" nowrap>$organisation 
    </td>
    <td bgcolor="$row_color">$role</td>
    <td width="100" bgcolor="$row_color" nowrap>$address1
    </td>
    <td width="100" bgcolor="$row_color" nowrap>$address2
    </td>
    <td bgcolor="$row_color">$city</td>
    <td width="110" bgcolor="$row_color" nowrap>$postcode
    </td>
    <td bgcolor="$row_color">$telephone</td>
    <td bgcolor="$row_color">$mobile</td>
    <td bgcolor="$row_color">$fax</td>
    <td width="100" bgcolor="$row_color" nowrap>$dateoflastcontact
    </td>
    <td width="100" bgcolor="$row_color" nowrap>$datecontactagain
    </td>
    <td width="300" bgcolor="$row_color" nowrap>$notes 
    </td>
    
    
    
    </tr>"; 
    
        // Add 1 to the row count

    $row_count++;
}

//break before paging
echo "<br />";

// next we need to do the links to other results

if ($s>1) { // bypass PREV link if s is 0
$prevs=($page-2);
print "&nbsp;<a href="$PHP_SELF?page=$prevs&q=$var"><<
Prev 10</a>&nbsp&nbsp;";
}

// calculate number of pages needing links

$pages=intval($numrows/$limit);
// $pages now contains int of pages needed unless there is a remainder from division


// check to see if last page
if (((($s+$limit)-1)>=$pages) && $pages!=0) {

// not last page so give NEXT link

$news=$page;

echo "&nbsp;<a href="$PHP_SELF?page=$news&q=$var">Next 10 >></a>";
}

$a = $s * $limit;
if ($a > $totalrows) { $a = $totalrows; }
$b = $limit * $page - $limit + 1;
echo "<p>Showing results $b to $a of $totalrows</p>";
    
mysql_free_result($result);


/* Closing connection */
mysql_close($link);
?>
</table>
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

Hi i tried the suggestion and updated my code (top of this post) but Still no joy, can someone help??
User avatar
harsha
Forum Contributor
Posts: 103
Joined: Thu Jul 11, 2002 1:35 am
Location: Bengaluru (Bangalore) > Karnataka > India

Post by harsha »

Post Reply