Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
?
Solved

Viewing MySql records into pages

Posted on 2003-02-23
4
Medium Priority
?
163 Views
Last Modified: 2006-11-17
I'm using PHP4 with MySql and I want to view a query result into pages each page with 10 records max. so can anyone help me to do that?

My psaudo code is:

<HTML>
<BODY>
<FORM>
.
.
</FORM>

<?php
.
.
.
while ($row=mysql_fetch_array($result))
{
 printf("%s",$row["xyz"]);                  
}
?> <!-- End of PHP code -->

Thanks,

Champion
0
Comment
Question by:Champion
  • 2
4 Comments
 
LVL 7

Expert Comment

by:Big_Red_Dog
ID: 8002883
Hi, I provided a complete working example in this question.  Just cut-n-paste, try it out, then modify it for your own database tables and columns...

See: http://www.experts-exchange.com/Web/Web_Languages/PHP/Q_20522370.html
0
 
LVL 1

Expert Comment

by:KC_Speedball
ID: 8004101
yes, thats it. Try the link...nothing more to add
0
 

Author Comment

by:Champion
ID: 8008204
Thanks... I used that example but because im using FORM in my example and the values from that FORM will be missed after doing that!
so how I can make that without missing the form data (Remember im using POST method).

- Champion
0
 
LVL 7

Accepted Solution

by:
Big_Red_Dog earned 400 total points
ID: 8008385
Here you go:

<?php
    session_start();
    mysql_connect("localhost", "testuser", "test")
         or die("(".mysql_errno().") Error:".mysql_error());
    mysql_select_db( "test3" )
         or die("(".mysql_errno().") Error:".mysql_error());
?>
<HTML>
<HEAD>
<TITLE>Example 1</TITLE>
</HEAD>
<BODY>

Here is the example:

<?php
    $items_per_page = 3;

    if (isset($_POST['PREV']) && isset($_POST['startprev'])) {
      $first = $_POST['startprev'];
    } else if (isset($_POST['NEXT']) && isset($_POST['startnext'])) {
      $first = $_POST['startnext'];
    } else {
      $first = 0;
    }

    $sql = "SELECT * FROM names_table";
    $result = mysql_query($sql) or die("(".mysql_errno().") Error:".mysql_error());
    $total_rows = mysql_num_rows($result);
    mysql_free_result($result);

    $sql = "SELECT * FROM names_table ORDER BY lastname LIMIT " . $first . ", " . $items_per_page;
    $result = mysql_query($sql) or die("(".mysql_errno().") Error:".mysql_error());

    $trow = 0;
    print "<table>"."\n";
    while ($row = mysql_fetch_array($result)) {
       if ($trow=='0') {
        print "<TH>last</TH><TH>first</TH><TH>ID</TH>";
       }
       $trow++;
       print "<TR>";
       print "<TD>".$row['lastname']."</TD><TD>".$row['firstname']."</TD><TD>".$row['id']."</TD>";
       print "</TR>";
    }
    print "</TABLE>";
    mysql_free_result($result);
    print "<P>";
    print "<FORM ACTION='test.php' METHOD='POST'>";
    if ($first != '0') {
     $start = $first - $items_per_page;
        print "<INPUT TYPE='HIDDEN' NAME='startprev' VALUE='" . $start . "'>";
        print "<INPUT TYPE='SUBMIT' NAME='PREV' VALUE='Previous'>";
    }
    $endrow = $first + $trow;
    if ($endrow < $total_rows) {
     $start = $first + $items_per_page;
        print "<INPUT TYPE='HIDDEN' NAME='startnext' VALUE='" . $start . "'>";
        print "<INPUT TYPE='SUBMIT' NAME='NEXT' VALUE='Next'>";
    }
    print "</FORM>";
?>

</BODY>
</HTML>
0

Featured Post

Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.
Suggested Courses

577 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question