Welcome Guest, Not a member yet? Register   Sign In
Query with Explode() Array?
#1

[eluser]Aaron L.[/eluser]
Hello,

I have a field (let's call it "Groups") in which user ID#s are stored in a comma separated string:
Code:
1,3,23,299

I want to populate these users' names in a <select> field, but in order to do that I need to get their names from the users table.

I know it is possible to get an array of these ID variables using something like:
Code:
$array = explode(", ", $ids);

With this array, how do I query for these users' names in the separate 'users' table? I can't figure this step out, but it's probably simple. Thanks in advance for any advice you can give. :-)

- Aaron
#2

[eluser]Unknown[/eluser]
Try something like:
Code:
$myusers = "1,2,5,7,13";
$sql = "SELECT username, userid FROM users where userid IN (" . $myusers . ")";
$result = $this->db->query($sql);
Then make your select list from the result array of username/userid values.
#3

[eluser]Aaron L.[/eluser]
Thanks a ton! Your solution works beautifully.




Theme © iAndrew 2016 - Forum software by © MyBB