Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Page 1 of 2 12 LastLast
Results 1 to 15 of 25
  1. #1
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts

    Coding next button in PHP

    Hi,
    My SQL query returns 50 rows, but am trying to sort that in 10 per page and have a next button to view others.
    Am using the following code:
    while($row = mysql_fetch_array($result))
    {
    echo "<tr>";
    echo "<img src=" . "'upload/" . $row['photo'] . "' width='150' height='150' />". "</td>";
    echo "<td width='400'>" . $row['description'] . "</td>";
    echo "<td>" . $row['price'] . "</td>";
    echo "</tr>";

    }
    Can someone plese help me on this.
    Thanks

  • #2
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    you need to limit that for each page. i.e. use the LIMIT clause in SQL and let each page pass a parameter, that determines, which 10 results to print.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #3
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    thanks for your reply Dormilich. I got the LIMIT clause in sql, but how do i pass parameter for each page.

  • #4
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    in the URL. say index.php?page=1 and in PHP
    PHP Code:
    $page = (int) $_GET['page'];
    $query "… LIMIT " . (($page 1) * 10) . ", 10"
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #5
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    thanks for your help Dormilich. Really appreciate it

  • #6
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    you’re welcome.

    of course, a Prepared Statement would be much cooler (and safer).
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    avizhome (03-23-2010)

  • #7
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Hi Dormilich,
    i have tried:
    Code:
    $page = (int) $_GET['page'];
    $sql="select * from cars LIMIT 0,2" . (($page - 1) * 10) . ", 10";
    but its giving me this error:
    Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource in /home/buysell5/public_html/car_search_result.php on line 90

  • #8
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    that’s correct, your SQL syntax is wrong.
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • #9
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    oh sorry typo error
    Code:
    $page = (int) $_GET['page'];
    $sql="select * from cars LIMIT" . (($page - 1) * 10) . ", 10";

  • #10
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    there is a space missing after LIMIT *geez*
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer

  • Users who have thanked Dormilich for this post:

    avizhome (03-23-2010)

  • #11
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    thanks

  • #12
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    ok, i got the part of sorting 10 records/page. I can do that by having a next button, but i would like to have something similar to page 1,2...10. where users can select which page they woild like to view. Is it possible with PHP?

  • #13
    Senior Coder
    Join Date
    Jul 2009
    Location
    South Yorkshire, England
    Posts
    2,318
    Thanks
    6
    Thanked 304 Times in 303 Posts
    Do a search on here or Google for pagination scripts.

  • Users who have thanked MattF for this post:

    avizhome (03-23-2010)

  • #14
    New Coder
    Join Date
    Mar 2010
    Location
    Australia
    Posts
    93
    Thanks
    38
    Thanked 0 Times in 0 Posts
    Thanks MattF, i love this forum therz always someone there 2 help you....thanks alot

  • #15
    Senior Coder Dormilich's Avatar
    Join Date
    Jan 2010
    Location
    Behind the Wall
    Posts
    3,280
    Thanks
    12
    Thanked 343 Times in 339 Posts
    guess what the $page parameter does …
    The computer is always right. The computer is always right. The computer is always right. Take it from someone who has programmed for over ten years: not once has the computational mechanism of the machine malfunctioned.
    André Behrens, NY Times Software Developer


  •  
    Page 1 of 2 12 LastLast

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •