SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Addict
    Join Date
    May 2005
    Posts
    297
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Array merging problem

    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:
    PHP 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

  2. #2
    SitePoint Member
    Join Date
    Jan 2007
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well, you only need to query the database once using a query like the following
    Code:
    $query = "SELECT pname FROM products WHERE pid = (SELECT chosen_products FROM orders WHERE uid = '$uid' AND oid = '$oid')";
    then rest of it is just throwing in the comma and concat. the string.

  3. #3
    SitePoint Addict
    Join Date
    May 2005
    Posts
    297
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    That query makes no difference. 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?

  4. #4
    SitePoint Wizard silver trophybronze trophy Cups's Avatar
    Join Date
    Oct 2006
    Location
    France, deep rural.
    Posts
    6,869
    Mentioned
    17 Post(s)
    Tagged
    1 Thread(s)
    $prods = explode(',' $chosen_products_ids);

    print_r($prods);

    www.php.net/explode

  5. #5
    SitePoint Addict
    Join Date
    May 2005
    Posts
    297
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks a lot Cups , explode was what I was looking for.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •