NSW42 Posted November 8, 2008 Share Posted November 8, 2008 Heya, wondering if someone can set me on the right path, what im trying to do and i know it can be done, is paginate a page without using mysql, the coding below is a roomlist for chat, what it does is grab the rooms on the server and echos them back, so instead of having 20 room etc on one page, id just like to paginate so the page isnt so long. Regards <?php echo " "; $ip = "Here"; $cg = $_GET['cat']; if ($cg != "") { $service_port = here; $address = gethostbyname ($ip); $socket = socket_create (AF_INET, SOCK_STREAM, 0); $result = socket_connect ($socket, $address, $service_port); $in = "ROOMLIST {$cg}\n"; $out = ""; socket_write ($socket, $in, strlen ($in)); while ($out = socket_read ($socket, 2048)) { echo $out; } socket_close ($socket); } echo " "; ?> Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/ Share on other sites More sharing options...
.josh Posted November 8, 2008 Share Posted November 8, 2008 Okay so basically what you need to do is instead of echoing $out, you need to store it in a session array, and from there, the pagination principles are pretty much the same. Instead of counting how many rows are in a database, you'd count how many elements are in the array. Instead of selecting x rows starting at an offset, you'd select x elements starting at the same offset. Instead of using a while loop to cycle through a fetch_blah, you'd use a regular loop that starts at the offset element, ends at offset+limit. You'd dynamically build links, passing the variable(s) through the URL just the same, and you'd echo out the page numbers just the same. Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685008 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 I'd check out the array_slice() function to make this a bit easier. You'd need to include some checking though: //pretend you did all the session stuff and $rooms is an array of all the rooms $total = count($rooms); $offset = $_GET['offset']; $limit = $_GET['limit']; if (($offset + $limit) > $total) { $limit = $total - $offset; } //now do processing $cut = array_slice($rooms, $offset, $limit); foreach ($cut as $room) { echo $room; } Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685014 Share on other sites More sharing options...
.josh Posted November 8, 2008 Share Posted November 8, 2008 Since you have to run a loop on the elements anyways, may as well just nix the array_slice, IMO. Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685016 Share on other sites More sharing options...
DarkWater Posted November 8, 2008 Share Posted November 8, 2008 But then you need to use a counter variable. But instead of assigning the array_slice() return value to a new variable, I guess you could do: //pretend you did all the session stuff and $rooms is an array of all the rooms $total = count($rooms); $offset = $_GET['offset']; $limit = $_GET['limit']; if (($offset + $limit) > $total) { $limit = $total - $offset; } //now do processing foreach (array_slice($rooms, $offset, $limit) as $room) { echo $room . "<br />"; } Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685017 Share on other sites More sharing options...
NSW42 Posted November 8, 2008 Author Share Posted November 8, 2008 thanks for the replies, I did up a pagination page for members via mysql not long ago, what im asking now, can it be implanted in that script, if so again im a tad lost with the roomlisting script on how to catch it with the pagination, coding below.. <?php include('config.php'); include('user_check.php'); $xpagedocol = 'D3D3D3'; // If current page number, use it // if not, set one! if(!isset($_GET['page'])){ $page = 1; } else { $page = $_GET['page']; } // Define the number of results per page $max_results = 10; // Figure out the limit for the query based // on the current page number. $from = (($page * $max_results) - $max_results); // Perform MySQL query on only the current page number's results $userselect = mysql_query("SELECT * FROM users LIMIT $from, $max_results"); $num = mysql_num_rows($userselect); print(" <TABLE width=100% heigth=100% bgcolor=#$xpagedocol cellspacing=0> <TR> <TD align=left><font color=#FFFFFF> <img src=favicon.ico> <b>$sitename Memberlist.</b></font></TD> </TR> </TABLE> <BR> <BR> <center> <table width=95% bordercolor='#$xpagedocol' border='2'> <tr> <td class='content'><b>Nickname</b></td> <td class='content'><b>Local</b></td> <td class='content'><b>Gender</b></td> </tr> "); if($num==0) { print("<tr><td class='content' colspan=10>There are 0 users that start with the letter/number $letter</td></tr>"); } while($row = mysql_fetch_array($userselect)) { $pronickx = $row[nickname]; $pronickp = bin2hex($row[nickname]); if ($row[pro_pic] == "") { $memberpic = 'images/nophotobig.gif'; } if ($row[pro_pic] != "") { $memberpic = $row[pro_pic]; } print(" <tr> <td class='content'> <img src='$memberpic' border=0 width=70 height=70> <A href='http://domain/rated.php?epuid=$pronickp'target='_blank'>$pronickx</a></td> <td class='content'>$row[local]</td> <td class='content'> "); print ("$row[gender]"); print (" </td> </tr> "); } print("</table></center><br> <TABLE width=100% heigth=100% bgcolor=#$xpagedocol cellspacing=0> <TR> <TD align=left><font color=#FFFFFF>  </font></TD> </TR> </TABLE> "); // Figure out the total number of results in DB: $total_results = mysql_result(mysql_query("SELECT COUNT(*) as Num FROM users"),0); // Figure out the total number of pages. Always round up using ceil() $total_pages = ceil($total_results / $max_results); // Build Page Number Hyperlinks echo "<center>Select a Page<br />"; // Build Previous Link if($page > 1){ $prev = ($page - 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$prev\">Previous</a> "; } for($i = 1; $i <= $total_pages; $i++){ if(($page) == $i){ echo "$i "; } else { echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$i\">$i</a> "; } } // Build Next Link if($page < $total_pages){ $next = ($page + 1); echo "<a href=\"".$_SERVER['PHP_SELF']."?page=$next\">Next</a>"; } echo "</center>"; ?></td> <td> </td> </tr> <tr> <td> </td> <td> </td> </tr> </table> Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685027 Share on other sites More sharing options...
NSW42 Posted November 8, 2008 Author Share Posted November 8, 2008 just bumping before sleep land Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685310 Share on other sites More sharing options...
NSW42 Posted November 8, 2008 Author Share Posted November 8, 2008 just 1 more awake bump Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-685643 Share on other sites More sharing options...
corbin Posted November 13, 2008 Share Posted November 13, 2008 (Just so no one yells at me for bumping a 4 day old post, NSW42 asked me to help.) Well, the basic concept is this: I'm on page x, and I need to see a number of results, y. The results I see need to be based on x and y. Let's say x is 3 and y is 10 (10 results per page, 3rd page): (x-1)y+1 through xy 21 - 30 in this case. Page 1 would be 1-10 (which would actually be 0-9 in array land) Page 2: 11-20 Page 3: 21-30 So on.... So, in PHP land, the offsets would be: (page-1)*y through page*y-1 (20 - 29 on page 3) So, assuming we don't care from where $list is coming in this example: <?php $list = array(); //pretend this has crap in it $per_page = 10; $page = (isset($_GET['page']) && ((int) $_GET['page']) >= 1) ? ((int) $_GET['page']) : 1; $list_count = count($list); $max_page = ceil($list_count/$per_page); if($page > $max_page) { $page = $max_page; //you could flag an error or what ever you wanted to do here } $min = ($page-1)*per_page; //(x-1)y //xy-1 $max = $page*per_page-1 if($max > $list_count-1) { $max = $list_count-1; } //show the stuff for($i = $min; $i < $max; ++$min) { echo $list[$i]; } //show the nav if($page > 1) { $tmp = $page - 1; echo '<a href="?page='.$tmp.'"><-- Previous</a>'; //could technically just do .($page-1). but I think a variable looks better and isn't a significant performance hit lol } else { echo '<-- Previous'; //no previous pages, so no link. } echo ' | '; //separator for the links if($page < $max_page) { echo '<a href="?page='.$tmp.'">Next --></a>'; } else { echo 'Next -->'; } Quote Link to comment https://forums.phpfreaks.com/topic/131860-pagination-with-no-mysql/#findComment-688975 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.