coupe-r Posted November 26, 2011 Share Posted November 26, 2011 Hi All, After a query runs, I want to grab the ID's and separate them with a comma because they will be used in the next query. What I currently have does not work, although the query is correct. $sql = "SELECT a.app_id FROM applications a LEFT JOIN tenants t ON a.app_id = t.app_id WHERE t.tenant_id IN (".implode(",", $checked).")"; $result = mysqli_query($connect, $sql); $checked1 = array(); while($row = mysqli_fetch_array($result)) { foreach($row['app_id'] as $value1) {$checked1[] = $value1;} } echo implode(",", $checked1); Link to comment https://forums.phpfreaks.com/topic/251852-help-using-implode-after-query/ Share on other sites More sharing options...
Laash Posted November 26, 2011 Share Posted November 26, 2011 Hi You've done $checked1 as an array. In order to access an array, you have to access its nodes, like this: echo implode(",", $checked1[1]); or: echo implode(",", $checked1[0]); My suggest to you is to make the implode inside the loop Link to comment https://forums.phpfreaks.com/topic/251852-help-using-implode-after-query/#findComment-1291376 Share on other sites More sharing options...
coupe-r Posted November 26, 2011 Author Share Posted November 26, 2011 Thanks for the reply. I'm storing the values as an array, but it should echo the entire array like 1,3,4,5,6,7, or which ever ID's are selected, without using a node. I want the entire array. Above this code, it works just fine. I'm using similar code but putting checkbox IDs in the array foreach($_POST['checkbox'] as $value) {$checked[] = $value;} If I echo that implode, I get (1,3,4,5, etc) which ever boxes I check. Now that I am grabbing the IDs from a $row['app_id'], I think there is an issue with the values getting put into a proper array? Link to comment https://forums.phpfreaks.com/topic/251852-help-using-implode-after-query/#findComment-1291382 Share on other sites More sharing options...
kicken Posted November 26, 2011 Share Posted November 26, 2011 There's no need for your foreach() loop inside your while loop. In fact I would expect it to not work as $row['app_id'] wouldn't be an array. while($row = mysqli_fetch_array($result)) { $checked1[] = $row['app_id']; } That said, can you not just use a JOIN in your first sql query to achieve the desired results? Seems like it should be possible if your just getting ID's and re-querying. Link to comment https://forums.phpfreaks.com/topic/251852-help-using-implode-after-query/#findComment-1291418 Share on other sites More sharing options...
coupe-r Posted November 26, 2011 Author Share Posted November 26, 2011 Wow, what the hell was I thinking... That was too simple. Works perfectly. Thanks Kicken!!! Link to comment https://forums.phpfreaks.com/topic/251852-help-using-implode-after-query/#findComment-1291421 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.