GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 19-Oct-2005, 00:55
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road

Viewing data in multiple groups in PHP


I'm working on developing a forum system for one of my newer websites. I have all of the tables that I will need for this, as far as database linking is concerned, and managing the forums with the user database. However, I've run into a road block.

Almost the only thing I haven't worked with, using PHP and MySQL, is viewing data in multiple groups. I don't want a forum topic page to list all 1,000 posts that it has in it, rather listing them in groups of 20 and allowing the user to browse their way through the groups. This is how most professional forum systems work, including this one I assume. I haven't done this type of thing with any of my websites before.

Can anyone point me in the right direction to get started?
  #2  
Old 19-Oct-2005, 02:22
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Try with SQL_CALC_FOUND_ROWS


You need to set the max number of posts per page. Say you set it at 10...

Assuming you have 2 basic tables; 1. `threads` 2. `posts`

Here's an example forum show-thread script with the basic but relevant bits to help you get started. See the comments within the script and also note the SQL_CALC_FOUND_ROWS keyword in the first SQL statement.

PHP Code:

<?php

// FILENAME: SHOW-THREAD.PHP

// include_once( "../config.inc.php" );
// the max number of posts per thread was set in the config file perhaps
// e.g. $config['max_posts_per_thread'] = 10;

// A single thread ID may have multiple pages, we check to see if the multipage
// ID (p) is set. e.g. http://www.example.com/forum/show-thread.php?t=123&p=2
$page   = 1;
if( isset($_REQUEST['p']) )
    $page = intval( $_REQUEST['p'] ); // 2
if( $page<1 )
    trigger_error( "Invalid Page ID.  Halting Script...", E_USER_ERROR );
// page 1 starts at offset 0; page 2 starts at offset 10, page 3 starts at offset 20, etc.
$offset = $page * $config['max_posts_per_thread'] - $config['max_posts_per_thread'];  

// screen the thread ID i.e. "t=123"
$thread = 0;
if( isset($_REQUEST['t']) )
    $thread = intval( $_REQUEST['t'] ); // 123
if( !$thread )
    trigger_error( "Invalid Thread ID.  Halting Script...", E_USER_ERROR );

// get the posts
$sql   =  "SELECT SQL_CALC_FOUND_ROWS
          `threads`.`title` AS `thread_title`,
          `posts`.`title`, `posts`.`message`, `posts`.`userid`, `posts`.`post_timestamp`
          FROM `posts`
          LEFT JOIN `threads`
            ON `posts`.`thread_id` = `threads`.`id`
          WHERE `posts`.`thread_id`=$thread
          ORDER BY `posts`.`thread_id`, `posts`.`post_timestamp`
          LIMIT $offset, {$config['max_posts_per_thread']}";
$result = mysql_query( $sql, $conn );
while( $row=mysql_fetch_assoc($result) )
{
    // process the posts to be displayed.    
}
            
// to link to the other multipages off this thread, you need the TOTAL number of posts.
$sql         = "SELECT FOUND_ROWS()";
$result      = mysql_unbuffered_query( $sql, $conn );
$row         = mysql_fetch_row( $result );
$total_posts = $row[0]; // if there are 1002 posts in thread ID 123, $total_posts will now be set at 1002.

// do the necessary math to display the multipage links.
$total_pages    = ceil( $total_posts / $config['max_posts_per_thread'] ); // ceil( 1002/10 ) == 101 pages of 10 posts each
?>


Please understand that this example code is just that -- an example. I didn't like test it or anything like that... and may contain errors/typos...
  #3  
Old 19-Oct-2005, 16:03
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road

Re: Viewing data in multiple groups in PHP


Thanks!

I have formatted the code for my site and the offset limiter for the pages seems to work great. I'm having a little trouble doing the multipage links, however. Do you think you could go more indepth into that possibly?

[EDIT:] Also, I was having problems returning a value from the FOUND_ROWS() clause. Can it only be used if the previous SQL_CALC_FOUND_ROWS statement is a UNION? You used a statement in the first sql line as LEFT JOIN and ON. This is something I had to leave out in my first line as it was returning errors.
  #4  
Old 20-Oct-2005, 15:50
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road
Exclamation

Re: Viewing data in multiple groups in PHP


Another problem I was having with this is the SQL_CALC_FOUND_ROWS....it seems to always return a SQL syntax error.

PHP Code:

$sql = "SELECT SQL_CALC_FOUND_ROWS * FROM forum_thread LEFT JOIN forum_assoc ON forum_thread.assoc_forum_id = forum_assoc.id WHERE forum_thread.assoc_forum_id = '$forum' LIMIT '$offset', {$config['max_threads_per_forum']}"; 



