aaron118 Posted February 17, 2007 Share Posted February 17, 2007 In my MYSQL database I have a field called chosen_products which has the IDs of each chosen product, e.g. 1,3. What I am trying to do is to get the contents of chosen_products (e.g. 1,3) and then find the chosen products names by querying the database against those IDs. Heres my code: $match = $DB->SelectRow("SELECT chosen_products FROM orders WHERE uid='$uid' AND oid='$oid'"); $chosen_products = $match["chosen_products"]; $chosen_products_ids = array($chosen_products); $chosen_products_names = array(); foreach($chosen_products_ids as $val) { $result = $DB->SelectRow("SELECT pname FROM products WHERE pid='$val'"); $pname = array($result['pname']); $chosen_products_names = array_merge($chosen_products_names, $pname); } $chosen_products_names = join(',', $chosen_products_names); So I eventually want $chosen_products_names returned as a string of names seperated by a commar. But the final string only shows the name of the first product from $chosen_products. Any ideas on where I've gone wrong? Thanks Quote Link to comment Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 try before the array_merge adding a debug echo $pname; see if all the products display in that loop I think its a bug in your DB class or maybe your not using it correctly Quote Link to comment Share on other sites More sharing options...
aaron118 Posted February 17, 2007 Author Share Posted February 17, 2007 It won't be a bug in my DB class, becasue I use many methods like this with my DB class just not like this. The problem is something to do with the arrays, becasue the foreach is only looping once, thats why the first product is only displayed. Quote Link to comment Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 what does print_r($chosen_products_names); show than just 1 array? Quote Link to comment Share on other sites More sharing options...
aaron118 Posted February 17, 2007 Author Share Posted February 17, 2007 print_r($chosen_products_names); just prints: Product 1. Which is the name of the first chosen_products ID. Quote Link to comment Share on other sites More sharing options...
sspoke Posted February 17, 2007 Share Posted February 17, 2007 ya that show anything anyways you have to inside the loop before the array merge add print_r $pname; if it always shows different products than thats really weird really is a array_merge problem if not im still sticking with a DB class problem Quote Link to comment Share on other sites More sharing options...
aaron118 Posted February 17, 2007 Author Share Posted February 17, 2007 Ok the problem is $chosen_products_ids = array($chosen_products); Because it is putting the result of $chosen_products (1,3) into one element row of the array, I can tell this becasue print_r($chosen_products_ids); outputs Array ( [0] => 1,3 ) whereas it should be Array ( [0] => 1 [1] => 3 ). Any ideas? Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.