Jump to content

Archived

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

Ninjakreborn

2 problems left with custom pagination code

Recommended Posts

After 3-4 cups of coffee, 2 headaches, and a major brain cramp, I finally have gotten a skeleton, workable system for pagination, and a thourough understanding of the basic concept behind it, so I can replicate it later, or customize it more when I have all this set in.  For now I am against a wall, Ihave encountered 2 things with my script, and no matter how hard I think at this point, I cannot figure these 2 things out.
THe system it self works perfectly, it is set up right, and for it to keep working, I can just change the number to 50, and it will use 50 results, as well as I can put it on every case switch and it will all work properly, the thing here is though, it works.  However there are 2 issues I can't figure out.
1. If they try to go previous, for instance if they go next 1 time, then previous 2 times, it passes there current script, adn starts showing a blank page, it keeps lessening that number, and never stops, so each time you hit previous, it goes to another blank page. 
2. The same for next, you can keep going to next for ever.  How can I factor in when to make it now show next, and not show previous, I don't want those showing up if there is nothing there, any advice on how I can fix this.
[code]<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include './includes/meta.inc.php'; // includes meta tags, stylesheet, and javascript inclusion
?>
</head>
<body>
<div id="wrap">
<?php
include './includes/header.inc.php'; // inserts header, logo, and everything up there
?>
    <br style="clear: both;" />

<?php
include './includes/leftnav.inc.php'; // inserts left navigation
?>

    <div id="content">

        <div class="overall">
<h3>Postings</h3>
<?php
// connect to database
mysql_connect("#######", "#####", "######");
mysql_select_db("joyel");
// get together the critical variables, and clean them up.
$postset = mysql_real_escape_string($_GET['ps']);
$school = mysql_real_escape_string($_GET['s']);
$category = mysql_real_escape_string($_GET['c']);
$subcategory = mysql_real_escape_string($_GET['sc']);
$limit = 2;
$limit = mysql_real_escape_string($limit);
if ($_GET['rownumberprev']) {
$rownumber = mysql_real_escape_string($_GET['rownumberprev']);
}elseif ($_GET['rownumbernext']) {
$rownumber = mysql_real_escape_string($_GET['rownumbernext']);
}else {
$rownumber = 0;
}

// go through databases.  Easier to keep up with, let's start with database number 1
switch ($postset) {
// Work with database postset1
case "postset1":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
}
echo "<hr />";
echo "<br />";
$rownumberprev = $rownumber - 2;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
$rownumbernext = $rownumber + 2;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";

break;
// work with database postset2
case "postset2":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset3
case "postset3":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset4
case "postset4":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset5
case "postset5":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset6
case "postset6":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset7
case "postset7":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
// work with database postset8
case "postset8":
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$query = mysql_query($select);
while ($row = mysql_fetch_array($query)) {

}
break;
}
?>

        </div>

    </div>
<?php
include './includes/banner.inc.php'; // includes the banner, randomly selected from a database
?>

<?php
include './includes/footer.inc.php'; // includes the page footer, bottom navigation copyright
?>
</div>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites
By the way I asked this after awhile of fighting with the different greater than less than, and equal to or less than, and equal to or less that, trying to use a variety of combinations of numrows, and record number, to try and make previous disappear, when there are none before, and vice versa, but nothing worked.

Share this post


Link to post
Share on other sites
why are you displaying all the database information?

come on buisiness man your more talented then that.

Share this post


Link to post
Share on other sites
shit, that never happened before.  I have been dealing with a lot later, 2 days ago, I spent 11 hours of doing the hardest programming I ever had before straight.  Then I spent 4 hours the next day(yesterday, tuesday), finishing that up, then about 8 hours of even harder programming, followed up by pagination today(which was the hardest), so because of that, I guess I am getting a little burnt out.  Now I am stuck on this and it's not working, I changed that by the way, but now I looked at the variables, when the first page comes up, $rownumber is first set to 0
when I go
if ($rownumber == 0) {
// echo the previous link
}
then it still displays the previous link??

Share this post


Link to post
Share on other sites
[code]
<?php
if ($rownumber != 0) {
   $rownumberprev = $rownumber - 2;
   echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
?>
[/code]
That seemed to have fixed up the previous link properly, it seems to be working, but nothign seems to be working for the next link.

Share this post


Link to post
Share on other sites
The previous link was showing no matter what, and even at the beginning, if they hit it, it changed to -2, so it kept going, I fixed that with what I showed earlier, now for some reason the next link's not working.  I need it to disappear, I even created a test query to get the total number of rows from the database, and still can't figure out anything to make the next link disappear, I just want to make it to where when it get's to the final page of results, then it makes the next link disappear.  But nothing at this point seems to be working.

Share this post


Link to post
Share on other sites
OK, I have to ask, why are you going forward 2 and back 2 each time, why not just 1?

Regards
Huggie

P.S I'm looking at the code in the mean time to see what's causing the issue ;)

Share this post


Link to post
Share on other sites
I have it set going back 2 and forward 2
I am increasing that to 50, that is how many records it's going to jump

