Solved

creating next and back button

Posted on 2006-06-12
4
236 Views
Last Modified: 2013-12-12
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;
0
Comment
Question by:jblayney
  • 2
  • 2
4 Comments
 
LVL 5

Expert Comment

by:aminerd
ID: 16888136
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)).
0
 
LVL 1

Author Comment

by:jblayney
ID: 16888396
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?
0
 
LVL 1

Author Comment

by:jblayney
ID: 16888515
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>
0
 
LVL 5

Accepted Solution

by:
aminerd earned 250 total points
ID: 16888739
"i notice something else, my back button always goes to first record.."

You're correct, sorry about that. Change to:

SELECT team_id, team_type FROM team WHERE team_type = %s AND team_id < $id3 ORDER BY team_id DESC LIMIT 1
0

Featured Post

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

740 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question