Page 1 of 1
Variables acting like pointers
Posted: Thu Apr 26, 2007 5:07 pm
by cczernia
I am having a problem with a variable that keeps changing and I don't know why. I have this bit of code inside a loop.
Code: Select all
if ($frameoptions == 1) {
$frames = $ov_data;
$frameoptions = 2;
}
The point of this code is to get $frame to equal $ov_data (an array) during one specific pass. However, it is changing during other passes. I have checked and noticed that the above condition only occurs once and that the output inside the condition is correct but the end result of the variable $frames will equal the last repetition of a loop.
Does anyone know what might be causing this. Thanks in advance.
Posted: Thu Apr 26, 2007 5:21 pm
by Burrito
it's hard to say w/o seeing the rest of your loop.
Posted: Thu Apr 26, 2007 5:29 pm
by cczernia
Here is the main loop from oscommerce creloaded (templates/content/product_info.tpl.php)
I removed the other case elements from the switch element as they would never be positive.
Code: Select all
<?php
// Store the information from the tables in arrays for easy of processing
$options = array();
$options_values = array();
while ($po = tep_db_fetch_array($products_options_query)) {
// we need to find the values name
if ( $po['options_type'] != 1 && $po['options_type'] != 4 ) {
$options_values_query = tep_db_query("select products_options_values_name from " . TABLE_PRODUCTS_OPTIONS_VALUES . " where products_options_values_id ='". $po['options_values_id'] . "' and language_id = '" . (int)$languages_id . "'");
$ov = tep_db_fetch_array($options_values_query);
} else {
$ov['products_options_values_name'] = '';
}
$options[$po['options_id']] = array('name' => $po['products_options_name'], 'type' => $po['options_type'], 'length' => $po['options_length'], 'instructions' => $po['products_options_instruct']);
$options_values[$po['options_id']][$po['options_values_id']] = array('name' => stripslashes($ov['products_options_values_name']), 'price' => $po['options_values_price'], 'prefix' => $po['price_prefix']);
}
foreach ($options as $oID => $op_data) {
//frame options begin
//If frame accessories are available it will set frameoptions to 1 which will display the options.
if ($op_data['name'] == 'Frame') {
$frameoptions = "1";
}
//frame options end
switch ($op_data['type']) {
case 0:
$tmp_html = '<select name="id[' . $oID . ']">';
//to include only specific frames
if ($frameoptions == 1) {
$frames = $ov_data;
$frameoptions = 2;
}
foreach ( $options_values[$oID] as $vID => $ov_data ) {
if ( (float)$ov_data['price'] == 0 ) {
$price = ' ';
} else {
$price = '( '.$ov_data['prefix'] . ' ' . $currencies->display_price($ov_data['price'], $tax_rate).' )';
}
$tmp_html .= '<option value="' . $vID . '">' . $ov_data['name'] . ' ' . $price .'</option>';
} // End of the for loop on the option values
$tmp_html .= '</select>';
?>
<tr>
<td class="main"><?php echo $op_data['name'] . ':' . ($op_data['instructions'] != '' ? '<br /><span class="smallText">' . $op_data['instructions'] . '</span>' : '' ); ?></td>
<td class="main"><?php echo $tmp_html; ?></td>
</tr>
<?php
break;
} //end of switch
} //end of while
?>
Posted: Thu Apr 26, 2007 5:41 pm
by Burrito
try echoing $op_data['type'] and $frameoptions inside your loop to make sure that they never reach the conditions that would reset the frames array. You are *potentially* resetting $frameoptions back to 1 with each iteration of the loop....don't know about $op_data['type'] though.
Posted: Fri Apr 27, 2007 12:50 am
by Chris Corbyn
$op_data["name"] also has a conditional on it... echo it.
Posted: Fri Apr 27, 2007 10:40 am
by cczernia
Burrito wrote:try echoing $op_data['type'] and $frameoptions inside your loop to make sure that they never reach the conditions that would reset the frames array. You are *potentially* resetting $frameoptions back to 1 with each iteration of the loop....don't know about $op_data['type'] though.
I thought about that and printed $frameoptions and $op_data['name'] through another loop. My test code looks like this:
Code: Select all
//to include only specific frames
if ($frameoptions == 1) {
echo $frameoptions . '<br>';
$frames = $ov_data;
$frameoptions = 2;
foreach ( $options_values[$oID] as $vID => $ov_data ) {
echo $ov_data['name'] . '<br>';
}
}
The output is:
Condition only becomes positive once
frameoptions is 1
The loop outputs the correct array.
Posted: Fri Apr 27, 2007 10:51 am
by Chris Corbyn
You potentially reset it here:
Code: Select all
foreach ($options as $oID => $op_data) {
//frame options begin
//If frame accessories are available it will set frameoptions to 1 which will display the options.
if ($op_data['name'] == 'Frame') {
$frameoptions = "1";
Echo something in that condition...
Posted: Fri Apr 27, 2007 10:57 am
by cczernia
d11wtq wrote:You potentially reset it here:
Code: Select all
foreach ($options as $oID => $op_data) {
//frame options begin
//If frame accessories are available it will set frameoptions to 1 which will display the options.
if ($op_data['name'] == 'Frame') {
$frameoptions = "1";
Echo something in that condition...
Ok, I changed it to:
Code: Select all
//If frame accessories are available it will set frameoptions to 1 which will display the options.
if ($op_data['name'] == 'Frame') {
echo 'first condition ' . $op_data['name'] . '<br>';
$frameoptions = "1";
}
//frame options end
Condition was positive once and outputed:
first condition Frame
Posted: Fri Apr 27, 2007 11:04 am
by cczernia
Ok, I figured out I don't need the first condition and deleted it. I changed the second condition to:
Code: Select all
//framing begin: to include only specific frames
if ($op_data['name'] == 'Frame') {
$frames = $ov_data;
$frameoptions = 2;
foreach ( $options_values[$oID] as $vID => $ov_data ) {
echo $ov_data['name'] . '<br>';
}
}
//framing end
However, I am getting the same results and having the same problem.