tomwi Posted December 30, 2008 Share Posted December 30, 2008 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; ?> Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/ Share on other sites More sharing options...
dennismonsewicz Posted December 30, 2008 Share Posted December 30, 2008 I am assuming you are pulling all of this data out of a DB and in that case look into the LIMIT statement in MySQL Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726260 Share on other sites More sharing options...
tomwi Posted December 30, 2008 Author Share Posted December 30, 2008 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'); ?> — <?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> — <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(); ?> Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726266 Share on other sites More sharing options...
Maq Posted December 30, 2008 Share Posted December 30, 2008 Anyway, how is $topics created? And what are all these functions! endif; ??? Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726518 Share on other sites More sharing options...
RussellReal Posted December 31, 2008 Share Posted December 31, 2008 this isn't visual basic.. php uses { and } not THEN and ENDIF just FYI Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726713 Share on other sites More sharing options...
Maq Posted December 31, 2008 Share Posted December 31, 2008 I don't even know how his code runs with that kind of syntax... Put this at the top: ini_set ("display_errors", "1"); error_reporting(E_ALL); Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726947 Share on other sites More sharing options...
premiso Posted December 31, 2008 Share Posted December 31, 2008 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. Quote Link to comment https://forums.phpfreaks.com/topic/138888-limit-this-loop/#findComment-726948 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.