Jump to content

Recommended Posts

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  ;D

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/55616-pagination-with-loop/
Share on other sites

$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

Link to comment
https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274815
Share on other sites

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  ;D

 

I hope that clears it up,

 

~ Chocopi

Link to comment
https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274823
Share on other sites

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

Link to comment
https://forums.phpfreaks.com/topic/55616-pagination-with-loop/#findComment-274830
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.