Jaynesh Posted July 10, 2011 Share Posted July 10, 2011 Hello, how can I get two separate queries into one loop? I have tried this, but it produces both queries separatley. while( $i < $row = mysql_fetch_array($friendfeed_query2) OR $row_karma = mysql_fetch_array($karma_query))) Link to comment https://forums.phpfreaks.com/topic/241540-two-queries-in-one-loop/ Share on other sites More sharing options...
PaulRyan Posted July 10, 2011 Share Posted July 10, 2011 By showing us more code? We cannot help with what little you've given us. Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/241540-two-queries-in-one-loop/#findComment-1240712 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 while( $i < $row = mysql_fetch_array($friendfeed_query2) OR $row_karma = mysql_fetch_array($karma_query)) { if ($row['post'] == $user) { echo "output1"; } else if ($row_karma['post'] == $user) { echo "output2"; } } Hello, here is what I am trying to achieve. I have an if statement which I want to use using two different queries to echo a different output depending on the database data. I cannot use a UNION because my 2nd query contains a different amount columns. Link to comment https://forums.phpfreaks.com/topic/241540-two-queries-in-one-loop/#findComment-1240900 Share on other sites More sharing options...
PaulRyan Posted July 10, 2011 Share Posted July 10, 2011 What is your query you're are using? Would it not be possible to do a LEFT JOIN? Regards, PaulRyan. Link to comment https://forums.phpfreaks.com/topic/241540-two-queries-in-one-loop/#findComment-1240951 Share on other sites More sharing options...
Jaynesh Posted July 10, 2011 Author Share Posted July 10, 2011 Hello $friendfeed2 = "SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers, dbFriends, dbKarma WHERE (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbFriends.user_id = $user AND dbFriends.friend_id = dbPosts.username_id AND dbFriends.status = 1) OR (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbFriends.friend_id = $user AND dbFriends.user_id = dbPosts.username_id AND dbFriends.status = 1) UNION SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers, dbPosts_share, dbFriends, dbKarma WHERE (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbPosts_share.user_id = dbFriends.friend_id AND dbFriends.user_id = $user AND dbPosts_share.post_id = dbPosts.post_id AND dbFriends.status = 1) OR (dbPosts.post_id NOT IN (SELECT post_id FROM dbDownvote WHERE user_id = $user) AND dbPosts.username_id = dbUsers.id AND dbPosts_share.user_id = dbFriends.user_id AND dbFriends.friend_id = $user AND dbPosts_share.post_id = dbPosts.post_id AND dbFriends.status = 1) UNION SELECT dbUsers.id, dbPosts.post, dbUsers.username, dbPosts.post_id, dbPosts.karma_up, dbPosts.karma_down FROM dbPosts, dbUsers, dbKarma WHERE dbPosts.username_id = dbUsers.id AND username_id = $user"; $friendfeed_query2 = mysql_query($friendfeed2); $karma = " SELECT DISTINCT dbPosts.post, dbKarma.karma_id, dbPosts.post_id FROM dbPosts, dbUsers, dbKarma WHERE dbPosts.username_id = dbUsers.id AND dbKarma.post_id = dbPosts.post_id"; $karma_query = mysql_query($karma); My queries are a little more complicated, the php code I gave you earlier was just a simplified example as not to confuse you. Here is the actual code. Ive highlighted the bit in red for what im trying to achieve. It however produces BOTH variables. while( $i < $row = mysql_fetch_array($friendfeed_query2) OR $row_karma = mysql_fetch_array($karma_query)) { $postid = $row_karma['post_id']; $karmaid_db = $row_karma['karma_id']; $karmaid = $user ."/". $postid; if ($row['id'] == $user) { echo $div_mypostbox; } elseif ($karmaid == $karmaid_db) { echo $div_postbox_vote; } else { echo $div_postbox; } echo $karmaid_db; $karma_up = $row['karma_up']; $karma_down = $row ['karma_down']; $karma_total = $karma_up - $karma_down; echo "<a href=vote.php?up=" . $row['post_id'] . "><</a>"; echo $karma_total; echo "<a href=vote.php?down=" . $row['post_id'] . ">></a>"; echo $break; $post = $row['post']; $post_wrap = wordwrap($post, 50, "\n", true); echo "$post_wrap"; echo $div_end; echo $div_postfooter; echo "Submitted by " . $row['username'] . "<br>"; echo $post_id; if ($row['id'] == $user) { echo "<a href=delete.php?id=" . $row['post_id'] . ">delete</a><br>"; } echo $div_end, $div_end, $div_end, $break; } Link to comment https://forums.phpfreaks.com/topic/241540-two-queries-in-one-loop/#findComment-1240959 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.