chocopi Posted June 14, 2007 Share Posted June 14, 2007 I would like to add a pagination on to my site, but because I already loop through my database results I have no idea where to put the code. I was using the script in the tutorials section and it works great for just getting the results but with my loop I do not know how to impliment it. This code selects the messages from the database where the board equals $board and then it loops through, so where would I add the loop ?? Here is the part of my code <?php // start session session_start(); require_once('page_header.php'); // set sessions to variables $id = $_SESSION['id']; $username = $_SESSION['username']; // set board id $board = '1'; // get max post_num $query = mysql_query("SELECT MAX(post_num) AS `post_num_max` FROM `zBoard_messages` WHERE board_id='$board'") or die(mysql_error()); $fetch = mysql_fetch_assoc($query) or die(mysql_error()); $post_num_max = $fetch['post_num_max']; for ($pn = 1; $pn <= $post_num_max; $pn++) { // select message id's from db $query = mysql_query("SELECT `id`,`poster_id`, `subject`, `message`, `date` FROM `zBoard_messages` WHERE board_id='$board' && post_num='$pn'") or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); // set db results to variables $id = $row['id']; $poster_id = $row['poster_id']; $subject = $row['subject']; $text = $row['message']; $date = $row['date']; // get username $query = mysql_query("SELECT username FROM `zBoard_users` WHERE id='$poster_id'") or die(mysql_error()); $row = mysql_fetch_assoc($query) or die(mysql_error()); $poster = $row['username']; // convert message into viewer message require('bbcode.php'); // get legible date list ($date,$time) = explode(' ',$date); list ($year,$month,$day) = explode('-',$date); $date = "$day/$month/$year"; $text = str_replace('<br /><br /><br />','',$text); $text = str_replace('\r\n','<br />',$text); //$text = nl2br(str_replace('\r\n',"\r\n",$text)); $text = stripslashes($text); // display contents // here in html } require('page_footer.php'); ?> Cheers ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/55616-pagination-with-loop/ Share on other sites More sharing options...
Full-Demon Posted June 14, 2007 Share Posted June 14, 2007 $row = mysql_fetch_assoc($query) or die(mysql_error()); This function works like this: when you call it it will return the first row, when you call it again it will return the second row...third row....fourth etc etc etc You dont need to get the post_num max! You need to select everything you want to loop through and do this: $query = [some selection query]; while ($row = mysql_fetch_assoc($query)) { [extract the values here] } Exactly how I explained it to you some days ago! Btw, if you select only one row, put LIMIT 1 on the end of the query = faster. Now, explain me what you want to display with 'pagination'? As I dont really get it.... FD Quote Link to comment https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274815 Share on other sites More sharing options...
chocopi Posted June 14, 2007 Author Share Posted June 14, 2007 well Firstly, i use post_num_max because in my database I store all of the messages in one table. The messages then have a column which determines which board they will be placed in. I use the post_num_max to find what the highest id (no matter what board it is on) is of that specific board, so I then loop until all posts have been displayed. So all the messages are displayed one after another according to there id. So I would like the pagination so I can split the messages up on to different pages. So basically I am building a mini-forum which displays posts and will use the pagination so I can span the messages over multiple pages I hope that clears it up, ~ Chocopi Quote Link to comment https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274823 Share on other sites More sharing options...
Full-Demon Posted June 14, 2007 Share Posted June 14, 2007 Ah, thanks. Still I wouldnt use MAX, but my approach. As some ID's might be missing! Your highest ID might not be identical to the number of rows. For pagination, use the LIMIT (begin row), (number of rows) function in the query to select a certain ammount of posts. You have a value in the url (index.php?page=1), which is when not set 1. You use the LIMIT function + $_GET['page'] to determine which rows you have to select. IE: LIMIT ".($_GET['page']*$posts_per_page).", ".$posts_per_page." You really should use my method of looping through the table, as it requires less querys. Also, perhaps you want to order the query based on the time the post is posted? ORDER date FD Quote Link to comment https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274830 Share on other sites More sharing options...
chocopi Posted June 15, 2007 Author Share Posted June 15, 2007 ok but how can i add to my code, so i replace one of my queries with it, also, i would rather not use GET at the moment and time being as the naff free webhost messes up urls Quote Link to comment https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-275275 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.