Solved

creating next and back button

Posted on 2006-06-12
4
234 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

809 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