The SQL statement returns:

You have an error in your SQL syntax near 'FROM forum_thread JOIN forum_assoc ON forum_thread.assoc_forum_id = forum_assoc.' at line 1

forum_thread.assoc_forum_id = the forum number where the thread belongs
forum_assoc.id = the id of the forum that the thread/forum id is supposed to match.

This syntax is similar to the SQL that you previously posted, with alterations to make it work for my table structure. Any idea why it returns these errors?
  #5  
Old 20-Oct-2005, 20:06
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Viewing data in multiple groups in PHP


I am sorry I missed replying to your previous post but now I am not sure if you still require help with the multi-page links. But I'll include a very simple example just for the people reading this thread sometime in the near future...

PHP Code:

<?php

// see example code from previous post above...

// do the necessary math to display the multipage links.
$total_pages    = ceil( $total_posts / $config['max_posts_per_thread'] ); // ceil( 1002/10 ) == 101 pages of 10 posts each

// dislay the multipage links...
$mp_links = "<div class=\"mplinks\">";
for( $i=1; $i<=$total_pages; $i++ )
{
  if( $page != $i )
  {
    if( $i != 1 )
    {
       $mp_links .= "<a href=\"$_SERVER[PHP_SELF]?t=$thread&amp;p=$i\">$i</a> | ";
    }
    else
    { // we drop the "&amp;p=1" since the first page is always "/show-thread.php?t=nnn" only.
       $mp_links .= "<a href=\"$_SERVER[PHP_SELF]?t=$thread\">$i</a> | ";
    }
  }
  else
  { // don't need a link here, since we're already viewing this page.
    $mp_links .= "$i | ";
  }
}
// remove the trailing " | " and close the div.
$mp_links = rtrim( $mp_links, " |" ) . "</div>";

// etc...
?>


Frankly, I have never used UNION before. I mean I know what it's supposed to do but I never really needed to use it. Since it always means joining two SELECT statements, I suppose you need to insert SQL_CALC_FOUND_ROWS into both e.g. SELECT SQL_CALC_FOUND_ROWS * ... UNION SELECT SQL_CALC_FOUND_ROWS * ....
  #6  
Old 20-Oct-2005, 20:13
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Viewing data in multiple groups in PHP


Quote:
Originally Posted by fallen_samurai
Another problem I was having with this is the SQL_CALC_FOUND_ROWS....it seems to always return a SQL syntax error. ..

At a glance, your SQL statement seems valid and I am not sure what is wrong. If in doubt, please test it out with a simple SQL e.g.

PHP Code:

$sql = "SELECT
        SQL_CALC_FOUND_ROWS
        *
        FROM `forum_thread`
        LIMIT 0, 3"; 


  #7  
Old 20-Oct-2005, 20:47
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road
Lightbulb

Re: Viewing data in multiple groups in PHP


You have an error in your SQL syntax near 'FROM forum_thread LIMIT 0, 3' at line 1

It seems that I can't use SQL_CALC_FONUD_ROWS at all. For some reason it does not recognize it. Instead of using those, I could do a simple COUNT(*) statement with the same limits as the first statement, and I believe that would return how many rows matched my parameters. What do you think?
  #8  
Old 20-Oct-2005, 20:59
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Re: Viewing data in multiple groups in PHP


What do I think? I think it's okay for you to use any method that works.

Also, if I were you, I would want to find out why I cannot use SQL_CALC_FOUND_ROWS! e.g. What is the version of the MySQL server and client?

If you figure out what is wrong eventually, please update this thread with the information so you can help save someone else some time looking for the solution.
  #9  
Old 20-Oct-2005, 21:24
fallen_samurai fallen_samurai is offline
Awaiting Email Confirmation
 
Join Date: Oct 2005
Location: Wisconsin, USA
Posts: 29
fallen_samurai is on a distinguished road

Re: Viewing data in multiple groups in PHP


MySQL version is....3.23.54

[EDIT] Seeing that my version in 3.23, I read on a site about 2 seconds before this edit that the SQL_CALC_FOUND_ROWS comes with version 4.0.

I am going to try to update and see if that solves the problem. Are there any risks to updating MySQL on a server that has established and much needed databases?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
[Include] Doubly-linked List dsmith C Programming Language 6 14-Apr-2006 14:12
[CONTEST?]Data Structure Test dsmith C Programming Language 2 06-Jun-2004 16:13
Grouping data from MySQL with PHP - Newbie question. giobbi MySQL / PHP Forum 12 27-Feb-2004 01:34
Automate a data change php form mjfmn MySQL / PHP Forum 4 20-Oct-2003 10:37

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 09:37.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.