Jump to content

While row fetch only 25 results how?


Mancent

Recommended Posts

I have this code and Im using a while to fetch all the data where users comments are but i want to change it to fetch only 25 rows at a time and each time it refreshes it fetch's 25 more different rows.

<?php
include "../../connect.php";
Header('Cache-Control: no-cache');
Header('Pragma: no-cache');
$username = mysql_real_escape_string($_GET['username']);
//If friend is set, then query for it, otherwise return all of the friends comments.
$friend = (isset($_GET['friend'])) ? ' AND b.friend = \'' . mysql_real_escape_string($_GET['friend']) . '\' ' : NULL;

$sql = "SELECT a.username,a.comment,a.timestamp,a.title,a.picture,a.pic_status,a.pic_url,b.id,c.avatar,c.atimestamp,c.onlinestatus FROM (user_comments as a JOIN accounts as c USING(username))JOIN `user_friends_list` as b ON (b.friend = a.username)WHERE (b.username = '$username' $friend AND b.status = 1) ORDER BY a.id DESC";

$result = mysql_query($sql);
$user_xml = "<?xml version=\"1.0\"?>\n";
//$user_xml .= "<root>\n";
if(mysql_num_rows($result) > 0) 
{  
while ($row = mysql_fetch_assoc($result)) 
{        
$user_xml .= "<comment>\n";
$user_xml .= "<image>" . $row['avatar'] . "</image>\n";
$user_xml .= "<name>" . $row['username'] . "</name>\n";
$user_xml .=	"<text>\n";
$user_xml .=	"<![CDATA[ ". $row['comment'] ."]]>\n"; 
$user_xml .=	 "</text>\n";
$user_xml .= "<time>" . $row['timestamp'] . "</time>\n";
$user_xml .= "<title>" . $row['title'] . "</title>\n";
$user_xml .= "<picture>" . $row['picture'] . "</picture>\n";
$user_xml .= "<status>" . $row['pic_status'] . "</status>\n";
$user_xml .= "<url>" . $row['pic_url'] . "</url>\n";
$user_xml .= "<ntimestamp>" . $row['atimestamp'] . "</ntimestamp>\n";
$user_xml .= "<online>" . $row['onlinestatus'] . "</online>\n";
$user_xml .= "</comment>\n"; 
} 
}
//$user_xml .= "</root>\n"; 

echo $user_xml;  
?>

 

 

So i think i can do something like this but im not to sure.

 

just add a if statement

 

while ($row = mysql_fetch_assoc($result)) 
{ 


$user_xml .= "<comment>\n";
$user_xml .= "<image>" . $row['avatar'] . "</image>\n";
$user_xml .= "<name>" . $row['username'] . "</name>\n";
$user_xml .=	"<text>\n";
$user_xml .=	"<![CDATA[ ". $row['comment'] ."]]>\n"; 
$user_xml .=	 "</text>\n";
$user_xml .= "<time>" . $row['timestamp'] . "</time>\n";
$user_xml .= "<title>" . $row['title'] . "</title>\n";
$user_xml .= "<picture>" . $row['picture'] . "</picture>\n";
$user_xml .= "<status>" . $row['pic_status'] . "</status>\n";
$user_xml .= "<url>" . $row['pic_url'] . "</url>\n";
$user_xml .= "<ntimestamp>" . $row['atimestamp'] . "</ntimestamp>\n";
$user_xml .= "<online>" . $row['onlinestatus'] . "</online>\n";
$user_xml .= "</comment>\n"; 
} 

 

I only wnat to read 25 comments at a time and when i click a button to read more then it reads 25 more rows but it dose not read the data unless requested... can any one help me?

 

Link to comment
https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/
Share on other sites

The easiest way is probably with SQL - using LIMIT.

 

if you add:

 

"LIMIT 0, 25"

 

at the end of the query it will get the first 25 records (0 indicates starting at 1st record).

 

"LIMIT 25, 25"

 

will return the next 25 (starting from number 26).

 

You can then use PHP to determine which set of 25 is being shown.

So that would be in  my sql result.. so somthing like this?

 

 

$sql = "SELECT a.username,a.comment,a.timestamp,a.title,a.picture,a.pic_status,a.pic_url,b.id,c.avatar,c.atimestamp,c.onlinestatus FROM (user_comments as a JOIN accounts as c USING(username))JOIN `user_friends_list` as b ON (b.friend = a.username)WHERE (b.username = '$username' $friend AND b.status = 1) ORDER BY a.id DESC LIMIT 0, 25";

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.