Link to home
Start Free TrialLog in
Avatar of jblayney
jblayneyFlag for Canada

asked on

creating next and back button

hello,

I have a page of results, each results links to a second page which displays the details of all the results, so I want to have a next and back button to scroll through the results, I have no idea where to start and wasted 3 hours now looking for tutorials online.

The first page sends the team_id(id) and team_type(id2) to the details page, my first recordset displays based on team_id, so i need a query that will start at team_id(id) and go to next record based on team_type(id2).

My first query is this...
$colname_Recordset1 = "1";
if (isset($_GET['id'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_GET['id'] : addslashes($_GET['id']);
}
mysql_select_db($database_mine, $mine);
$query_Recordset1 = sprintf("SELECT * FROM team WHERE team_id = %s", $colname_Recordset1);
$Recordset1 = mysql_query($query_Recordset1, $mine) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);
$totalRows_Recordset1 = mysql_num_rows($Recordset1);

So i decided to create a new query based on id2, and i want to make an array.... then go into my array, compare to id and select the next record... so far my echo dumps a 1 on the page and the word array..


$colname_Recordset3 = "1";
if (isset($_GET['id2'])) {
  $colname_Recordset3 = (get_magic_quotes_gpc()) ? $_GET['id2'] : addslashes($_GET['id2']);
}
mysql_select_db($database_mine, $mine);
$query_Recordset3 = sprintf("SELECT * FROM team WHERE team_type = %s", $colname_Recordset3);
$Recordset3 = mysql_query($query_Recordset3, $mine) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

do {
  $pages[] = $row_Recordset3['team_id'];
  //  do stuff with other column
  //  data if we want
} while ($row_Recordset3 = mysql_fetch_assoc($Recordset3));

echo $pages;
Avatar of aminerd
aminerd

You could...

Previous:

SELECT team_id FROM team WHERE team_type = 'TYPE' AND team_id < ID LIMIT 1;

Next:

SELECT team_id FROM team WHERE team_type = 'TYPE' AND team_id > ID LIMIT 1;

And I'd probably recommend a combined index on team_type and team_id (i.e., ALTER TABLE `team` ADD UNIQUE INDEX idx_team_id_type (team_id, team_type)).
Avatar of jblayney

ASKER

Hello,

Tx for the help,

it works except when i get to the first or last record, then the query has no result an my links die, so how would i make it so that if I am on record #1, it will select the highest id?
i notice something else, my back button always goes to first record...

this is my code...

BACK BUTTON

<td width="53%">
             
                <?
              $colname_Recordset4 = "1";
if (isset($_GET['id2'])) {
  $colname_Recordset4 = (get_magic_quotes_gpc()) ? $_GET['id2'] : addslashes($_GET['id2']);
}

$id3 = $_GET['id'];

mysql_select_db($database_mine, $mine);
//$query_Recordset3 = sprintf("SELECT * FROM team WHERE team_type = %s", $colname_Recordset3);
$query_Recordset4 = sprintf("SELECT team_id, team_type FROM team WHERE team_type = %s AND team_id < $id3 LIMIT 1", $colname_Recordset4);
$Recordset4 = mysql_query($query_Recordset4, $mine) or die(mysql_error());
$row_Recordset4 = mysql_fetch_assoc($Recordset4);
$totalRows_Recordset4 = mysql_num_rows($Recordset4);

             
              ?>
             
              <a href="team_details.php?id=<?php echo $row_Recordset4['team_id']; ?>&id2=<?php echo $row_Recordset4['team_type']; ?>">Back</a>
             
              </td>

NEXT BUTTON


<td width="14%">
              <?
              $colname_Recordset3 = "1";
if (isset($_GET['id2'])) {
  $colname_Recordset3 = (get_magic_quotes_gpc()) ? $_GET['id2'] : addslashes($_GET['id2']);
}

$id3 = $_GET['id'];

mysql_select_db($database_mine, $mine);
//$query_Recordset3 = sprintf("SELECT * FROM team WHERE team_type = %s", $colname_Recordset3);
$query_Recordset3 = sprintf("SELECT team_id, team_type FROM team WHERE team_type = %s AND team_id > $id3 LIMIT 1", $colname_Recordset3);
$Recordset3 = mysql_query($query_Recordset3, $mine) or die(mysql_error());
$row_Recordset3 = mysql_fetch_assoc($Recordset3);
$totalRows_Recordset3 = mysql_num_rows($Recordset3);

             
              ?>
             
             
             
              <a href="team_details.php?id=<?php echo $row_Recordset3['team_id']; ?>&id2=<?php echo $row_Recordset3['team_type']; ?>">Next</a></td>
        </tr>
ASKER CERTIFIED SOLUTION
Avatar of aminerd
aminerd

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial