Jump to content
Larry Ullman's Book Forums

Recommended Posts

I am building a forum from the example in the book, and I am having trouble getting the page numbers to work like they should, right now the thread results don't update when you click on other page numbers, it is always the same query results on every page. 

Here is some code where I think the problem is, but I can't find it. I would post the whole page's code, but I have done that before and didn't get many helpers for that.

 

The query I am using is 

$sql = "SELECT t.thread_id, t.subject, u.username, COUNT(p.post_id) - 1 AS responses, MAX(DATE_FORMAT($last, '%e-%b-%y %l:%i %p')) AS last, MIN(DATE_FORMAT($first, '%e-%b-%y %l:%i %p')) AS first, w.forum_id FROM threads AS t INNER JOIN posts AS p USING (thread_id) INNER JOIN forums AS w ON t.forum_id = w.forum_id INNER JOIN users AS u ON t.user_id = u.user_id WHERE w.forum_id=$forumid AND t.lang_id = {$_SESSION['lid']} GROUP BY (p.thread_id) ORDER BY last DESC";

$query = mysqli_query ($dbc, $sql);

At the top of my forum.php page I am using this code to identify which forum the threads belong to.

if (isset($_GET['id']) && is_numeric($_GET['id'])){
	$forumid = $_GET['id'];
} elseif (isset($_POST['id']) && is_numeric($_POST['id'])) {
	$forumid = $_POST['id'];
} else {
	echo '<p class="error">This page has been accessed in error.</p>';
	include ('includes/footer.html');
	exit();
}

Here are the page links, modified to include the forum_id in the url.

if ($pages > 1){
	echo '<br /><p class="center4">';
	$current_page = ($start/$display) + 1;
	
	// If it's not the first page, make a Previous button:
	if ($current_page != 1) {
		echo '<a href="forum.php?s=' . ($start - $display) . '&p=' . $pages . '&id='.$forumid.'">Previous</a> ';
	}
	
	// Make all the numbered pages:
	for ($i = 1; $i <= $pages; $i++) {
		if ($i != $current_page) {
			echo '<a href="forum.php?s=' . (($display * ($i - 1))) . '&p=' . $pages . '&id='.$forumid.'">' . $i . '</a> ';
		} else {
			echo $i . ' ';
		}
	} // End of FOR loop.
	
	// If it's not the last page, make a Next button:
	if ($current_page != $pages) {
		echo '<a href="forum.php?s=' . ($start + $display) . '&p=' . $pages . '&id='.$forumid.'">Next</a>';
	}
	
	echo '</p>'; // Close the paragraph.
	
} // End of links section.

thanks for taking a look.

Share this post


Link to post
Share on other sites

I have about 12 results and I set the $display variable to 5 so I should get 5 results per page and 3 page numbers , but my page still displays 12 results on the first page, yet displays the 3 page numbers correctly.

I am pretty sure the problem is because of the sql query that has a Group By clause, it groups the threads and I think that is the problem, but if I take the Group By out, the results aren't complete. It's as if the page numbers aren't connecting to the sql query results. How could I fix this. 

Share this post


Link to post
Share on other sites

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.   Paste as plain text instead

  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.

Loading...
Sign in to follow this  

×
×
  • Create New...