Variables acting like pointers

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Variables acting like pointers

Post 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.
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

it's hard to say w/o seeing the rest of your loop.
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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 = '&nbsp;';
              } else {
                $price = '(&nbsp; '.$ov_data['prefix'] . '&nbsp;' . $currencies->display_price($ov_data['price'], $tax_rate).'&nbsp;)';
              }
              $tmp_html .= '<option value="' . $vID . '">' . $ov_data['name'] . '&nbsp;' . $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
?>
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post by Chris Corbyn »

$op_data["name"] also has a conditional on it... echo it.
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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.
User avatar
Chris Corbyn
Breakbeat Nuttzer
Posts: 13098
Joined: Wed Mar 24, 2004 7:57 am
Location: Melbourne, Australia

Post 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...
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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
User avatar
cczernia
Forum Newbie
Posts: 20
Joined: Tue May 16, 2006 2:00 pm
Location: San Diego, CA

Post 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.
Post Reply