1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

PHP Paging Problem

Discussion in 'PHP' started by scoopy82, Oct 20, 2008.

  1. #1
    I have had this code on a site... and it seems to work... all except it is stuck on page 1. It will only display the first page of results and does not display the previous or next page. What could be wrong ?

    Here it is if anyone should be kind enough to take a look:
    <?
    
    // ==============
    // Configuration
    // ==============
    $perpage = "100"; // How many results to show per page
    $pageshow = "20"; // How many pages you want to show in the direction bar
    
    $records = mysql_fetch_array(mysql_query("select count(*) as results from articles"));
    $page_num = ceil($records[results] / $perpage);
    $page = ($page) ? $page : 1;
    $vstart = $perpage * ($page-1);
    $page_start = floor(($page-1)/ $pageshow ) * $pageshow ;
    $page_end = $page_start + $pageshow;
    for ($p=$page_start+1 ; ($p <= $page_end) && ($p <= $page_num)  ; $p++ )
    {
    if ($page == $p) {
    $direct_bar .= "<b>$p</b> ";
    } else {
    $direct_bar .= "<a href='/test/allarticles/?page=$p'>$p</a> ";
    }
    }
    if ($records[results] > $vstart+$perpage ) {
    $next_p=$page+1;
    $next_list = "<a href='/test/allarticles/?page=$next_p'>Next >></a> \n";
    }
    if ($page>1) {
    $prev_p=$page-1;
    $prev_list="<a href='/test/allarticles/?page=$prev_p'><< Prev</a>\n";
    }
    
    
    // Below will show the page numbers
    // Print "Pages: $prev_list : $direct_bar : $next_list<br /><br />";
    
    $query = "SELECT ID, Category, Author, Email, Title, ArticleBody, ArticleBlurb, DateSub, DateAct, URL, Active FROM articles ORDER BY DateSub DESC limit $vstart,$perpage";
    $query_result = mysql_query($query) or die("<b>MySQL Error:</b> " . mysql_error());
    											  
    				//while
    						while ($GetPosts = mysql_fetch_array($query_result))
    													{
    						$ArtID = $GetPosts['ID'];
    						$ArtCategory = $GetPosts['Category'];
    						$ArtAuthor = $GetPosts['Author'];
    						$ArtEmail = $GetPosts['Email'];
    						$ArtTitle = $GetPosts['Title'];
    						$ArtURL = $GetPosts['ID'];
    						
    						$ArtTitle2 = str_replace(" ", "_", $ArtTitle);
    						$ArtTitle2 = strtolower($ArtTitle2);
    						
    						$categorysql = mysql_query ("SELECT ID, Name FROM category WHERE ID = $ArtCategory");
    			
    						$CatName = mysql_result($categorysql, 0, "Name");
    
    				//Process CatName
    						$CatName2 = str_replace(" ", "_", $CatName);
    						$CatName2 = strtolower($CatName2);
    						$CatName2 = str_replace("'","",$CatName2);
    							
    				//Print the list
    						Print "<li><a href='/test/article$ArtURL' title='$ArtTitle'>$ArtTitle</a></li></b>";
    													}
    
    						Print "<br /><br /><center>Pages $prev_list $direct_bar $next_list</center><br />";
    
      ?>
    PHP:
     
    scoopy82, Oct 20, 2008 IP
  2. zerxer

    zerxer Peon

    Messages:
    368
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Well, where exactly do you set what $page is? I see you have $page = ($page) ? $page : 1; but where does it set it initially? It's always going to set it to 1 if no other value was ever set. I assume you'd want something like $page = $_GET['page']; above that at some point or whatever your URL value is (or maybe it's a POST value?).
     
    zerxer, Oct 20, 2008 IP
    scoopy82 likes this.
  3. scoopy82

    scoopy82 Active Member

    Messages:
    838
    Likes Received:
    45
    Best Answers:
    0
    Trophy Points:
    70
    #3
    Thank You... That was it:
    $page = $_GET['page'];
    PHP:
    I added the above code and now I have more than 1 page :)

    Now to (*maybe*) add a last page link. I am guessing I will have to add another variable ($page_end ?) and define that somehow... then I could just add it to the last print statement. Am I on the right track here ?
     
    scoopy82, Oct 20, 2008 IP
  4. Kyosys

    Kyosys Peon

    Messages:
    226
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    $page_num should be the last page

    it's the number of pages
     
    Kyosys, Oct 21, 2008 IP
  5. coolbudy

    coolbudy Guest

    Messages:
    30
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #5
    for best PHP paging visit java2s.com
     
    coolbudy, Oct 21, 2008 IP
    scoopy82 likes this.