desjardins2010 Posted December 13, 2010 Share Posted December 13, 2010 Hey All? so I have thsi script in place that returns the last 5 forum post based on time, however I have also set them to an array returning all 5 results I want to be ablet to plug the first on in somewhere then plug the second somewhere and so forth... I tried to use an echo $topic_name but thats shoots out all 5 results.. any ideas? is there a way to use echo and limit the output then use it again and it would start at the second one? <?php include("connect.php"); mysql_select_db("heaven_forum"); $recent = mysql_query("SELECT * FROM phpbb_posts ORDER BY post_time DESC LIMIT 5"); while ($recent_row = mysql_fetch_assoc($recent)) { //get data $post_id = $recent_row['post_id']; $topic_id = $recent_row['topic_id']; $forum_id = $recent_row['forum_id']; $poster_id = $recent_row['poster_id']; $post_time = $recent_row['post_time']; //get topic name $topic_name = mysql_query("SELECT topic_title FROM phpbb_topics WHERE topic_id='$topic_id'"); $topic_name = mysql_fetch_assoc($topic_name); $topic_name = $topic_name['topic_title']; //get username $username = mysql_query("SELECT username FROM phpbb_users WHERE user_id='$poster_id'"); $username = mysql_fetch_assoc($username); $username = $username['username']; echo $topic_name; echo "Posted By: $username"; } ?>[code=php:0] Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/ Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 First, I'd look at trying to reduce this to a single query using JOIN's. Forums should be highly trafficked so I imagine performance will play a major part. On your problem, though I would advise against getting comfortable with these 3 queries, is to use arrays (or objects). if you don't know how to use arrays this might be hard to grasp. For each row - each while() you are doing, you need to store all of that data in between into an array. So, for example, to do that with the first variable: $n=0; while ($recent_row = mysql_fetch_assoc($recent)) { $n++; //get data $forumPosts[$n]['post_id'] = $recent_row['post_id']; Then you can try var_dump() or print_r() to check the output. You can replace $n with whatever key you like but it must be unique to avoid conflict. If you just do that above and print out the array you will see the 5 posts contained within an array with the id set for each. You can then refer to each individual array using: echo $forumPosts[1]['post_id']; echo $forumPosts[1]['post_time']; etc Hope that helps. Again, try to compress this into a single query though. You're currently running about 11 queries in this small loop. Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/#findComment-1146436 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 [quote author=desjardins2010 link=topic=318075.msg1499736#msg1499736 date=1292200090] $recent = mysql_query("SELECT * FROM phpbb_posts ORDER BY post_time DESC LIMIT 5"); [code=php:0] [/quote] You only really need 1 query. [code=php:0]$recent = mysql_query( "SELECT phpbb_posts.id AS post_id, phpbb_posts.topic_id AS topic_id, phpbb_posts.forum_id AS forum_id, phpbb_posts.poster_id AS poster_id, phpbb_topics.topic_title AS title, phpbb_users.username AS username FROM phpbb_posts, phpbb_topics ON phpbb_posts.topic_id=phpbb_topics.id, users ON phpbb_posts.poster_id=users.id ORDER BY post_time DESC LIMIT 5"); Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/#findComment-1146466 Share on other sites More sharing options...
Anti-Moronic Posted December 13, 2010 Share Posted December 13, 2010 [quote author=desjardins2010 link=topic=318075.msg1499736#msg1499736 date=1292200090] $recent = mysql_query("SELECT * FROM phpbb_posts ORDER BY post_time DESC LIMIT 5"); [code=php:0] [/quote] You only really need 1 query. [code=php:0]$recent = mysql_query( "SELECT phpbb_posts.id AS post_id, phpbb_posts.topic_id AS topic_id, phpbb_posts.forum_id AS forum_id, phpbb_posts.poster_id AS poster_id, phpbb_topics.topic_title AS title, phpbb_users.username AS username FROM phpbb_posts, phpbb_topics ON phpbb_posts.topic_id=phpbb_topics.id, users ON phpbb_posts.poster_id=users.id ORDER BY post_time DESC LIMIT 5"); There's the magic sql not my strong point. I need to brush up on structure of these queries. Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/#findComment-1146468 Share on other sites More sharing options...
laffin Posted December 13, 2010 Share Posted December 13, 2010 LOL, I say the same thing, I got a lot of the basics down. prolly only know 20% of what mysql offers. Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/#findComment-1146471 Share on other sites More sharing options...
desjardins2010 Posted December 13, 2010 Author Share Posted December 13, 2010 Thanks alot guys, I think I can manage this now.. thats great I love this place btw! cheers Link to comment https://forums.phpfreaks.com/topic/221446-is-there-a-way-to-do-this/#findComment-1146670 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.