Jump to content

Foreach vs while? Nested question...


Jim R

Recommended Posts

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.

Link to comment
Share on other sites

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>";


Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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