Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post

    Variable Variables and Multidimensional arrays... HELP!

    To pull product info from my database, I use an SQL call that will pull only the appropriate products based on the users permission. I then want all the product attributes stored in an array, and then each individual product array is stored in one big products array.

    PHP Code:
    $prod_sql mysql_query("SELECT * FROM products WHERE status ='active' AND id IN ({$_SESSION['plist']})");
        
    $active_prod = array();
        while (
    $prd mysql_fetch_array($prod_sql)) {
            
    $prd['id'] = $pid;
            ${
    "prod$pid"} = array();
                ${
    "prod$pid"}['id']     = $pid;
                ${
    "prod$pid"}['model'] = $prd['model'];
                ${
    "prod$pid"}['name'] = $prd['name'];
            if (
    substr_count($prd['mgr'], ",") > 0) {
                
    $exp_mgr explode($prd['mgr'], ",");
                ${
    "prod$pid"}['mgr'] = $exp_mgr;
            } else {
                ${
    "prod$pid"}['mgr'] = $prd['mgr'];
            }    
                ${
    "prod$pid"}['url_id']  = $prd['url_id'];
                ${
    "prod$pid"}['txt_id'] = $prd['txt_id'];
                ${
    "prod$pid"}['desc'] = $prd['description'];
                ${
    "prod$pid"}['app'] = $prd['application'];
                ${
    "prod$pid"}['img_sml'] = $prd['img_thumb'];
                ${
    "prod$pid"}['img_ful'] = $prd['img_fullsize'];
            
    $active_prod[$pid] = ${"prod$pid"};
        } 
    Any ideas why I'm getting nothing in my array?

  • #2
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    1. I think that, if you're using variable variable names, your code may benefit from some improvement. Why not just use $current_product as your variable. Once you put it in your products array, it won't matter what it was named anyways.

    2. Try doing a print_r on $prd and your crazy $prod# variable...see what shows up. If you're not getting data in those, you can better identify where the problem is.

  • #3
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post
    Quote Originally Posted by bacterozoid View Post
    1. I think that, if you're using variable variable names, your code may benefit from some improvement. Why not just use $current_product as your variable. Once you put it in your products array, it won't matter what it was named anyways.

    2. Try doing a print_r on $prd and your crazy $prod# variable...see what shows up. If you're not getting data in those, you can better identify where the problem is.
    1. That makes perfect sense. If I don't use the variable variable name for the smaller array and instead go for $current_product, do I need to release $current_product at the end of each loop?

    2. I tried doing a count on the final product and was getting 0, but unfortunately this section of code rests outside of the HTML tags so I don't think I can get print_r to work...

  • #4
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post
    Just found out that I'm not setting the $_SESSION['plist'] variable initially. If you enter into the admin area of the site, then that variable doesn't exist yet, if you then go out to the user area it does, that seems to be getting me somewhere.

  • #5
    Regular Coder bacterozoid's Avatar
    Join Date
    Jun 2002
    Location
    USA
    Posts
    490
    Thanks
    24
    Thanked 35 Times in 35 Posts
    You don't need to release it no, as long as you say $current_product = array() before you enter into that section next time so it's clear.

    Still try and get some visible results...it will help tremendously in knowing what has been set. Try doing

    PHP Code:
    echo '<script language="text/javascript">';
    echo 
    'alert("',print_r($variable),'")';
    echo 
    '</script>'
    to use javascript to show your PHP results if you can't directly display them on a page.

  • Users who have thanked bacterozoid for this post:

    Gremlyn1 (09-10-2009)

  • #6
    New Coder
    Join Date
    Apr 2009
    Location
    San Diego, CA
    Posts
    64
    Thanks
    2
    Thanked 1 Time in 1 Post
    OK, so it was mostly the $_SESSIONS['plist'] that was screwing it up for me. I moved where that gets set and cleaned up my code a little to:

    PHP Code:
        $prod_sql mysql_query("SELECT * FROM products WHERE status ='active' AND id IN ({$_SESSION['plist']})");
        
    $active_prod = array();
        while (
    $prd mysql_fetch_array($prod_sql)) {
            
    $pid $prd['id'];
            
    $curr_prod = array();
                
    $curr_prod['id'] = $pid;
                
    $curr_prod['model']    = $prd['model'];
                
    $curr_prod['name'] = $prd['name'];
            if (
    substr_count($prd['mgr'], ",") > 0) {
                
    $exp_mgr explode($prd['mgr'], ",");
                
    $curr_prod['mgr'] = $exp_mgr;
            } else {
                
    $curr_prod['mgr'] = $prd['mgr'];
            }    
                
    $curr_prod['url_id'] = $prd['url_id'];
                
    $curr_prod['txt_id'] = $prd['txt_id'];
                
    $curr_prod['desc'] = $prd['description'];
                
    $curr_prod['app'] = $prd['application'];
                
    $curr_prod['img_sml'] = $prd['img_thumb'];
                
    $curr_prod['img_ful'] = $prd['img_fullsize'];
            
    $active_prod[$pid] = $curr_prod;
        } 
    Thanks for the suggestion bacterozoid, it's much cleaner this way.


  •  

    Posting Permissions

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