Jump to content

[SOLVED] Array merging problem


aaron118

Recommended Posts

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

Link to comment
https://forums.phpfreaks.com/topic/38941-solved-array-merging-problem/
Share on other sites

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?

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.