George take a look at this

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

George take a look at this

Post by mohson »

George can you see any errors with this code all im trying to do is create a simple navigation bar. to present the records 10 at a time??

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");

//* This code will obtain the required page number from the $_GET array. Note that if it is not present it will default to 1.
  
if (isset($_GET['pageno'])) {
   $pageno = $_GET['pageno'];
} else {
   $pageno = 1;
} // if

//* This code will count how many rows will satisfy the current query.

$query = "SELECT count(*) FROM people ORDER BY firstname";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
$query_data = mysql_fetch_row($result);
$numrows = $query_data[0];

//*This code uses the values in $rows_per_page and $numrows in order to identify the number of the last page.

$rows_per_page = 10;
$lastpage      = ceil($numrows/$rows_per_page);

//*This code checks that the value of $pageno is an integer between 1 and $lastpage

$pageno = (int)$pageno;
if ($pageno < 1) {
   $pageno = 1;
} elseif ($pageno > $lastpage) {
   $pageno = $lastpage;
} // if

//*This code will construct the LIMIT clause for the sql SELECT statement.

$limit = 'LIMIT ' .($pageno - 1) * $rows_per_page .',' .$rows_per_page;

//*Now we can issue the database query and process the result.

$query = "SELECT * FROM people $limit";
$result = mysql_query($query, $db) or trigger_error("SQL", E_USER_ERROR);
... process contents of $result ...

//*Finally we must construct the hyperlinks which will allow the user to select other pages. 
We will start with the links for any previous pages.

if ($pageno == 1) {
   echo " FIRST PREV ";
} else {
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=1'>FIRST</a> ";
   $prevpage = $pageno-1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$prevpage'>PREV</a> ";
} // if

//*Next we inform the user of his current position in the sequence of available pages.

echo " ( Page $pageno of $lastpage ) ";

//*This code will provide the links for any following pages.

if ($pageno == $lastpage) {
   echo " NEXT LAST ";
} else {
   $nextpage = $pageno+1;
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$nextpage'>NEXT</a> ";
   echo " <a href='{$_SERVER['PHP_SELF']}?pageno=$lastpage'>LAST</a> ";
} // if


// 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++;
}
 


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

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



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

Post by harsha »

go to pear.php.net under the HTML Category there package(S) name Pagin 2.0 and Page sliding if you use that it will take less time for you to acheive naviagtion ie. pagination with stagination :-)
mohson
Forum Contributor
Posts: 372
Joined: Thu Dec 02, 2004 6:58 am
Location: London

Post by mohson »

thanks but I managed to do it my self and it works FANTASTIC
Post Reply