Spreading MySQL Result across multiple pages... help

This forum is now closed as part of retiring phpBB2.
Forum rules
READ: phpBB.com Board-Wide Rules and Regulations

This forum is now closed due to phpBB2.0 being retired.
Post Reply
Been Told
Registered User
Posts: 90
Joined: Sat May 07, 2005 11:26 pm

Spreading MySQL Result across multiple pages... help

Post by Been Told »

Hi there. Okay, this is probably a very stupid question, but I'm a noob when it comes to databases and dealing with MySQL etc.
I am trying to learn a little by simply making the following:
A page that will query the database for all smilies. It will display 3 smilies per row and 5 rows per page.
Now, I checked the documentation and found a great code-snippet to query the DB, but I still can't figure out how to make it stop at 5 rows.

Code: Select all

<?php 

// standard hack prevent 
define('IN_PHPBB', true); 
$phpbb_root_path = './'; 
include($phpbb_root_path . 'extension.inc'); 
include($phpbb_root_path . 'common.'.$phpEx); 

// standard session management 
$userdata = session_pagestart($user_ip, PAGE_klaus); 
init_userprefs($userdata); 

// set page title 
$page_title = 'klaus'; 

$columns_per_row = 3;  // or whatever you want
// create and execute our database query....
$sql = "SELECT * FROM phpbb_smilies";
if ( !($result = $db->sql_query($sql)))
{
  message_die(GENERAL_ERROR, "Could not query user table.", '', __LINE__, __FILE__, $sql);
}

// now here comes the interesting stuff
$i = 0;
while ( $row = $db->sql_fetchrow($result) )
{ if ($row == 5) { exit; }

	 if ( !($i % $columns_per_row) ) // modulus kicks butt
  {
    $template->assign_block_vars('row',array());
  } 


  $template->assign_block_vars('row.cell', array('CONTENT' => $row['smile_url']));

  $i++;
}

// standard page header 
include($phpbb_root_path . 'includes/page_header.'.$phpEx); 

// assign template 
$template->set_filenames(array( 
        'body' => 'klaus.tpl') 
); 


$template->pparse('body'); 

// standard page footer 
include($phpbb_root_path . 'includes/page_tail.'.$phpEx); 

?>
I tried the following in the while loop:

Code: Select all

while ( $row = $db->sql_fetchrow($result) )
{ if ($row == 5) { exit; }

	 if ( !($i % $columns_per_row) ) // modulus kicks butt
  {
    $template->assign_block_vars('row',array());
  } 


  $template->assign_block_vars('row.cell', array('CONTENT' => $row['smile_url']));

  $i++;
  if ($i > 14) { exit ; }
}
But that gives me a blank page.

Here's what I use in the tpl file to display the smilies:

Code: Select all

<center>
<!-- BEGIN row -->
<table width="500">
 <tr>
  <!-- BEGIN cell -->
   <td align="center" width="80"><img src="images/smiles/{row.cell.CONTENT}" /></td>
  <!-- END cell -->
 </tr>
</table>
<!-- END row --> 
</center>
As I said, I'm just doing this to learn how to do that sort of thing... so there wouldn't be a point in saying stuff like "Oh, why would you want to do that?".
I really appreciate any kind of help you can give me. :)
Thanks in advance.
Post Reply

Return to “[2.0.x] MOD Writers Discussion”