...

View Full Version : Variable Variables and Multidimensional arrays... HELP!



Gremlyn1
09-09-2009, 09:48 PM
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.


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

bacterozoid
09-09-2009, 09:56 PM
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.

Gremlyn1
09-09-2009, 10:28 PM
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...

Gremlyn1
09-09-2009, 10:37 PM
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.

bacterozoid
09-09-2009, 10:59 PM
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



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.

Gremlyn1
09-09-2009, 11:01 PM
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:


$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.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum