Jump to content

limit this loop


tomwi

Recommended Posts

good afternoon all!  Can someone tell me how to add a limit to this?  Currently pulls all the latest topics, want to limit it to say 5 or 7.

 

Thanks!

Tom

 

<?php if ( $topics ) : foreach ( $topics as $topic ) : ?>
	<tr<?php topic_class(); ?>>
		<td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>
		<td class="num"><?php topic_posts(); ?></td>
		<td class="num"><?php topic_last_poster(); ?></td>
		<td class="num"><small><?php topic_time(); ?></small></td>
	</tr>
	<?php endforeach; endif; ?>

Link to comment
https://forums.phpfreaks.com/topic/138888-limit-this-loop/
Share on other sites

correct, and thanks for the quick reply.

 

I am not good at php, so asking for help here.  I am familiar with editing the mysql call by adding the LIMIT, but I was confused because I didn't see that in this file.  can that be located in another file, it seemed like this was the only code in this file.

 

full code of this file is below: (sorry for the big code box)

<?php bb_get_header(); ?>

<?php if ( $forums ) : ?>

<div id="contentleft">

<div id="login">
	<?php login_form(); ?>
</div>

<div id="discussions">

	<?php if ( $topics || $super_stickies ) : ?>

	<h2><?php _e('Latest Discussions'); ?></h2>

	<table id="latest">
	<tr>
		<th><?php _e('Topic'); ?> &#8212; <?php new_topic(); ?></th>
		<th><?php _e('Posts'); ?></th>
		<th><?php _e('Last Poster'); ?></th>
		<th><?php _e('Freshness'); ?></th>
	</tr>

	<?php if ( $super_stickies ) : foreach ( $super_stickies as $topic ) : ?>
	<tr<?php topic_class(); ?>>
		<td><?php _e('Sticky:'); ?> <big><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></big></td>
		<td class="num"><?php topic_posts(); ?></td>
		<td class="num"><?php topic_last_poster(); ?></td>
		<td class="num"><small><?php topic_time(); ?></small></td>
	</tr>
	<?php endforeach; endif; ?>

	<?php if ( $topics ) : foreach ( $topics as $topic ) : ?>
	<tr<?php topic_class(); ?>>
		<td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>
		<td class="num"><?php topic_posts(); ?></td>
		<td class="num"><?php topic_last_poster(); ?></td>
		<td class="num"><small><?php topic_time(); ?></small></td>
	</tr>
	<?php endforeach; endif; ?>
	</table>
	<?php endif; ?>

	<h2><?php _e('Forums'); ?></h2>
	<table id="forumlist">

	<tr>
		<th><?php _e('Main Theme'); ?></th>
		<th><?php _e('Topics'); ?></th>
		<th><?php _e('Posts'); ?></th>
	</tr>

	<?php foreach ( $forums as $forum ) : ?>
	<tr<?php alt_class('forum'); ?>>
		<td><a href="<?php forum_link(); ?>"><?php forum_name(); ?></a> &#8212; <small><?php forum_description(); ?></small></td>
		<td class="num"><?php forum_topics(); ?></td>
		<td class="num"><?php forum_posts(); ?></td>
	</tr>
	<?php endforeach; ?>
	</table>

	<?php if ( $bb_current_user->ID ) : ?>

	<?php endif; else : // $forums ?>

	<div id="contentleft">

	<h3 class="bbcrumb"><a href="<?php bb_option('uri'); ?>"><?php bb_option('name'); ?></a></h3>

	<?php post_form(); ?>

	<?php endif; ?></div>

<h2><?php _e('Hot Tags'); ?></h2>
<div id="hottags">
	<?php bb_tag_heat_map(); ?>
</div>

<h2><?php _e('Search'); ?></h2>
<div id="search">
	 <?php search_form( $q ); ?>
</div>

</div>

<?php include 'sidebar.php'; ?>

<div style="clear:both;"></div>

<?php bb_get_footer(); ?>

Link to comment
https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726266
Share on other sites

this isn't visual basic.. php uses { and } not THEN and ENDIF just FYI

 

http://us2.php.net/manual/en/language.basic-syntax.php#80098

 

It is valid syntax for PHP. Just different and probably easier for users who come from Visual Basic.

 

Not for me though, I like the other way, it is way more efficient imo =)

 

Edit: Solution

 

To limit it without going deep into the files this would work:

 


<?php if ( $topics ) : $i=0; foreach ( $topics as $topic ) : ?>

<tr<?php if ($i == 5) : break; endif; $i++; topic_class(); ?>>

<td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>

<td class="num"><?php topic_posts(); ?></td>

<td class="num"><?php topic_last_poster(); ?></td>

<td class="num"><small><?php topic_time(); ?></small></td>

</tr>

<?php endforeach; endif; ?>

 

I am unsure if that will work with the syntax, if not this will work.

 

<?php if ( $topics ) {
        $i=0; 
         foreach ( $topics as $topic ) { ?>

<tr<?php if ($i == 5) { break; } $i++; topic_class(); ?>>

<td><a href="<?php topic_link(); ?>"><?php topic_title(); ?></a></td>

<td class="num"><?php topic_posts(); ?></td>

<td class="num"><?php topic_last_poster(); ?></td>

<td class="num"><small><?php topic_time(); ?></small></td>

</tr>

<?php } // end foreach 
   } // end if ?>

 

That should work if the first one does not.

Link to comment
https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726948
Share on other sites

Archived

This topic is now archived and is closed to further replies.

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