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
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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.