Im having trouble getting explode to work I have a table field named Attending with multiple user Ids separated with commas 73,1,5 right now i can easily get user 73 to echo out but need explode for the rest, I want it to echo out each username of those 3 users or however many it ends up being. I was thinking it might be something like what i commented out with the //
Attending Field is list of users http://imageshack.us/a/img38/1425/eventsne.jpg
Trying to Echo out like this once i get username working ill do the avatar and in a table http://imageshack.us/a/img819/8210/events2d.jpg
$Attending1 = array();
$Attending1 = mysql_query("SELECT * FROM Events, Users WHERE Events.Attending = Users.UserId");
//$AttendingUserIds = $Attending1['Attending'];
//$AttendingExploded = explode(",", $AttendingUserIds);
//$Attending3 = array();
//$Attending3 = mysql_query("SELECT * FROM Events, Users WHERE $AttendingExploded = Users.UserId");
while ($Attending2 = mysql_fetch_array($Attending1)) {
echo $Attending2['username'];
}
Just tryed KyleK 3rd suggestion
$Attending1 = array();
$Attending1 = mysql_query("SELECT * FROM Events, Users WHERE Events.Attending = Users.UserId");
$AttendingUserIds = $Attending1['Attending'];
//$AttendingExploded = explode(",", $AttendingUserIds);
$Attending3 = array();
$Attending3 = mysql_query("SELECT * FROM Events, Users WHERE Users.UserId IN ($AttendingUserIds)");
It gives me Warning: mysql_fetch_array(): supplied argument is not a valid MySQL result resource where the While starts.
You can do it all in just one SELECT
, by joining Events
and Users
tables thru the find_in_set()
function. That way, you will get all users which attended each event, because find_in_set()
will look up the first parameter in the CSV string supplied as the second parameter. You may also want to add a WHERE
to filter a specific event. And don't forget to replace the * with only the fields you need, to avoid unnecessary data traffic:
$Attending = array();
$Attending = mysql_query("
SELECT *
FROM Events e
INNER JOIN Users u ON find_in_set(u.UserId, e.Attending)
");
So if I understand you correctly you have values in a string that are comma seperated "73, 3, 5". So then just use WHERE IN
Your string of values from the database, however you gonna get it...
$stringofids = "73,3,3";//You will obviously retrieve these from database
Then just pass that string into another query, with a WHERE IN statement... That will return an array of all the users attending.
$AttendingUsers = mysql_query("SELECT * FROM Events, Users WHERE Users.UserId IN ($stringofids)");
while ($Attending = mysql_fetch_array($AttendingUsers)) {
echo $Attending['username'];
}
$Attending1 = mysql_query("SELECT * FROM Events, Users WHERE Events.Attending = Users.UserId");
while ($Attending2 = mysql_fetch_assoc($Attending1)) {
$arr[] = $Attending2['username'];
}
mysql_query
doesn't give you an array, fetch_array
(or fetch_assoc
) does.
Its result will be similar to this: $arr[0]['username'], $arr[1]['username'], $arr[2]['username']
, etc. I don't know how you want to "explode" so I can't answer, but after fetch_assoc
you should get an array.