Jump to content

MovePrevious MoveNext using links


hellboy_matcha

Recommended Posts

Hi guys I'm kinda new to php and i need some help. I've goggled around but with no success.

 

I have a table "employees":

 

id | name | email | address | job

1 | agnes | agnes...com | new york | accounting

2 | jose | jose...com | california | manager

3 | robert | robert...com | new jersey | IT

4 | anderson | anderson...com | new york | IT

5 | kelvin | kelvin...com | california | IT

 

I want to look through this data on my form but using links previous or next.

 

How can this be done guys?

 

<?php
include 'confgTableTest.php';
include 'opendb.php';

$sql ="SELECT * FROM $tbl_tableTest";
$result =mysql_query($sql);
$row =mysql_fetch_array($result);

...

?>
<form method=post>
ID:<input type="text" name="id" value="<?php ?>"><br /><br />
Name:<input type="text" name="name" value="<?php ?>"><br /><br />
Address:<input type="text" name="address" value="<?php ?>"><br /><br />
Job:<input type="text" name="job" value="<?php ?>"><br /><br />
City:<input type="text" name="city" value="<?php ?>"><br /><br />
<a href="nextPrevious.php?ID=<?php ?>">Previous</a> 
<a href="nextPrevious.php?ID=<?php ?>">Next</a> 
</form>

 

Regards,

Link to comment
https://forums.phpfreaks.com/topic/134366-moveprevious-movenext-using-links/
Share on other sites

Thnkx Crayon Violent

 

This is what i got and it's working pretty well. But instead i want this to work with my Mysql table "members" instead of the hard coded arrays. It actually has the same fields ID| Name| Email| Address| Job| City

 

What changes should be done on the code below to meet those needs?

 

<?php

$row[0]['ID']="1";
$row[0]['Name']="Agnes";
$row[0]['Email']="Agnes.com";
$row[0]['Address']="New York";
$row[0]['Job']="Accounting";
$row[0]['City']="New York";

$row[1]['ID']="2";
$row[1]['Name']="Jose";
$row[1]['Email']="Jose.com";
$row[1]['Address']="Cali";
$row[1]['Job']="Manager";
$row[1]['City']="Cali";

$row[2]['ID']="3";
$row[2]['Name']="Robert";
$row[2]['Email']="Robert.com";
$row[2]['Address']="New Jersey";
$row[2]['Job']="IT";
$row[2]['City']="New J";

$row[3]['ID']="4";
$row[3]['Name']="Anderson";
$row[3]['Email']="Anderson.com";
$row[3]['Address']="Texas";
$row[3]['Job']="IT";
$row[3]['City']="Texas";

$row[4]['ID']="5";
$row[4]['Name']="Kelvin";
$row[4]['Email']="Kelvin.com";
$row[4]['Address']="Cali";
$row[4]['Job']="IT";
$row[4]['City']="Cali";

$x = isset($_GET['ID'])?$_GET['ID']:0;
$l = count($row)-1;
$p = $x-1;
$n = $x+1;

echo"<form method=post>\n
ID:<input type='text' name='id' value='".$row[$x]['ID']."'><br /><br />\n
Name:<input type='text' name='name' value='".$row[$x]['Name']."'><br /><br />\n
Address:<input type='text' name='address' value='".$row[$x]['Address']."'><br /><br />\n
Job:<input type='text' name='job' value='".$row[$x]['Job']."'><br /><br />\n
City:<input type='text' name='city' value='".$row[$x]['City']."'><br /><br />\n";

if($x=="0")
{
    echo "Previous";
}
else
{
    echo "<a href='nextPrevious.php?ID=".$p."'>Previous</a>";
}
echo " || ";
if($x==$l)
{
    echo "Next";
}
else
{
    echo "<a href='nextPrevious.php?ID=".$n."'>Next</a>";
}
?>

 

Regards,

You need to put your data in your database.  You can put it in by hand or use a loop to insert those arrays into your table.  Once your data is in your database, you can get rid of those arrays.  Since it looks like you're wanting to pull info one row at a time, the only part of the pagination tutorial you really need, is the part that checks to see if the current page number from the $_GET var is valid, and generate a prev/next link from that.

I got it working  :shocked!:.

 

Here is the entire code:

<?php
include 'confgTableTest.php';
include 'opendb.php';

$sql ="SELECT * FROM $tbl_tableTest";
$result =mysql_query($sql);
while($line =mysql_fetch_array($result)) {
     $row[] = $line;
}

$x = isset($_GET['ID'])?$_GET['ID']:0;
$l = mysql_num_rows($result)-1;
$p = $x-1;
$n = $x+1;

echo"<form method=post>\n
ID:<input type='text' name='id' value='".$row[$x]['ID']."'><br /><br />\n
Name:<input type='text' name='name' value='".$row[$x]['Name']."'><br /><br />\n
Email:<input type='text' name='email' value='".$row[$x]['Email']."'><br /><br />\n
Address:<input type='text' name='address' value='".$row[$x]['Address']."'><br /><br />\n
Job:<input type='text' name='job' value='".$row[$x]['Job']."'><br /><br />\n
City:<input type='text' name='city' value='".$row[$x]['City']."'><br /><br />\n";

if($x=="0")
{
    echo "Previous";
}
else
{
    echo "<a href='nextPrevious.php?ID=".$p."'>Previous</a>";
}
echo " || ";
if($x>=$l)
{
    echo "Next";
}
else
{
    echo "<a href='nextPrevious.php?ID=".$n."'>Next</a>";
}
?>

 

Regards,

One thing: there's no reason for you to be doing a blanket query and returning your entire table every single time, because you're only using 1 row each page load.  Move the $x assignment to before the query and then base your query off of $x (SELECT * FROM $tbl_tableTest where ID = $x)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.