doddsey_65 Posted January 16, 2011 Share Posted January 16, 2011 I am trying to pull the username for everyone who appears within an array. How would i go about this? something like this maybe? SELECT * FROM users WHERE username (in_array($users)) Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/ Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 $usersql=mysql_query("SELECT * FROM users"); while ($row=mysql_fetch_array($usersql)) { $user=$row['username']; if(in_array($user, $users)) { echo $username; } } Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160175 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 i did that but it only echoes one username: $user_query = mysqli_query($link, "SELECT user_username, user_avatar, user_regdate FROM ".TBL_PREFIX."members ") or die(mysqli_error($link)); while($user_info = mysqli_fetch_array($user_query, MYSQLI_ASSOC)) { if (in_array($user_info['user_username'], $likes)) { $user_name .= profile_link("", $user_info['user_username'], ""); $user_avatar .= "<img src=\"./avatars/{$user_info['user_avatar']}\" alt=\"avatar\" />"; $user_reg_date .= asf_date($user_info['user_regdate'], 1); } } Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160177 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 how did you generate the array $likes, the results would be case sensitive, so maybe only one matches? Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160178 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 $likes = explode(", ", $p_name_info['post_likes']); post_likes is a db table with content like user1, user2, user3 there is no case sensitive issue but to be safe i added strtolower on the array and database field but still only shows one result Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160179 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 what happens if you echo out in the if statement for testing purposes, does it display more than 1 then? Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160180 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 when i echo it displays all of the results. but i wanted it in a variable because it needs to be called later. Also all of the results display on the same line if i echo them and i need them in individual table tds Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160182 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 you could create an array for each of those results in the if statement and the use foreach to spit them out where needed above the while statement have $user_name=array(); $user_avatar=array(); $user_reg_date=array(); then in the if statement $user_name[]= profile_link("", $user_info['user_username'], ""); $user_avatar[]= "<img src=\"./avatars/{$user_info['user_avatar']}\" alt=\"avatar\" />"; $user_reg_date[]= asf_date($user_info['user_regdate'], 1); Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160186 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 i tried the array but dont know how to go about the foreach loop Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160187 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 can you show an example line of how you would display the data so I can see how you want to set it out when you are echoing the results, it really depends on how you want to display it Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160192 Share on other sites More sharing options...
doddsey_65 Posted January 16, 2011 Author Share Posted January 16, 2011 in pseudocode: <table> <tr> <td>$user_avatar</td> <td><p>$user_name</p><p>$user_reg_date</p></td> </tr> </table> Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160193 Share on other sites More sharing options...
dragon_sa Posted January 16, 2011 Share Posted January 16, 2011 <table> <?php $num=(count($user_name)-1); for ($i=0;$i<=$num;$i++) { echo "<tr>\n"; echo "<td>$user_avatar[$i]</td>"; echo "<td><p><?php echo $user_name[$i]</p><p>$user_reg_date[$i]</p></td>\n"; echo "</tr>\n"; } ?> </table> something like that should work Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160206 Share on other sites More sharing options...
trq Posted January 16, 2011 Share Posted January 16, 2011 $sql = "SELECT * FROM users WHERE username IN('" . implode("','", $users) . "')"; Link to comment https://forums.phpfreaks.com/topic/224595-in_array-within-query/#findComment-1160255 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.