Jump to content


Photo

For Loops


  • Please log in to reply
1 reply to this topic

#1 supanoob

supanoob
  • Members
  • PipPipPip
  • Advanced Member
  • 342 posts

Posted 22 June 2006 - 04:36 PM

right so the thing i want to do is like only show say 30 results then go onto a new page with the other reults.

what i have is:
$num_rows=mysql_num_rows($result3);


 for ($i=0;$i<$num_rows;$i++)
{
$row=mysql_fetch_array($result3);
$poster_name2=($row['poster_name']);
$poster_id2=($row['poster_id']);
$board2=($row['board']);
$post_date2=($row['post_date']);
$reply_to=($row['reply_to']);
$reply_body=($row['reply_body']);
$reply_subject=($row['reply_subject']);


        
        echo "<table width=\"729\" height=\"32\" border=\"1\">
                  <tr>
                    <td><span class=\"style6\">$reply_subject</span></td>
                  </tr>
               
                <br>
                
                   <tr>
                    <td width=153 height=\"28\"><p class=\"style3\">Posted By: $poster_name2($poster_id2)<br>
                      Time Of Post: $post_date2 <br>
                      Post Count: $postcount2 </p>
                    </td>
                    <td width=566>$reply_body</td>
                  </tr>
                </table>"; 
                }
                ?>

now i know i could use something like

if ($i == 5) { break; }

but someone told me that isnt the best way to do it, and it also wont show like Page 1 - 2 - 3 - 4 etc ate the bottom. any advice on how to improve it would be great.

#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 22 June 2006 - 04:44 PM

Add to you query (in the end) "LIMIT 30"- To limit the result to 30 rows.
Query for example: "SELECT * FROM `table` LIMIT 30". This will select the 30 first records.
But you can break it in an "ugly" way, adding in the end of the loop:
if($i==30){$i=$num_rows;};
But using the limit will be better.

For page splitting- just google. Here's an example:
[a href=\"http://www.php-mysql-tutorial.com/php-mysql-paging.php\" target=\"_blank\"]http://www.php-mysql-tutorial.com/php-mysql-paging.php[/a]

Orio.
Think you're smarty?

(Gone until 20 to November)




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users