0

I have been trying to get data from exploded values, but I am failing miserably and I am completely clueless despite all the researching I've been doing. This is how the code looks like:

$array = explode(",", $hos['prop_owner']);
list($a) = $array;
$gu = $db->prepare("SELECT * FROM users WHERE user_id = :id");
$gu->execute(array(':id' => $a));
$dau = $gu->fetch();

echo $hos['prop_name']."<br><small>";

if(end($array)){
    echo "<a href='/user/view/".$dau['user_id']."' style='color:#".$dau['user_colour']."'>".$dau['user_name']."</a></small><br>";
} else {
    echo "<a href='/user/view/".$dau['user_id']."' style='color:#".$dau['user_colour']."'>".$dau['user_name']."</a>,";
}

Currently, the database field $hos['prop_owner'] contains the values "2,20" which are IDs of users (this field can potentially contain more IDs in the future). What I want to do is get all the user data from the exploded values, in this case 2 and 20, and then echo the information out in order as well.

Re-explanation:
I have a field in my database called prop_owner which is supposed to contain an unlimited number of user IDs, seperated by comma. Format: 1,2,3,4.
I want to take the value from this field, then somehow separate the user IDs and separately retrieve the usernames and echo them out.
Example result: Darren, Eva, Miles, Lisbeth

I hope I explained myself good enough to understand where I am trying to go with this.
Thanks in advance!

Darren
  • 13
  • 4
  • Hmm I'm not sure what you'r trying to do, its because of you'r lack of knowledge or my misunderstanding, why don't you use `foreach` to run over the array and sorting it with the new values you'r trying to add ? – Danny Jan 30 '15 at 18:02
  • See http://stackoverflow.com/questions/920353/can-i-bind-an-array-to-an-in-condition for how you can bind your exploded `$array` of user IDs to your query. – jszobody Jan 30 '15 at 18:06
  • I already tried that Danny, and yes I lack some knowledge which is why I'm here. The code in the link you sent me @jszobody is imploding though, not exploding. And I did try a couple of things but without luck. – Darren Jan 30 '15 at 20:33

1 Answers1

0

First of all the query will be like

SELECT * FROM users WHERE user_id in (2,20)

You need the data of both the users so the query will return all the data of all the ids that are being passed here.. You can directly pass here but you need to take care of security... or may be you can check how to pass values securely in such queries ...

Danyal Sandeelo
  • 12,196
  • 10
  • 47
  • 78