Jump to content

Display mysql results in 10 per page


ted_chou12

Recommended Posts

I sort of have the idea, but i dont know how to put it into practical, what I believe will do is this:
by using the while fuction as well as the modules function, so when the remainder equal to one, then the list goes to another page for the new 10 results, and goes on until when the modules cannot reach 1, it will display the remainder of the ten pages in the last page. But what I am not sure is how to add pages, can anyone help me out with this?
Thanks
Ted
Link to comment
https://forums.phpfreaks.com/topic/31985-display-mysql-results-in-10-per-page/
Share on other sites

here is what I use. Change it up to fit your database structure
[code]<?php
// connect to mysql below


// Set how many rows to display on each page
$limit = 200;
// Query the database to get the total number of rows
$query_count = "SELECT * FROM tablename";
  $result_count = mysql_query($query_count) or die (mysql_error());
    $totalrows = mysql_num_rows($result_count);
    if(isset($_GET['page'])){    // Checks if the $page variable is empty (not set)
        $page = $_GET['page'];      // If it is empty, we're on page 1
    } else {
        $page = 1;
    }
// Set the start value
$startvalue = $page * $limit - ($limit);
// Query the database and set the start row and limit
$sql = "SELECT * FROM table_name LIMIT $startvalue, $limit";
  $res = mysql_query($sql) or die (mysql_error());
    echo "<table border=1 width=450 align=center>
            <tr>
              <td width=100>field1 title</td>
              <td width=175>field2 title</td>
              <td width=175>field3 title</td>
            </tr>";
// Do a quick check to see if there are any records to display
    if(mysql_num_rows($res) == 0){
        echo "<tr>
                <td colspan=3 align=center>No Records found!!</td>
              </tr>";
    }
// Start loop through records
while ($r = mysql_fetch_array($res)){
// Echo out the records with wordwrap, remove if you like
echo "<tr>\n";
echo "<td width=100>" . wordwrap($r['field1'], 2, " ", 1)."</td>\n";
echo "<td width=175>" . wordwrap ($r['field2'], 10, " ", 1)."</td>\n";
echo "<td width=175>" . wordwrap($r['field3'], 10, " ", 1)."</td>\n";
echo "</tr>\n";
}
// Close the table
echo "</table>";
// Start links for pages
echo "<p align=center>";

// Sets link for previous 25 and return to page 1
    if($page != 1){
        $pageprev = ($page - 1);
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=1\"><<</a>&nbsp;&nbsp;";
        echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pageprev\">PREV&nbsp;</a> ";
    }else{
        echo "PREV&nbsp;";
    }
// Find out the total number of pages depending on the limit set
    $numofpages = $totalrows / $limit;
    $totalpages = round($numofpages);
// Loop thru all the pages and echo out the links
    for($i = 1; $i <= $numofpages; $i++){
        if($i == $page){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
        }
    }

// Check for straglers after the limit blocks
    if(($totalrows % $limit) != 0){
        if($i == $page){
            echo "[".$i."] ";
        }else{
            echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> ";
        }
    }
// Print out the Next 25 and Goto Last page links
    if(($totalrows - ($limit * $page)) > 0){
        $pagenext = $page++;
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$pagenext\">NEXT&nbsp;</a>&nbsp;&nbsp;";
          echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$totalpages\">>></a>&nbsp;&nbsp;";
    }else{
        echo("NEXT");
    }
echo "</p>";
// Free results
mysql_free_result($res);
// Close mysql connection
mysql_close($mysql_conn);
?>[/code]
Since the links don't seem to work for you:

1. Point your browser to http://www.phpfreaks.com
2. Click the PHP Tutorials link in the middle, right of the page
3. Click the "Page Numbering / Pagination" link on the Tutorials page
4. Click the "Page Numbering With PHP And MySQL Results" tutorial (which is the one Barand suggested)
Thanks mjdamato, but I cannot access all of the pages before the forum folder, after the "http://www.phpfreaks.com/forums/" <<<before the folders of forums, all the pages turn out blank white for me, and I dont know why, I tried to add the http://www.phpfreaks.com link as a safe link as a property of IE but it still wouldnt work... btw, i first got here through google, so i didn't get through the main page at all...
Thanks anyway, really appreciated it :D
Ted

Archived

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

×
×
  • Create New...

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.