Jump to content

Next || Previous Buttons


Mundo

Recommended Posts

Basically, I've been working from this tutorial: http://www.spoono.com/php/tutorials/tutorial.php?id=25.

 

There are a few obvious errors but it seems easy enough to implement. However, I can just not seem to get it to work.

My code:

 

index.php

<?
include "tpl/header.tpl";
include "tpl/navigation.tpl";

mysql_connect("localhost","root","");
mysql_select_db("ncfc");

if(!$rowstart) $rowstart=0;
$query = sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5");
$result = mysql_query($query);
$result2 = mysql_query("SELECT * FROM news");
$numrows = mysql_num_rows($result2);

if (!$result) {
$message  = 'Invalid query: ' . mysql_error() . "\n";
$message .= 'Whole query: ' . $query;
die($message);
}
while ($row = mysql_fetch_assoc($result)) {
include "tpl/news.tpl";
}


/* IGNORE THIS FOR NOW
if ($rowstart > $numrows) 
{
   echo "<a href=\"<? $php_self; ?>?rowstart=<? echo $rowstart-5; ?>\">Previous Page</a>";
}
*/

if($rowstart+5 < $numrows) {
echo $rowstart;
$rowstart =+ 5;
echo $rowstart;
include "tpl/next.tpl";
}


include "tpl/footer.tpl";

mysql_free_result($result);
mysql_close();
?>

 

next.tpl

<a href="index.php?rowstart=<? echo "$rowstart"; ?>">Next --></A>

 

Can you see what I've done wrong?

 

 

Link to comment
Share on other sites


let me share you my super page by page script ;-)

 

<?php

$rowsPerPage=10;
if ($_GET

==""){$pageNum = 1;}else{$pageNum = $_GET['page'];}

$offset = ($pageNum - 1) * $rowsPerPage;
$result  = mysql_query("SELECT COUNT(id) AS numrows FROM vip $limit_status") or die (mysql_error());
$row     = @mysql_fetch_array($result, MYSQL_ASSOC);
$numrows = $row['numrows'];

$re_page = $pageNum*$rowsPerPage;
$ree_page = $re_page-$rowsPerPage+1;

// Table Start
print ("");

$result = mysql_query("SELECT * FROM vip $where") or die (mysql_error());
while($vip = @mysql_fetch_array($result))
{

// Table Middle

}
// Table end
print ("");

$dbh2=mysql_connect("localhost", "ffrteam_vip", "g22ppl22") or die ('1 Erreur: '.mysql_error());
mysql_select_db("ffrteam_joom1");

$maxPage = ceil($numrows/$rowsPerPage);

$self = "index.php?option=com_interractif";
$nav = '';
for($page = 1; $page <= $maxPage; $page++)
{
    if ($page == $pageNum)
    {
        $nav .= " $page ";
    }
    else
    {
        $nav .= " <a href=\"$self&page=$page\">$page</a> ";
    }
}

if ($pageNum > 1)
{
    $page = $pageNum - 1;
    $prev = " <a href=\"$self&page=$page\">[back]</a> ";

    $first = " <a href=\"$self&page=1\">[First page]</a> ";
}
else
{
    $prev  = ' ';
    $first = ' ';
}

if ($pageNum < $maxPage)
{
    $page = $pageNum + 1;
    $next = " <a href=\"$self&page=$page\">[Next]</a> ";

    $last = " <a href=\"$self&page=$maxPage\">[Last page]</a> ";
}
else
{
    $next = ' ';
    $last = ' ';
}

echo "<p align='center'>($ree_page/$re_page from $numrows)  Page:<b>$first $prev $nav $next $last</b></p>";

?>

Link to comment
Share on other sites

Thanks for the speedy reply but I'd rather fix mine than use yours! I've worked on it a bit more and I've pretty much got it sorted...

 

<?
include "tpl/header.tpl";
include "tpl/navigation.tpl";

mysql_connect("localhost","root","");
mysql_select_db("ncfc");

if(!$rowstart) $rowstart=0;
$rowstart = $_GET["rowstart"];
$result = mysql_query(sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5"));
$numrows = mysql_num_rows(mysql_query("SELECT * FROM news"));
echo $numrows;

while ($row = mysql_fetch_assoc($result)) {
include "tpl/news.tpl";
}

if($rowstart+5 < $numrows) {
include "tpl/more.tpl";
}

include "tpl/footer.tpl";

mysql_free_result($result);
mysql_close();
?>

 

The only problem is when the page is viewed from "index.php" then I get error messages, I have to view the page from "index.php?rowset=0"...

 

It's because of

$rowstart = $_GET["rowstart"];

Is there a way I can do the following:

 

IF $_GET["rowstart"] exists

THEN $rowstart = $_GET["rowstart"]

ELSE $rowstart = 0;

 

I only know how to test if something doesn't exist... (if(!$rowstart))

Link to comment
Share on other sites

Yeah like this

 

<?
include "tpl/header.tpl";
include "tpl/navigation.tpl";

mysql_connect("localhost","root","");
mysql_select_db("ncfc");

if ($_GET["rowstart"]){$rowstart=$_GET["rowstart"];}else{$rowstart="0";}

$result = mysql_query(sprintf("SELECT * FROM news ORDER BY `news_ID` DESC LIMIT $rowstart, 5"));
$numrows = mysql_num_rows(mysql_query("SELECT * FROM news"));
echo $numrows;

while ($row = mysql_fetch_assoc($result)) {
include "tpl/news.tpl";
}

if($rowstart+5 < $numrows) {
include "tpl/more.tpl";
}

include "tpl/footer.tpl";

mysql_free_result($result);
mysql_close();
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.