Jump to content


Photo

display a database table in pages


  • Please log in to reply
2 replies to this topic

#1 otuatail

otuatail
  • Members
  • PipPipPip
  • Advanced Member
  • 960 posts

Posted 28 March 2006 - 12:57 PM

I have a mysql database (4.0.25 I believe – streamline.net) I would like to show the first 10 or so with a

Previous 1 2 3 4 next

system underneath. I could count the records and work out how many pages, but I would not know the unique id as records could be removed and there would be gaps in the auto numbering. I could re-invent the wheel, but is there a simple system. SQL server has a page system although I never investigated it.

Desmond.

#2 Eddyon

Eddyon
  • Members
  • PipPipPip
  • Advanced Member
  • 30 posts
  • LocationEngland

Posted 28 March 2006 - 01:03 PM

[a href=\"http://www.phpfreaks.com/tutorials/43/0.php\" target=\"_blank\"]Pagination: Easy as PREV 1 2 3 NEXT[/a]

Nice tutorial there from TheReverend.

And here is another, this time by phpfreak.

[a href=\"http://www.phpfreaks.com/tutorials/73/1.php\" target=\"_blank\"]Page Numbering With PHP And MySQL Results[/a]

#3 lansing

lansing
  • Members
  • PipPip
  • Member
  • 15 posts
  • LocationClinton, TN USA

Posted 13 April 2006 - 12:51 PM

[!--quoteo(post=359252:date=Mar 28 2006, 08:03 AM:name=Eddyon)--][div class=\'quotetop\']QUOTE(Eddyon @ Mar 28 2006, 08:03 AM) View Post[/div][div class=\'quotemain\'][!--quotec--]
[a href=\"http://www.phpfreaks.com/tutorials/43/0.php\" target=\"_blank\"]Pagination: Easy as PREV 1 2 3 NEXT[/a]

Nice tutorial there from TheReverend.

And here is another, this time by phpfreak.

[a href=\"http://www.phpfreaks.com/tutorials/73/1.php\" target=\"_blank\"]Page Numbering With PHP And MySQL Results[/a]
[/quote]I have been trying different things to get this code to work for days now. I used that Pagination: Easy as PREV 1 2 3 NEXT. My code isn't showing the PREV & NEXT as links. I don't have any links to navigate to different pages. I think it has something to do with checking to see if the [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]?page=1[!--colorc--][/span][!--/colorc--] or the [!--coloro:#FF0000--][span style=\"color:#FF0000\"][!--/coloro--]?page=2[!--colorc--][/span][!--/colorc--] in the url is there or not. I don't see any code for that.

Like in the Page Numbering With PHP And MySQL Results tutorial it has coding like this which the other tutorial doesn't. I can get lnks in the 2nd tutorial, but I like the first one better.
if(!isset($_GET['page'])){
    $page = 1;
} else {
    $page = $_GET['page'];
}


This is the code for my page!
<?php

    //@mysql_connect($localhost, $user, $password) or die("ERROR--CAN'T CONNECT TO SERVER");
    //@mysql_select_db($database) or die("ERROR--CAN'T CONNECT TO DB");

    $limit          = 5;               
    $query_count    = "SELECT count(*) FROM orders";    
    $result_count   = mysql_query($query_count);    
    $totalrows      = mysql_num_rows($result_count); 

    if(empty($page)){
        $page = 1;
    }
        

    $limitvalue = $page * $limit - ($limit); 
    $query  = "SELECT * FROM orders LIMIT $limitvalue, $limit";        
    $result = mysql_query($query) or die("Error: " . mysql_error()); 

    if(mysql_num_rows($result) == 0){
        echo("Nothing to Display!");
    }

    $bgcolor = "#E0E0E0"; // light gray

    echo("<table>");
    
    while($row = mysql_fetch_array($result)){
        if ($bgcolor == "#E0E0E0"){
            $bgcolor = "#FFFFFF";
        }else{
            $bgcolor = "#E0E0E0";
        }

    echo("<tr bgcolor=".$bgcolor.">n<td>");
    echo($row["order_id"]);
    echo("</td>n<td>");
    echo($row["customers_id"]);
    echo("</td>n</tr>");
    }

    echo("</table>");

    if($page != 1){ 
        $pageprev = $page--;
        
        echo("<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV</a> "); 
    }else{
        echo("PREV");
    }

    $numofpages = $totalrows / $limit; 
    
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ");
        }
    }


    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo($i." ");
        }else{
            echo("<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ");
        }
    }

    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
         
        echo("<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT ".$limit."</a>"); 
    }else{
        echo("NEXT ".$limit); 
    }
    
    mysql_free_result($result);

?>





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users