How Can I Limit the Results of a MySQL Selection per Page?

Discussion in 'PHP' started by Masterful, Sep 7, 2008.

  1. #1
    The code below selects all of the coupons in my database and presents them on one page! :eek:

    How can I change it so that only 10 coupons show per page?

    <?php
    
    $con = mysql_connect("localhost","root");
    
    if (!$con)
    
    {
    die('Could not connect: ' . mysql_error());
    }
    
    mysql_select_db("ctyi", $con);
    
    $result = mysql_query("SELECT
    
    Coupon_Table.offer_anchor_text,
    Coupon_Table.offer_url,
    DATE_FORMAT(Coupon_Table.offer_expiration_date, '%d %M, %Y') AS formated_date,
    Coupon_Table.coupon_code,
    
    Advertiser_Table.advertiser_logo,
    Advertiser_Table.advertiser_name,
    Advertiser_Table.advertiser_url,
    
    Category_Table.category_name,
    Category_Table.category_url,
    
    Offer_Instructions_Table.block_anchor_text,
    Offer_Instructions_Table.offer_instructions
    
    FROM Coupon_Table, Advertiser_Table, Category_Table, Offer_Instructions_Table
    WHERE Coupon_Table.advertiser_id=Advertiser_Table.advertiser_id
    AND Coupon_Table.category_id=Category_Table.category_id
    AND Coupon_Table.offer_instructions_id=Offer_Instructions_Table.offer_instructions_id");
    
    while($row = mysql_fetch_array($result))
    
    {
    echo "<table border=\"0\">";
    echo "<tr>";
    echo "<td><img src=\"" . $row['advertiser_logo'] . "\" alt=\"\" /></td>";
    echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['offer_anchor_text'] . "</a><br />";
    echo (empty($row['coupon_code']) ? $row['offer_instructions'] : $row['coupon_code']);
    echo "<br /> Expires: " . $row['formated_date'] . "<br />";
    echo "About <a href=\"" . $row['advertiser_url'] . "\">" . $row['advertiser_name'] . "</a><br />";
    echo "Category: <a href=\"" . $row['category_url'] . "\">" . $row['category_name'] . "</a></td>";
    echo "<td><a href=\"" . $row['offer_url'] . "\">" . $row['block_anchor_text'] . "</a></td>";
    echo "</tr>";
    echo "</table>";
    }
    
    mysql_close($con);
    
    ?>
    PHP:
     
    Masterful, Sep 7, 2008 IP
  2. Freewebspace

    Freewebspace Notable Member

    Messages:
    6,213
    Likes Received:
    370
    Best Answers:
    0
    Trophy Points:
    275
    #2
    You can use Limit 1,10 statement along with the SELECT
     
    Freewebspace, Sep 7, 2008 IP
  3. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #3
    OK. But then it will just show 10 results on 1 page, right? I want pages 1, 2, 3, etc., to be generated and for 10 results to show per page. Do you know how I can do this?
     
    Masterful, Sep 7, 2008 IP
  4. Dollar

    Dollar Active Member

    Messages:
    2,598
    Likes Received:
    82
    Best Answers:
    0
    Trophy Points:
    90
    #4
    You need to use Pagination. Which is not easy to setup. If your using a framework, it may already have a pagination class.
     
    Dollar, Sep 7, 2008 IP
    Masterful likes this.
  5. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #5
    like freewebspace said, but the values of the limit must be dynamic..
     
    bartolay13, Sep 8, 2008 IP
  6. adrevol

    adrevol Active Member

    Messages:
    124
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    58
    #6
    Yes, doing pagination is bit tricky, but not tough

    Here is an article which works perfect, i tested it .. just go ahead with that and customize according to your requirements .

    ITTreats.com- Pagination in PHP
     
    adrevol, Sep 8, 2008 IP
    Masterful likes this.
  7. Masterful

    Masterful Well-Known Member

    Messages:
    1,653
    Likes Received:
    28
    Best Answers:
    0
    Trophy Points:
    140
    #7
    Masterful, Sep 8, 2008 IP