Solved

creating next and back button

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

What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

Join & Write a Comment

Generating table dynamically is the most common issue faced by php developers.... So it seems there is a need of an article that explains the basic concept of generating tables dynamically. It just requires a basic knowledge of html and little maths…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

760 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now