1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Help With Pagination

Discussion in 'PHP' started by Clockwork Joe, Nov 1, 2013.

  1. #1
    I have my articles on one of my websites organized newest to oldest using simple php. Now that the number of articles is increasing, I need pagination. The tutorials I've been trying to do are not showing me how to keep my articles from newest to oldest (ascending id) with the pagination. Can any give me a hand?

    This is how I have it now.

    
    <div id="content">
    
    
    <?php
    $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC");
    while($row = mysql_fetch_array($sql)){
        $title = $row['title'];
        $description = $row['description'];
        $image = $row['image'];
    ?>
    
    
    <a class="box" href="#">
        <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div>
        <div class="boxleft">
            <p class="title"><?php echo $title ?></p>
            <p class="des"><?php echo $description ?></p>
        </div>
    </a>
    
    
    <?php
    }
    ?>
    </div>
    
    Code (markup):
    Any help would be greatly appreciated.
     
    Clockwork Joe, Nov 1, 2013 IP
  2. marht

    marht Active Member

    Messages:
    142
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    58
    #2
    Hi Clockwork Joe,

    To make a pagination working you need the add a variable LIMIT in your query.
    This variable will be set usually with an $_GET response.

    For example:

    
    <div id="content">
    
    <?php
    // Use a specified url to get an $_GET response, like: http://yourwebsite.com/?page=2
    $GETPAGE = intval($_GET['page']);
    $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC LIMIT ".$GETPAGE.",18 ");
    while($row = mysql_fetch_array($sql)){
        $title = $row['title'];
        $description = $row['description'];
        $image = $row['image'];
    ?>
    
    
    <a class="box" href="#">
        <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div>
        <div class="boxleft">
            <p class="title"><?php echo $title ?></p>
            <p class="des"><?php echo $description ?></p>
        </div>
    </a>
    
    
    <?php
    }
    ?>
    </div>
    PHP:
    Hope this help you out,

    Kind regards,

    Maarten
     
    marht, Nov 1, 2013 IP
  3. MakZF

    MakZF Well-Known Member

    Messages:
    390
    Likes Received:
    9
    Best Answers:
    1
    Trophy Points:
    140
    #3
    Hello Clockwork Joe, I would just google this topic because there are loads of simple examples/tutorials of pagination in PHP out there.

    I would not recomnend using marht's code as you should not be using PHP's old MySQL API at all. Check out PDO, which allows you to use prepared statements among other things - http://php.net/manual/en/book.pdo.php
     
    MakZF, Nov 1, 2013 IP
  4. marht

    marht Active Member

    Messages:
    142
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    58
    #4
    Hi MakZF,

    I agree with you that PDO is better. Nevertheless, the code is not bad and it gives the OP a pagination system ;-).

    Regards
     
    marht, Nov 4, 2013 IP
  5. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #5
    I'm not sure if the code above does what it's supposed to do. For instance, ?page=2 will start at the second record, and pull the next 18. Page 3 will start at the third record, and go from there. It's not how pagination is supposed to work, I'm afraid.

    To calculate the offset, you have to multiply the page number by the amount of rows you want to display on each page.
     
    nico_swd, Nov 6, 2013 IP
  6. marht

    marht Active Member

    Messages:
    142
    Likes Received:
    2
    Best Answers:
    1
    Trophy Points:
    58
    #6
    Thank you for correcting me.
    This should do the trick:

    <div id="content">
    <?php
    // Use a specified url to get an $_GET response, like: http://yourwebsite.com/?page=2
    $GETPAGE = intval($_GET['page']);
    $items_per_page = 18;//INPUT HERE THE NUMBER OF ITEMS PER PAGE MAX
    if ($GETPAGE!=0) $GETPAGE=(($GETPAGE-1)*$items_per_page);
    $sql = mysql_query("SELECT * FROM enteries ORDER BY id DESC LIMIT ".$GETPAGE.",".$items_per_page." ");
    while($row = mysql_fetch_array($sql)){
        $title = $row['title'];
        $description = $row['description'];
        $image = $row['image'];
    ?>
    <a class="box" href="#">
        <div class="boxleft"><img src="postpics/<?php echo $image ?>" width="148" height="120"></div>
        <div class="boxleft">
            <p class="title"><?php echo $title ?></p>
            <p class="des"><?php echo $description ?></p>
        </div>
    </a>
    <?php
    }
    ?>
    </div>
    PHP:
     
    marht, Nov 6, 2013 IP
  7. Ronnie403

    Ronnie403 Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    80
    Digital Goods:
    8
    #7
    Actually you don;t have to LIMIT the queries do something like array_chunk(); that will be an easy start for n00bs.. hehe.
     
    Ronnie403, Nov 11, 2013 IP
  8. bartolay13

    bartolay13 Active Member

    Messages:
    735
    Likes Received:
    14
    Best Answers:
    1
    Trophy Points:
    98
    #8
    disagree... you can't/must not/shouldn't do that.
     
    bartolay13, Nov 11, 2013 IP
  9. Ronnie403

    Ronnie403 Active Member

    Messages:
    50
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    80
    Digital Goods:
    8
    #9
    You are right, but it will be easy for him.. until later on
     
    Ronnie403, Nov 11, 2013 IP
  10. NetStar

    NetStar Notable Member

    Messages:
    2,471
    Likes Received:
    541
    Best Answers:
    21
    Trophy Points:
    245
    #10
    Don't suggest temporary fixes that will need to be changed later.
     
    NetStar, Nov 16, 2013 IP
  11. ezprint2008

    ezprint2008 Well-Known Member

    Messages:
    611
    Likes Received:
    15
    Best Answers:
    2
    Trophy Points:
    140
    Digital Goods:
    1
    #11
    what if you use the current date ...and get a greater/lesser than <> script to grab only the recent articles
    rather than processing and pulling * wildcard ALL... that'll kill your pagination the more articles you file.
     
    ezprint2008, Nov 16, 2013 IP
  12. nico_swd

    nico_swd Prominent Member

    Messages:
    4,153
    Likes Received:
    344
    Best Answers:
    18
    Trophy Points:
    375
    #12
    The wildcard is for the fields, and not the rows. Having it compare ALL dates in the database is actually much slower than using the offset/limit.
     
    nico_swd, Nov 17, 2013 IP