I want to auto load new data into my php page, my homepage is getting to long with statuses, so when people go to the home page i want it to only show ten, then when they scroll to the bottom of the page, it loads the next ten, and so on.

right now this is what i have set to load the data.

<?php
                $sql = mysql_query("SELECT * FROM post WHERE Deleted like '0' ORDER BY ID DESC");
                $numrows = (mysql_num_rows($sql));
                if($numrows > 0){
                    while ($row = mysql_fetch_assoc($sql)){
                        $uid = $row['UID'];
                        $postid = $row['ID'];
                        $post1 = $row['Post'];
                        $Timestamp = $row['Timestamp'];
                        $id = $row['ID'];
                        $check_pic = "members/$uid/image01.jpg";
                        $default_pic="members/0/default.png";
                        if(file_exists($check_pic)){
                            $user_pic ="<img src=\"$check_pic\" width=\"50\" heign='50'>";
                        }else{
                            $user_pic ="<img src=\"$default_pic\" width=\"50\" heign='50'/>";
                        }
                        if ($user=='1'){
                            $type = 'Admin';
                        }else{
                            $type = 'member';
                        }
                        $namesql = mysql_query("SELECT * FROM members WHERE ID like $uid");
                        while($row = mysql_fetch_assoc($namesql)){
                            $firstname = $row['firstname'];
                            $lastname = $row['lastname'];
                            $Name = "$firstname $lastname";
                            $uid1 = $_COOKIE['idCookie'];
                            if($uid == $uid1){
                                $delete = "  - <a href='index.php?delete=$id'><img border='0' title='Delete this post.' src='images/delete.jpg'></a>";
                            }else{
                                $delete = "";
                            }
                        }
                        $reg_exUrl = "/(http|https|ftp|ftps)\:\/\/[a-zA-Z0-9\-\.]+\.[a-zA-Z]{2,3}(\/\S*)?/";
                        $text = $post1;
                        if(preg_match($reg_exUrl, $text, $url)){
                            $text = preg_replace($reg_exUrl, "<a href='{$url[0]}'>{$url[0]}</a>", $text);
                        }else{
                            $text;
                        }
                    echo "<table width='100%'>
                                <tr><td width='51' valign='top'>$user_pic</td><td valign='top'><a href='profile.php?id=$uid'>$Name</a>$delete<br>$text<br><span style='font-size:0.8em;'>Posted: $Timestamp</span><br></td></tr>
                                <tr><td colspan='2' bgcolor='#EEEEEE'>
                                <a href='#' onclick=\"document.getElementById('$postid').style.display = 'block';\">Comment</a>
                                    <form action='index.php?comment=$postid' method='post'>
                                    <div id='$postid' style='display:none;'>
                                    <input type='text' name='message'/>
                                    <input name='uid' type='hidden' value='$uid1' />
                                    <input name='comment' type='hidden' />
                                    <input name='pid' type='hidden' value='$postid' />
                                    <input type='submit' value='Post'/>
                                    </div>
                                </form>
                                </td></tr><tr><td colspan='2' bgcolor='#EEEEEE'>";
                            $commentsql = mysql_query("SELECT * FROM postcomment WHERE postid like $postid");
                            while($row = mysql_fetch_assoc($commentsql)){
                                $comment = $row['comment'];
                                $uid = $row['uid'];
                                $pid = $row['postid'];
                                $commentname = mysql_query("SELECT * FROM members WHERE id LIKE $uid");
                                while($row = mysql_fetch_assoc($commentname)){
                                    $firstname = $row['firstname'];
                                    $lastname = $row['lastname'];
                                    $name = "$firstname $lastname";
                                    $uid123 = $row['id'];
                                    $check_pic = "members/$uid/image01.jpg";
                                    $default_pic="members/0/default.png";
                                    if(file_exists($check_pic)){
                                        $user_pic1 ="<img src=\"$check_pic\" width=\"32\" border='0' height='32'/>";
                                    }else{
                                        $user_pic1 ="<img src=\"$default_pic\" width=\"32\" border='0' height='32'/>";
                                    }
                                    if($postid == $pid)
                                    {
                                        echo "<div style=\"float:left;margin:0 5px 5px 0;\"><a href=''>$user_pic1</div>$name</a><br>$comment<br><hr>";
                                    }
                                }
                            }
                            echo "</td></tr></table>
                            <hr>";
                    }
                }else{
                    echo "Theres no statuses to display!";
                }
                mysql_close();
                ?>

Dani AI

Generated

— the simplest, most reliable pattern is server-side pagination plus a client-side loader. 's "paginator" suggestion is on point: return 10 posts per request (SQL LIMIT/OFFSET) and fetch the next page with AJAX when the page bottom is reached — or use an explicit "Load more" button (better for accessibility and fewer accidental requests than infinite scroll).

Example server endpoint (PDO, return HTML fragments):

<?php
// load_posts.php
$limit = 10;
$page  = max(1, (int)($_GET['page'] ?? 1));
$offset = ($page - 1) * $limit;
$stmt = $pdo->prepare(
  "SELECT id, uid, post, timestamp FROM post WHERE Deleted = 0 ORDER BY id DESC LIMIT :lim OFFSET :off"
);
$stmt->bindValue(':lim', $limit, PDO::PARAM_INT);
$stmt->bindValue(':off', $offset, PDO::PARAM_INT);
$stmt->execute();
foreach ($stmt->fetchAll(PDO::FETCH_ASSOC) as $row) {
  echo '<div class="post">'.htmlspecialchars($row['post']).'<div class="time">'.htmlspecialchars($row['timestamp']).'</div></div>';
}

Client-side trigger (jQuery, basic infinite loader):

var page = 1, loading = false;
function loadMore() {
  if (loading) return;
  loading = true; page++;
  $.get('load_posts.php', {page: page}, function(html){
    if (!$.trim(html)) { $(window).off('scroll'); return; }
    $('#feed').append(html); loading = false;
  });
}
$(window).on('scroll', function(){
  if ($(window).scrollTop() + $(window).height() >= $(document).height() - 150) loadMore();
});

Practical notes: replace deprecated mysql_ with PDO/MySQLi and use prepared statements; avoid SELECT and fetch only needed columns; add an index on (Deleted, id) for faster queries; escape output with htmlspecialchars to prevent XSS; avoid expensive per-row filesystem checks (store avatar info in DB or let the browser fallback); lazy-load comments separately; throttle requests or prefer "Load more" on slow connections; never trust client-side hidden user IDs for authorization — validate on the server.

Member Avatar for Member #120589

Sounds like you need a paginator. This can be a js-driven show/hide affair or php (or ajax) driven.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.