Jump to content

[SOLVED] Pagination Help


RealDrift

Recommended Posts

Try this

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

$limitvalue = $page * $limit - ($limit);
$limitrange = $limitvalue+$limit;
$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT $limitvalue, $limitrange";
echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

  • Replies 126
  • Created
  • Last Reply

Good job POCO, you trully are a Guru.

 

phpSensei - LMAO, far from it. It's just a lot easier when you test the code out yourself rather than guessing over and over. With pagination it's almost impossible to help with unless you take it into your own hands, hah.

Poco you are a Guru, no questions

 

and yes the results are as follows:

 

page 1 = 10 rows

page 2 = 11 rows

page 3 = 1 row which was the last row on page 2

 

maybe this has something to do with remainders when ya divide total rows by the amount of rows you want to see?

try

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;


$query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT '$limit'";
echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

 

 

edit: O wait, nvm this then.

Why don't you just use

 

 $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT '$limit' "; 

 

Showing 10 records at a time per page.. Ama right?

 

Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near ''10'' at line 1

Why don't you just use

 

 $query  = "SELECT * FROM goldies WHERE Sender='$username' LIMIT '$limit' "; 

 

Showing 10 records at a time per page.. Ama right?

 

That will only show the first 10 records...that works for the first page, but we are trying to figure out how to make it work for all the others as well.

AH! I am such an idiot. The reason I was getting duplicates is because I had duplicates in the DB! So I've been doing it right for a while now and didn't even know it, haha.

 

So here is the FINAL working code (hopefully)

 

<?php

$username       = $_SESSION['username'];
$limit          = 10;
$query_count    = "SELECT count(*) FROM goldies WHERE Sender='$username'";
$result_count   = mysql_query($query_count) or die("Error: " . mysql_error());
$totalrows      = mysql_result($result_count, 0, 0);
$numofpages     = ceil($totalrows/$limit);
$rowsperpage = ceil($totalrows/$numofpages);

if (isset($_GET['page'])) $page = $_GET['page'];
else $page = 1;

$offset = ($page - 1) * $rowsperpage;
$query  = "SELECT * FROM i_horses LIMIT $offset,10";

echo  $query .'<br>';
$result = mysql_query($query) or die("Error: " . mysql_error());

//Exit if no records to display
if (mysql_num_rows($result) == 0) {
    exit("Nothing to Display!");
}

//Create the table of output data
echo "<table>\n";
while ($row = mysql_fetch_array($result)) {
    //Alternate the row background color
    $bgcolor = ($bgcolor == "#737373")?'#737373':'#737373';
    //Display the row of data
    echo "<tr bgcolor=\"$bgcolor\">\n";
    echo "  <td>".$row["Receiver"]."</td>\n";
    echo "  <td>".$row["Amount"]."</td>\n";
    echo "  <td>".$row["Date"]."</td>\n";
    echo "</tr>";
}
echo "</table>\n";

//Enable the Prev link if not first page
if ($page > 1) {
    echo("<a href=\"creditshistory.php?page=".($page-1)."\">PREV</a> ");
} else {
    echo("PREV ");
}

//Create links for each page in report
for ($i=1; $i<=$numofpages; $i++) {
    if ($i == $page) {
        echo "$i ";
    } else {
        echo "<a href=\"creditshistory.php?page=$i\">$i</a> ";
    }
}

//Enable the Next link if not last page
if ($page < $numofpages) {
    echo("<a href=\"creditshistory.php?page=".($page+1)."\">NEXT</a>");
} else {
    echo("NEXT");
} 

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.