Display rows in sequence then loop back to first row

I’m looking for a little direction… how best to solve this?

I am working on a website for a performing arts organization. All of the acts will be in a mysql database. I want to have a page that will display 1 act at a time with a “previous” and “next” button at the bottom of the page. When the user gets to the last performance (last row in the database), I would like the “next” button to return to the first row in the database and so on.

Thanks much!

Previous and next links see http://www.codewalkers.com in the tutorials/database section - “PHP - Previous and Next Links by Matt Wade”

As for the starting back at the top. Since you are displaying 1 thing at a time… you can do a mysql_num_rows on a full list of the data and if the displayed item is equal to to the number of rows have the next link go to the beginning ($start = 0).

I have checked out the tutorial on codewalkers and am running into a roadblock.

Here is my code…

$db = mysql_connect("localhost", "username", "password");
if (!$db) {
    echo( "<p>Unable to connect to the " .
"database server at this time.</p>" );
    exit();
  }
mysql_select_db("myDatabase",$db);
 if (! mysql_select_db("myDatabase") ) {
    echo( "<p>Unable to locate the" .
"database at this time.</p>" );
    exit();
  }

if(!isset($start)) $start = 0;

$query = mysql_query("SELECT * FROM test LIMIT " . $start . ", 10", $db);
$result = mysql_query($query);
$query = mysql_query("SELECT count(*) as count FROM test", $db); 
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$numrows = $row['count'];
if($start > 0) 
   echo "<a href="" . $PHP_SELF . "?start=" . ($start - 10) . 
        "">Previous</a><BR>n";
if($numrows > ($start + 10)) 
   echo "<a href="" . $PHP_SELF . "?start=" . ($start + 10) . 
        "">Next</a><BR>n";

I am getting an Parse error on line 24…

echo "<a href="" . $PHP_SELF . "?start=" . ($start - 10) .

Try changing $PHP_SELF to $_SERVER[‘PHP_SELF’]

changed to (both instances):

echo "<a href="" . $_SERVER['PHP_SELF'] . "?start=" . ($start - 10) .

stiil get the same Parse error

Why are you querying the results of your query?

$query = mysql_query("SELECT * FROM test LIMIT " . $start . ", 10", $db);
$result = mysql_query($query); 

That should be

$query = "SELECT * FROM test LIMIT " . $start . ", 10");
$result = mysql_query($query, $db); 

OR

$result = mysql_query("SELECT * FROM test LIMIT " . $start . ", 10", $db);

You did it both times in your code.

My apologies… I posted some bad code (i.e. code I have been mucking with).

This is the code that I tried first, which I got from the Codewalkers Tutorial…

if(!isset($start)) $start = 0;

$query = "SELECT * FROM test LIMIT " . $start . ", 10";
$result = mysql_query($query);
$query = "SELECT count(*) as count FROM test"; 
$result = mysql_query($query);
$row = mysql_fetch_array($result);
$numrows = $row['count'];
if($start > 0) 
   echo "<a href="" . $PHP_SELF . "?start=" . ($start - 10) . 
        "">Previous</a><BR>n";
if($numrows > ($start + 10)) 
   echo "<a href="" . $PHP_SELF . "?start=" . ($start + 10) . 
        "">Next</a><BR>n";

I have also tried…

$query = "SELECT * FROM test LIMIT " . $start . ", 10";
$result = mysql_query($query, $db);
$query = "SELECT count(*) as count FROM test"; 
$result = mysql_query($query, $db);

and…

$result = mysql_query("SELECT * FROM test LIMIT " . $start . ", 10", $db);
$result = mysql_query("SELECT count(*) as count FROM test", $db);

All of these have resulted in the Parse error.

you do realize that when you run the second query and put it into the same variable as the first query you are overwriting the results of the first (deleting them) so they are no longer available for use later.

Oh and where you you set $start at? Or is that a $_GET variable… I think it is. Is the echo all on 1 line or broken up like you show? If it is you might need brackets around it.

The first response to my topic referred me to a Codewalkers Tutorial. The code I posted is taken directly from that tutorial with only the name of the table changed. (At the end of the tutorial, the writer Matt Wade says, “Man is that simple stuff or what?”… well…)

I did put the echo line on one line and received the same error.

OK I admit I’m befuddled… I can’t find anything. What EXACTLY does the parse error say? (copy paste) and you are using $_SERVER[‘PHP_SELF’].

The error copied and pasted:

Parse error: parse error in /d3/home/clubs/carts/public_html/new/acts1.php on line 16

Line #16:

echo “<a href=”" . $_SERVER[‘PHP_SELF’] . “?start=” . ($start - 10) .

echo “<a href=”" . $_SERVER[‘PHP_SELF’] . “?start=” . ($start - 10) .

try

echo “<a href=”" . $_SERVER[‘PHP_SELF’] . “”?start=" . ($start - 10) .

first one has a double quotation mark that isn’t closed (href="")

same error.

D**n it we will beat this darn thing
echo “<a href=” . $_SERVER[‘PHP_SELF’] . “?start=” . ($start - 10) .

That did it! Thanks much! More questions to come (sorry).

Sponsor our Newsletter | Privacy Policy | Terms of Service