?
Solved

Viewing MySql records into pages

Posted on 2003-02-23
4
Medium Priority
?
160 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this. Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

771 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