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");
} 

?>

Link to comment
Share on other sites

  • Replies 126
  • Created
  • Last Reply

Top Posters In This Topic

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?

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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");
} 

?>

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.