SOLVED: Help me with Incrementing for pagination please!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
tabitha
Forum Newbie
Posts: 6
Joined: Thu Aug 27, 2009 1:09 am

SOLVED: Help me with Incrementing for pagination please!

Post by tabitha »

I'm totally lost, I've been looking at this for hours and am certain that I must be some sort of *** because I can't find the problem. The pagination links display, urls behave correctly ?page=1 ?page=2 but my results aren't actually incrementing. I just see the first 10 results on every page.

here's the code where I think the problem is:

Code: Select all

$comp = $_GET['comp'];
if(!$comp){
    if(!$page) $page = 1;
    $npage = $page - 1;
 
    if($c){
        $addsql = " AND `cat` = '". $c ."'";
        $addhtml = "&c=". $c;
    }
 
 
    $limit1 = $npage * 10;
    $limit2 = $npage + 10;
 
    $q = "SELECT * FROM `comps` WHERE `accepted` = 'Yes'". $addsql ." ORDER BY `time` DESC LIMIT ". $limit1 .", ". $limit2;
And then do some display stuff

finally, bottom of page where links live:

Code: Select all

   $q = "SELECT COUNT(*) FROM `comps` WHERE `accepted` = 'Yes'". $addsql;
    $r = mysql_query($q);
    $num = mysql_fetch_array($r);
    $nbrPages = ceil($num[0] / 10);
 
    $i = 1;
 
    $maincontent .= "<br />";
 
    // Pagination links.
    $nbrPages = $nbrPages;
    $thisPage = "";
    include("pagination.php");
    $maincontent .= $paginationLinks;
Pretty sure my problem is somewhere in the first query and has something to do with my limits. Please help me understand what I'm doing wrong.
Last edited by tabitha on Thu Aug 27, 2009 9:47 am, edited 1 time in total.
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Re: Help me with Incrementing for pagination please!

Post by JAB Creations »

if(!$comp) should be...

Code: Select all

if(!isset($comp))
This is PHP, not JavaScript. :wink:

I've subscribed, if you still are having problems let me know. :)
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Help me with Incrementing for pagination please!

Post by susrisha »

Code: Select all

 
$comp = $_GET['comp'];
if(!$comp){
    if(!$page) $page = 1; 
    $npage = $page - 1;
//i dont see any assignment for $page earlier in the code which means $page always gets the value 1
 
    if($c){
        $addsql = " AND `cat` = '". $c ."'";
        $addhtml = "&c=". $c;
    }
 
 
    $limit1 = $npage * 10;
    $limit2 = $npage + 10;
 
    $q = "SELECT * FROM `comps` WHERE `accepted` = 'Yes'". $addsql ." ORDER BY `time` DESC LIMIT ". $limit1 .", ". $limit2;
//try to print $q each to see whats the exact query you are trying to run on each page.
// Moreover LIMIT statement has to be coded like this
// LIMIT 10,5 implies records from the row 10 and further 5 records..
//so please reconsider the query statement
 
tabitha
Forum Newbie
Posts: 6
Joined: Thu Aug 27, 2009 1:09 am

Re: Help me with Incrementing for pagination please!

Post by tabitha »

Thank you so far for the response, I think maybe I needed to post more code for you to see what's going on here. I think the problem is in the query statement or my limits. Here's the full code:

Code: Select all

<?php
 
include('header.php');
/* Let's submit something */
if($_POST['submit']){
    if(!isset($_SESSION['user']['username'])) $username = "Anonymous";
    else $username = $_SESSION['user']['username'];
    @$query = mysql_query("INSERT INTO `comps`(time,message,accepted,votes1,votes2,via,user,cat,numcoms,subcat) VALUES ('". time() ."', '". clean($_POST['comp']) ."', 'No', 0, 0, 'internet', '". $username ."', '". clean($_POST['cat']) ."', '0', '0')") or die(mysql_error());
    
    echo "<strong>Your submission has been received!</strong>
    <script type=\"text/javascript\"><!--
setTimeout('Redirect()',1000);
function Redirect()
{
    opener.location.href='./';
    window.close();
}
// --></script>";
} else {
 
 
}
 
$cnt = '<p style="font-size: 18px; font-weight: bold;">Share your terrible or awesome predicament&#46;</p>  
    <p> Concept&#58;  Your submission should always end with <b>"NHNL&#63;"</b> (meaning "New high or new low&#63;")  </p> <p>Please avoid using texting language and read your post over before submitting&#46;</p>
Thanks for participating&#33; </p></br></br><form action="submitCompliment.php" class="submit_form" method="POST" target="sendCommentIFrame">
    <table id="submissionform">
    <tr><td class="input" colspan=5>
    <textarea onfocus="javascript&#058;document.getElementById(\'commbox\').value=\'\';" name="comp" rows="13" height="75" cols="20" id="commbox">'. $names[4] .'</textarea>
    </td></tr>
    <tr><td><p>Want your own Name&#63; <span style="color : #028013"><a href="http://www.newhighnewlow.com/register.php">Getting one</a></span> takes 2 seconds.</p>
    <select name="cat">';
 
while($category = current($catlist)){
    $cnt .= '<option value="'. key($catlist) .'">'. current($catlist) .'</option>';
    next($catlist);
}
 
$cnt .= '
</select></td></tr> <tr><td><input type="submit" name="Ok" value="Submit!" class="button_login2"/>
</td></tr>
<tr><td>
<div id="loadingsubmit_article" style="display:none;"><img src="images/loader.gif"/></div>
<div id="resultsubmit_article" style="display:none;"></div>
</td></tr>
</table>
       </form><iframe name="sendCommentIFrame" style="display:none"></iframe>';
 
$maincontent = "<h1><a href=\"javascript&#058;submitToggle();\" class=\"submit\" style=\"font-weight: bold;\">" . $submitpop . "</a></h1><div style=\"display: none;\" id=\"submit\" class=\"post\"><div id=\"result2\" class=\"result\">" . $cnt . "</div></div>";
/* To institute TOP BANNER ADS:  Place this code-->  <style=\"margin-left:40px;\"><script type=\"text/javascript\"><!--
google_ad_client = \"pub-2934960017790058\";
google_ad_slot = \"2975484663\";
google_ad_width = 468;
google_ad_height = 60;
//-->
</script>
<script type=\"text/javascript\"
src=\"http://pagead2.googlesyndication.com/pagead/show_ads.js\">
</script></style>
<--End of code  
Between the > and the " at the end of the previous line. Repeat for each category page and Random page*/
/* submissions done!  Let's move on to displaying our content */
$maincontent .= "<div id=\"mainresultlist\">\n";
$comp = $_GET['comp'];
if(!isset($comp)){
    if(!$page) $page = 1;
    $npage = $page - 1;
 
    if($c){
        $addsql = " AND `cat` = '". $c ."'";
        $addhtml = "&c=". $c;
    }
 
 
    $limit1 = $npage * 10;
    $limit2 = $npage + 10;
 
    $q = "SELECT * FROM `comps` WHERE `accepted` = 'Yes'". $addsql ." ORDER BY `time` DESC LIMIT ". $limit1 .", ". $limit2;
print $q;
    $r = mysql_query($q);
 
    $voting_array = mysql_fetch_array(mysql_query("SELECT * FROM `votes` WHERE `ip`='$ip'"));
 
    $vote_explode = explode(',', $voting_array['comp_id']);
 
    foreach($vote_explode as $vote_ex){
 
        $vote_ex = explode(":", $vote_ex);
 
        $compnum = $vote_ex[0];
        $compvote = $vote_ex[1];
        $compvotearray[$compnum] = $compvote;
    }
 
    if(mysql_num_rows($r) > 0){
        while($row = mysql_fetch_array($r, MYSQL_ASSOC)){
            // count the comments
            $commentsCountQuery = "select * from comments where comp_id=" . $row['id'];
            $commentsCountResult = mysql_query($commentsCountQuery);
            $commentsCount = mysql_num_rows($commentsCountResult);
 
            $maincontent .= "<br />\n<div id=\"result". $row['id'] ."\" class=\"result\">\n";
            $maincontent .= stripslashes($row['message']) ."\n<br /><br />\n";
            $maincontent .= '<span class="sentBy"><span class="lside"><a href="comment-'. $row['id'] .'" class="commlink">read comments ('. $commentsCount .')</a></span>';
            if(!$compvotearray[$row['id']]){
 
                $maincontent .= '<span id="vote_buttons'. $row['id'] .'" class="vote_buttons">
                <span id="a1votes_count'. $row['id'] .'">
                <a id=":'. $row['id'] .':1:'. $row['votes1'] .':'. $row['votes2'] .':" class="vote_up" title="'. $phrase1 .':'. $phrase2 .'" href="javascript&#058;;">
                '. $phrase1 .' ('. $row['votes1'] .')</a></span>';
                $maincontent .= '   -   <span id="a2votes_count'. $row['id'] .'">
                <a id=":'. $row['id'] .':2:'. $row['votes1'] .':'. $row['votes2'] .':" class="vote_down" title="'. $phrase1 .':'. $phrase2 .'" href="javascript&#058;;">
                '. $phrase2 .' ('. $row['votes2'] .')</a>
                </span></span><br />';
            } else {
 
                $maincontent .= '<span id="vote_buttons'. $row['id'] .'" class="vote_buttons voted">
                <span id="a1votes_count'. $row['id'] .'">
                '. $phrase1 .' ('. $row['votes1'] .')</span>';
                $maincontent .= '   -   <span id="a2votes_count'. $row['id'] .'" class="vote_buttons voted">
                '. $phrase2 .' ('. $row['votes2'] .')
                </span></span><br />';
 
            }
            $maincontent .= '<span class="mesdetail">'. date('F j, Y \a\t g:i a ', $row['time']) .'by <a href="' . $row['user'] . '_' . $row['user'] .'-'. $row['user'] . '">'. $row['user'] .'</a>
        </span>';
            $maincontent .= '<span class="mesdetail"> in - <a href="category-'. $row['cat'] .'_'. $catlist[$row['cat']] .'">'. $catlist[$row['cat']] .'</a></span>';
            $maincontent .= "</span>";
            if($sharelinks == 'yes') $maincontent .= '<span id="addslider'. $row['id'] .'">
<a class="addslide" onclick="Slide(\'result'. $row['id'] .'\',\'down\');" href="javascript&#058;;" style="">[+]</a>
</span>
<div id="sharelinks'. $row['id'] .'" class="sharelinks">'. $sharelinkscode .'</div>';
            $maincontent .= "</div>";
        }
    } else {
        $maincontent .= "<strong>There are no results in this category.</strong>";
    }
 
    $q = "SELECT COUNT(*) FROM `comps` WHERE `accepted` = 'Yes'". $addsql;
    $r = mysql_query($q);
    $num = mysql_fetch_array($r);
    $nbrPages = ceil($num[0] / 10);
 
    $i = 1;
 
    $maincontent .= "<br />";
 
    // Pagination links.
    $nbrPages = $nbrPages;
    $thisPage = "";
    include("pagination.php");
    $maincontent .= $paginationLinks;
//    while($i <= $nbrPages){
//        if($i == 1) $maincontent .= "Pages: ";
//        if($i != $page) $maincontent .= "<a href=\"?page=". $i . $addhtml ."\">". $i ."</a>\n ";
//        else $maincontent .= $i ." ";
//        $i++;
//    }
} elseif ($comp){
    include('comment.php');
}
 
$maincontent .= "\n</div>";
$layout = str_replace("[[main-content]]", $maincontent, $layout);
echo $layout;
 
?>
JAB Creations: I did change as suggested. No change in behavior, but maybe with more code it will be easier to see my error.

susrisha: I did print, and I get the same result: SELECT * FROM `comps` WHERE `accepted` = 'Yes' ORDER BY `time` DESC LIMIT 0, 10 from any page. I'm just not sure where I'm failing here.

Thanks again for the responses and your continued help.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: Help me with Incrementing for pagination please!

Post by susrisha »

Code: Select all

 
 if(!$page) $page = 1;
    $npage = $page - 1;
 
This is where the problem is.,.I dont see the variable assignment anywhere earlier in the code which means $page will always get the value of 1 which means the same query gets executed. I think the assignment should be something like this

Code: Select all

 
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
else
{
$page=1;
}
$npage=$page-1;
 
Not sure if this will be the exact code needed unless i get to see what will be the pagination links that you will provide.
tabitha
Forum Newbie
Posts: 6
Joined: Thu Aug 27, 2009 1:09 am

Re: Help me with Incrementing for pagination please!

Post by tabitha »

Thank you thank you thank you! It works wonderfully now! I really appreciate your help, and will remember this for future reference.
User avatar
susrisha
Forum Contributor
Posts: 439
Joined: Thu Aug 07, 2008 11:43 pm
Location: Hyderabad India

Re: SOLVED: Help me with Incrementing for pagination please!

Post by susrisha »

would you mind posting where the code went wrong??
tabitha
Forum Newbie
Posts: 6
Joined: Thu Aug 27, 2009 1:09 am

Re: SOLVED: Help me with Incrementing for pagination please!

Post by tabitha »

susrisha wrote:would you mind posting where the code went wrong??

It was the variable assignment problem you noted. After making these changes as suggested:

Code: Select all

 
if(isset($_GET['page']))
{
$page=$_GET['page'];
}
else
{
$page=1;
}
$npage=$page-1;
 
It functions properly. Thank you again, I will know next time to check that. I'm still learning, and your knowledge has been invaluable.
Post Reply