Jim R Posted February 10, 2010 Share Posted February 10, 2010 I'm trying to echo Posts about basketball games (Games) on my home page. I'm using WordPress, and Games is a tag I use on my site. Game 1 Post 1 Post 2 Game 2 Post 1 Post 2 Post 3 Right now the code I'm working with, which I've had help on, just echos the Games in the order of its most recent post. $mf_query = <<<EOF SELECT t.*, tt.*, p.* FROM {$wpdb->posts} as p, {$wpdb->term_relationships} as tr, {$wpdb->terms} AS t, {$wpdb->term_taxonomy} AS tt WHERE tt.`taxonomy` = 'series' AND t.`term_id` = tt.`term_id` AND tr.`object_id` = p.`ID` AND tt.`term_taxonomy_id` = tr.`term_taxonomy_id` AND tt.`count` > 0 GROUP BY t.`term_id` ORDER BY p.`post_date` DESC LIMIT 5 EOF; $mf_terms = $wpdb->get_results( $mf_query ); if( !empty( $mf_terms ) ) { print "\n\t" . '<ul>'; foreach ( $mf_terms as $mf_term ) { print "\n\t\t" . '<li><a href="' . get_term_link( $mf_term, $mf_term->taxonomy ) . '">' . $mf_term->name . '</a></li>'; } print "\n\t" . '</ul>'; } I'm assuming I can use nested Foreach loops or a While within a Foreach. My question is, how do I use the same query? It's already querying all Post information. Thanks. Quote Link to comment https://forums.phpfreaks.com/topic/191704-foreach-vs-while-nested-question/ Share on other sites More sharing options...
sader Posted February 10, 2010 Share Posted February 10, 2010 Not sure I understand u correctly but... Let's say u wont print query result into diferent page areas and y don't want to to send same query again and retrive same results so here's first method I would use //connection $result = mysql_query($sql); echo "<p>"; while($row = mysql_fetch_assoc($result)) { echo $row['title']; } echo "</p>"; //now back to result starting point mysql_data_seek($result, 0); echo "<p>"; while($row = mysql_fetch_assoc($result)) { echo $row['title']; } echo "</p>"; mysql_free_result($result); or u can first put all results into some temporary array and then use that array over and over again //connection $temp = array(); $result = mysql_query($sql); //collect data and store ir into temp array while($row = mysql_fetch_assoc($result)) { $temp[] = $row; } echo "<p>"; forach($temp as $r) { echo $r['title']; } echo "</p>"; //print again from the begining echo "<p>"; forach($temp as $r) { echo $r['title']; } echo "</p>"; Quote Link to comment https://forums.phpfreaks.com/topic/191704-foreach-vs-while-nested-question/#findComment-1010444 Share on other sites More sharing options...
Jim R Posted February 10, 2010 Author Share Posted February 10, 2010 I didn't explain it as well as I should have. The query is echo'ing the Games based on the date of the Posts. I would also like it to echo the applicable Posts' titles underneath each Game title. I'm not sure how to use the query to use the Post information, in this case the title of each Post. Quote Link to comment https://forums.phpfreaks.com/topic/191704-foreach-vs-while-nested-question/#findComment-1010450 Share on other sites More sharing options...
Jim R Posted February 11, 2010 Author Share Posted February 11, 2010 I need to print one within the other. I'm not sure how to get that done. Quote Link to comment https://forums.phpfreaks.com/topic/191704-foreach-vs-while-nested-question/#findComment-1010537 Share on other sites More sharing options...
Jim R Posted February 12, 2010 Author Share Posted February 12, 2010 I still need help with this. Quote Link to comment https://forums.phpfreaks.com/topic/191704-foreach-vs-while-nested-question/#findComment-1011154 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.