Displaying results on multiple pages

Discussion in 'PHP' started by johneva, May 19, 2009.

  1. #1
    Hi

    I could do with a little help, I got a basic script and think its close to what I need now but its not quite working though. excuse the crudeness of the html and formatting for the data, its just till I get the main workings of this right first.

    Here is a link to the page as it is so you can see where things are at and what sort of data is being requested.
    http://www.john-eva.co.uk/multi-details-page.php

    Here is the code

    
    <a href="#" onClick="window.location.reload( true );">Reload</a> 
    
    <? 
    $db_addr = 'localhost';        // address of MySQL server. 
    $db_user = 'username';        // Username to access server. 
    $db_pass = 'password';        // Password access server. 
    $db_name = 'database_name';    // Name of database to connect to. 
    $connect = @mysql_connect("localhost", "user", "password"); 
    
    if (!($connect)) // If no connect, error and exit(). 
    { 
    echo("<p>Unable to connect to the database server.</p>"); 
    exit(); 
    } 
    
    if (!(@mysql_select_db($db_name))) // If can't connect to database, error and exit(). 
    { 
    echo("<p>Unable to locate the $db_name database.</p>"); 
    exit(); 
    } 
    
    if (!($limit)){ 
    $limit = 10;} // Default results per-page. 
    if (!($page)){ 
    $page = 0;} // Default page value. 
    $numresults = mysql_query("SELECT * FROM data WHERE bike LIKE '%". $query ."%'"); // the query. 
    $numrows = mysql_num_rows($numresults); // Number of rows returned from above query. 
    if ($numrows == 0){ 
    echo("No results found matching your query - $query"); // bah, modify the "Not Found" error for your needs. 
    exit();} 
    
    $pages = intval($numrows/$limit); // Number of results pages. 
    
    // $pages now contains int of pages, unless there is a remainder from division. 
    
    if ($numrows%$limit) { 
    $pages++;} // has remainder so add one page 
    
    $current = ($page/$limit) + 1; // Current page number. 
    
    if (($pages < 1) || ($pages == 0)) { 
    $total = 1;} // If $pages is less than one or equal to 0, total pages is 1. 
    
    else { 
    $total = $pages;} // Else total pages is $pages value. 
    
    $first = $page + 1; // The first result. 
    
    if (!((($page + $limit) / $limit) >= $pages) && $pages != 1) { 
    $last = $page + $limit;} //If not last results page, last result equals $page plus $limit. 
    
    else{ 
    $last = $numrows;} // If last results page, last result equals total number of results. 
    
    //escape from PHP mode. 
    ?> 
    <html> 
    <head> 
    <title>Current Bikes in Stock</title> 
    </head> 
    <body> 
    <center><h2>Current Bikes in Stock</h2></center> 
    
    <table width="100%" border="0"> 
    <tr> 
      <td width="50%" align="left">Results <b><?=$first?></b> - <b><?=$last?></b> of <b><?=$numrows?></b></td> 
      <td width="50%" align="right">Page <b><?=$current?></b> of <b><?=$total?></b></td> 
    </tr> 
    
    <tr> 
      <td colspan="2">&nbsp;</td> 
    </tr> 
    
    <tr> 
      <td colspan="2">Page - 
    <? 
    if ($page != 0) { // Don't show back link if current page is first page. 
    $back_page = $page - $limit; 
    echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");} 
    
    for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. 
    { 
    $ppage = $limit*($i - 1); 
    if ($ppage == $page){ 
    echo("<b>$i</b> \n");} // If current page don't give link, just text. 
    else{ 
    echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");} 
    } 
    
    if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. 
    $next_page = $page + $limit; 
    echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");} 
    ?> 
    </td> 
    </tr> 
    
    <tr> 
    <td colspan="2"> 
    <? 
    //Go back into PHP mode. 
    
    // Now we can display results. 
    $results = mysql_query("SELECT * FROM data WHERE bike LIKE '%". $query ."%' LIMIT $page, $limit"); 
    Print "<table border cellpadding=3>"; 
    while ($data = mysql_fetch_array($results)) 
    { 
    Print "<tr>"; 
    Print "<th>Bike:</th> <td>".$data['bike'] . "</td></tr><tr> "; 
    Print "<th>Location:</th> <td>".$data['location'] . "</td></tr><tr> "; 
    Print "<th>Price:</th> <td>&pound;".$data['price'] . "</td></tr><tr> "; 
    Print "<th>Details:</th> <td>".$data['details'] . "</td></tr><tr> "; 
    Print "<th>Image:</th> <td><img src='uploaded_files/".$data['database_filename'] . "' alt='".$info['database_filename'] . "' /> </td></tr>"; 
    } 
    print "</table>"; 
    ?> 
      </td> 
    </tr> 
       
    <tr> 
      <td colspan="2" align="right"> 
    Results per-page: <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=5">5</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=10">10</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=20">20</a> | <a href="<?=$PHP_SELF?>?query=<?=$query?>&page=<?=$page?>&limit=50">50</a> 
      </td> 
    </tr> 
    <tr> 
    <td colspan="2">Page - 
    <? 
    if ($page != 0) { // Don't show back link if current page is first page. 
    $back_page = $page - $limit; 
    echo("<a href=\"$PHP_SELF?query=$query&page=$back_page&limit=$limit\">back</a>    \n");} 
    
    for ($i=1; $i <= $pages; $i++) // loop through each page and give link to it. 
    { 
    $ppage = $limit*($i - 1); 
    if ($ppage == $page){ 
    echo("<b>$i</b> \n");} // If current page don't give link, just text. 
    else{ 
    echo("<a href=\"$PHP_SELF?query=$query&page=$ppage&limit=$limit\">$i</a> \n");} 
    } 
    
    if (!((($page+$limit) / $limit) >= $pages) && $pages != 1) { // If last page don't give next link. 
    $next_page = $page + $limit; 
    echo("    <a href=\"$PHP_SELF?query=$query&page=$next_page&limit=$limit\">next</a>\n");} 
    ?> 
    </td> 
    </tr> 
    </table> 
    </body> 
    </html> 
    
    PHP:
     
    johneva, May 19, 2009 IP
  2. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I tried messaging you on MSN. I can help you out.
     
    NatalicWolf, May 19, 2009 IP
    johneva likes this.
  3. NatalicWolf

    NatalicWolf Peon

    Messages:
    262
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    0
    #3
    
    <?
    
    if (isset($_GET['limit']) && is_numeric($_GET['limit']) && intval($_GET['limit'])>0)
    	$DisplayLimit=intval($_GET['limit']);
    else
    	$DisplayLimit=10;
    
    if (isset($_GET['page']) && is_numeric($_GET['page']) && intval($_GET['page'])>0)
    	$Page=intval($_GET['page']);
    
    if($Page<=0)
    	$Page=1;
    
    
    /* Handle Connections - Put in another file and use include */
    $DBName='';
    $connect = mysql_connect("localhost", "", "") or die("<p>Unable to connect to the database server.</p>");
    
    mysql_select_db($DBName) or die("<p>Unable to locate the $db_name database.</p>");
    /* End Connections */
    
    $Select = $Page*$DisplayLimit;
    
    mysql_query("SELECT SQL_CALC_FOUND_ROWS bike FROM data"); // the query.
    $Overall = mysql_result(mysql_query("SELECT FOUND_ROWS()"),0); // Number of rows returned from above query.
    
    if (!$Overall) die("No results matching your query");
    
    $NumPages = ceil($Overall/$DisplayLimit); // Number of results pages.
    
    if($Page>$NumPages || $Page>$Overall)
    {
    	$Select=10;
    	$Page=1;
    	$limit=$DisplayLimit;
    }
    
    ?>
    <html>
    <head>
    <title>Current Bikes in Stock</title>
    </head>
    <body>
    <center><h2>Current Bikes in Stock</h2></center>
    
    <table width="100%" border="0">
     <tr>
      <td width="50%" align="left">Results <b><?=($Select-$DisplayLimit)+1?></b> - <b><?=$Select?></b> of <b><?=$Overall?></b></td>
      <td width="50%" align="right">Page <b><?=$Page?></b> of <b><?=$NumPages?></b></td>
     </tr>
     
     <tr>
      <td colspan="2">&nbsp;</td>
     </tr>
     
     <tr>
      <td colspan="2">Page Select - 
    <?
    	
    	if($Page>1) echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($Page-1).'&limit='.$DisplayLimit.'"><< Prev</a> ';
    	for($i=1;$i<=$NumPages;$i++)
    		echo ($i==$Page?' <b>'.$i.'</b> ':' <a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'&limit='.$DisplayLimit.'">'.$i.'</a> ');
    	if($Page!=$NumPages) echo ' <a href="'.$_SERVER['PHP_SELF'].'?page='.($Page+1).'&limit='.$DisplayLimit.'">Next >></a>';
    ?>
    </td>
    </tr>
    
    <tr>
     <td colspan="2">
    <?
    //Go back into PHP mode.
    
    // Now we can display results.
    $results = mysql_query("SELECT * FROM data LIMIT ".(($Page-1)*$DisplayLimit).", ".$DisplayLimit) or die(mysql_error());
    Print "<table border cellpadding=3>"; 
    while ($data = mysql_fetch_array($results))
    { 
    Print "<tr>"; 
    Print "<th>Bike:</th> <td>".$data['bike'] . "</td></tr><tr> "; 
    Print "<th>Location:</th> <td>".$data['location'] . "</td></tr><tr> ";
    Print "<th>Price:</th> <td>&pound;".$data['price'] . "</td></tr><tr> ";
    Print "<th>Details:</th> <td>".$data['details'] . "</td></tr><tr> ";
    Print "<th>Image:</th> <td><img src='uploaded_files/".$data['database_filename'] . "' alt='".$info['database_filename'] . "' /> </td></tr>"; 
    } 
    print "</table>";
    ?>
      </td>
     </tr>
      
    <tr>
      <td colspan="2" align="right">
    Results per-page: 
    <?PHP
    for($i=5;$i<=20;$i+=5)
    	echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.$Page.'&limit='.$i.'">'.$i.'</a> ';
    ?>
      </td>
     </tr>
    <tr>
     <td colspan="2">
    	<?
            
            if($Page>1) echo '<a href="'.$_SERVER['PHP_SELF'].'?page='.($Page-1).'&limit='.$DisplayLimit.'"><< Prev</a> ';
            for($i=1;$i<=$NumPages;$i++)
                echo ($i==$Page?' <b>'.$i.'</b> ':' <a href="'.$_SERVER['PHP_SELF'].'?page='.$i.'&limit='.$DisplayLimit.'">'.$i.'</a> ');
            if($Page!=$NumPages) echo ' <a href="'.$_SERVER['PHP_SELF'].'?page='.($Page+1).'&limit='.$DisplayLimit.'">Next >></a>';
        ?>
    </td>
    </tr>
    </table>
    </body>
    </html>
    
    
    PHP:
     
    NatalicWolf, May 19, 2009 IP