Page 1 of 1

Previous Next Buttons

Posted: Thu Dec 02, 2004 7:01 am
by mohson
patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Im trying to create previous next buttons on my display records page to show 10 records at a time but I havent got a clue where to put the code or what to use? would anyone be able to help?

Here is my code to display records:

Code: Select all

// 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; 
	
/* Performing SQL query */
$query = "SELECT * FROM people ORDER BY firstname "; 
$result = mysql_query($query) or die("Query failed : " . mysql_error());

/* 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>

patrikG | Help us, help you. Please use

Code: Select all

and

Code: Select all

tags where approriate when posting code. Read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url][/color]

Posted: Thu Dec 02, 2004 8:40 am
by []InTeR[]
How i do it :)

In the query who select's the 'list' has to be a limit.

Like

Code: Select all

SELECT * FROM `users` LIMIT 0,10
Will select the first 10.

And if you make the 0 a variable.

Code: Select all

SELECT * FROM `users` LIMIT $start , 10
Than in you program, you check if it's set by a 'request'.

Code: Select all

if(!isset($_REQUEST['start']) || $_REQUEST['start']<0){
    $start = 0;
  }else{
    $start = $_REQUEST['start'];
  }
And for the next page button is.

Code: Select all

<A HREF="script.php?start=<? echo $start + 10; ?>">Next</A>
This is a start, it can be finetuned by checking the number of results first and check if you realy have a next page. And you can make a backlink if $start>0

Hope to be helpfull.