For instance if my limit is 50, the starting row is 0, the first record set is 0-50, after that 50-100 then 100-150 well you get the point.  But you see what I mean.  I need to get the next link changed, hten I am changing all of those to 50.

Share this post


Link to post
Share on other sites
Fair enough, so when you hover the mouse over the next link, what's the URL it's pointing to, does it actually look correct.

Regards
Huggie

Share this post


Link to post
Share on other sites
Yes that all works, even the previous link disappears when it's suppose to.  They both work perfectly as far as functionality, I just need it to disappear when it reaches the end.

Share this post


Link to post
Share on other sites
Try...

[code]<?php
if ($rownumber <= $num_rows -2){
  $rownumbernext = $rownumber + 2;
  echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc={$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
?>[/code]

If this doesn't work, do you have it working somewhere I can see it?

Regards
Huggie

Share this post


Link to post
Share on other sites
Someone told me something vaguely similar, as a favor can you explani that to me, so I understand the point.  I want advice from everyone here, this is NOT asking for help this time, this is asking for a script analysis I guess you could call it.  I have finished the whole thing, it has previous and next buttons, I have to go back and do my queries( like what they are displaying) because the data will be different for each one.  I just don't want to get to the point where I go ahead and say it's finished, and then 2 months from now they are complaining about something to do with the site, and it ends up being something with teh  pagination system that didn't go right.  If you can just look over everything, tell me if everything is right, if you don't see any problems then I am declaring it done.  It works right for both previous and next pages, and everything seems to work properly.??  Is there anything i Missed before I declare it done.

[code]<?php
session_start();
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<?php
include './includes/meta.inc.php'; // includes meta tags, stylesheet, and javascript inclusion
?>
</head>
<body>
<div id="wrap">
<?php
include './includes/header.inc.php'; // inserts header, logo, and everything up there
?>
    <br style="clear: both;" />

<?php
include './includes/leftnav.inc.php'; // inserts left navigation
?>

    <div id="content">

        <div class="overall">
<h3>Postings</h3>
<?php
// connect to database
mysql_connect("mysql185.secureserver.net", "####", "#####");
mysql_select_db("joyel");
// get together the critical variables, and clean them up.
$postset = mysql_real_escape_string($_GET['ps']);
$school = mysql_real_escape_string($_GET['s']);
$category = mysql_real_escape_string($_GET['c']);
$subcategory = mysql_real_escape_string($_GET['sc']);
$limit = 50;
$limit = mysql_real_escape_string($limit);
if ($_GET['rownumberprev']) {
$rownumber = mysql_real_escape_string($_GET['rownumberprev']);
}elseif ($_GET['rownumbernext']) {
$rownumber = mysql_real_escape_string($_GET['rownumbernext']);
}else {
$rownumber = 0;
}

// go through databases.  Easier to keep up with, let's start with database number 1
switch ($postset) {
// Work with database postset1
case "postset1":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset2
case "postset2":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset3
case "postset3":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset4
case "postset4":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset5
case "postset5":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset6
case "postset6":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset7
case "postset7":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
// work with database postset8
case "postset8":
// test query for total row numbers
$testselect = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school';";
$testquery = mysql_query($testselect);
$total_rows = mysql_num_rows($testquery);
// query db
$select = "SELECT * FROM $postset WHERE categoryname = '$category' AND
subcategoryname = '$subcategory' AND schoolname = '$school' ORDER BY dateentered
LIMIT $rownumber, $limit;";
$query = mysql_query($select);
$num_rows = mysql_num_rows($query);
echo "<hr />";
while ($row = mysql_fetch_array($query)) {
echo "<br />";
echo "<a href=\"view.php\" title\"View Post\">{$row[itemtitle]}-{$row[price]}-
{$row[dateentered]}</a>";
echo "<br />";
$temp = true;
}
if ($temp != true) {
echo "We apologize but there were no results";
}
echo "<hr />";
if ($rownumber != 0) {
$rownumberprev = $rownumber - 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumberprev={$rownumberprev}\">Previous Page</a>";
echo "<br />";
}
if ($rownumber <= ($total_rows - 50)) {
$rownumbernext = $rownumber + 50;
echo "<a href=\"display.php?ps={$postset}&s={$school}&c={$category}&sc=
{$subcategory}&rownumbernext={$rownumbernext}\">Next Page</a>";
}
break;
}
?>

        </div>

    </div>
<?php
include './includes/banner.inc.php'; // includes the banner, randomly selected from a database
?>

<?php
include './includes/footer.inc.php'; // includes the page footer, bottom navigation copyright
?>
</div>
</body>
</html>[/code]

Share this post


Link to post
Share on other sites
[code=php:0]$limit = 50;
$limit = mysql_real_escape_string($limit);[/code]


There's no need to escape a hardcoded value like $limit if you know it contains no special characters.

Regards
Huggie

Share this post


Link to post
Share on other sites
I know, that is something I always do.  I do that for get, post, session, and sometimes even variables I create, to avoid any possibilty.  There may not be a possibility, but incase if there is, then it's there, incase

Share this post


Link to post
Share on other sites

×

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.