Mancent Posted March 1, 2011 Share Posted March 1, 2011 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? Quote Link to comment https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/ Share on other sites More sharing options...
KPH71 Posted March 1, 2011 Share Posted March 1, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/#findComment-1181374 Share on other sites More sharing options...
Maq Posted March 1, 2011 Share Posted March 1, 2011 SQL would bet the best way to limit your results. As KPH mentioned, the LIMIT clause can take 2 parameters to specify a range of where to limit. Quote Link to comment https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/#findComment-1181384 Share on other sites More sharing options...
Mancent Posted March 1, 2011 Author Share Posted March 1, 2011 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"; Quote Link to comment https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/#findComment-1181418 Share on other sites More sharing options...
Maq Posted March 1, 2011 Share Posted March 1, 2011 Yep, try it out. Quote Link to comment https://forums.phpfreaks.com/topic/229274-while-row-fetch-only-25-results-how/#findComment-1181430 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.