Kevin Reeves
Kevin Reeves

Reputation: 65

display while loop results on next page?

Hi i have a function that collects a list of posts added by users and displays it in a while loop down the page.

I have limited the number of results diplayed i.e. LIMIMT 15 so it doesnt overflow on the page. but what i want to be able to do now is have all the other results (however many a user may add) to display on the next page. Each page only ever showing 15 results. So you end up with page 1, page 2, page 3 etc.

How is this achieved?

function get_forum() {
            global $connection;
            global $profile_id;
            $query = "SELECT *
                        FROM ptb_forum
                        WHERE ptb_forum.deleted ='0'
                        ORDER BY ptb_forum.date_added DESC
                        LIMIT 0 , 16";
            $forum_set = mysql_query($query, $connection);
            confirm_query($query, $connection);
            return $forum_set;
        }

<?php
    $forum_set = get_forum();

    ?>
    <br/>
    <h3>Latest Forum Posts</h3>
    <br/> 

    <?php
    if(mysql_num_rows($forum_set) > 0) {
        while ($forum = mysql_fetch_array($forum_set)) {
            $age = days_from_date($forum['date_added']);
            ?>
            <div class="prof-content-box-forum" id="reviews">
             <div class="forum-content">
             <?php echo "{$forum['content']}"; ?>
             </div>
             <div class="message_pic">
             <?php echo "<a href=\"profile.php?id={$forum['from_user_id']}\"><img width=\"50px\" height=\"50px\"  src=\"{$prof_photo}\"></a>";?>

             </div>

             <div class="forum-text">
             <?php echo "Posted by {$forum2['display_name']}"; ?> <?php echo "".$age." days ago"; ?>
          </div>

             </div>

             <? } } ?>

Upvotes: 0

Views: 216

Answers (1)

Kai Qing
Kai Qing

Reputation: 18843

get_forum(); needs to factor the page it's on.

    $page = $_GET['page'];
    $page or $page = 1;
    $offset = ($page * 15) - 15;
    // obviously you need to sanitize this

    "SELECT *
    FROM ptb_forum
    WHERE ptb_forum.deleted ='0'
    ORDER BY ptb_forum.date_added DESC
    LIMIT $offset , 16"

You will also need to write out the pagination so your users can browse these pages. A basic google search on php pagination will give you many pre made functions for this. This example assumes you are using the $_GET var "page" to represent the page you're on. So for example, yoursite.com/forum.php?page=2

Also, I just put the query itself as an example here since there are many ways you can execute this that are not using mysql_ functions.

You should stop using mysql_ functions. PDO and mysqli are very easy to implement and tons of support exist all over this site as well as basically the whole internet.

Upvotes: 1

Related Questions