Jump to content

Why doesn't $_SESSION[$index] evaluate correctly?


aebuell

Recommended Posts

New here - I hope someone can help.

I want to compare $OptionID and $_SESSION[$index].

This is in a loop, where $index is equal to 'OptionID0', 'OptionID1', etc.

My if statement wasn't returning true when it should have. I have this debugging output from the loop:

    $_SESSION['OptionID0'] is 1
    $index is 'OptionID0'
    $_SESSION[$index] is
    $OptionID is 1


Since $OptionID is 1, and $_SESSION['OptionID0'] is 1, and $index = 'OptionID0', you would think $_SESSION[$index]  should be 1. But its output is blank. Anyone know why this doesn't work? Got a workaround?

Thanks a bunch.

-Abby
Link to comment
Share on other sites

The following code works for me -
[code]<?php
session_start();
$_SESSION['OptionID0'] = 1;
echo "direct: {$_SESSION['OptionID0']}<br />";
$index = 'OptionID0';
echo "var index: {$_SESSION[$index]}<br />";

$OptionID = 1;
if($OptionID == $_SESSION[$index])
{
echo '$OptionID is = to $_SESSION[$index]<br />';
}
?>[/code]thorpe beat me to it, but post the actual code you are using that gives a blank output.
Link to comment
Share on other sites

see the display here:
http://retain.iusb.edu/studebaker/visitor/work_in_progress

problem is that options do not remain checked once form is submitted. I want to store a session variable for each option selected. Having a little difficulty since there is no way of telling how many options the user will select.

code:


// get_option_data() returns the 'OptionID' and 'Name' fields for each result.

$option_results = get_option_data($_SESSION['MakeID'], $_SESSION['ModelID'],
                                   $_SESSION['Year'], $_SESSION['FrameID'],
                                   $_SESSION['ColorID'], $_SESSION['InteriorID'],
                                   $_SESSION['PackageID']);
                                   
  echo "<table border = \"0\" cellpadding = \"8\">";
 
  if(sizeof($option_results) > 0)
   {
       $i = 0;
       while($i < sizeof($option_results))
       {  
         echo "<tr>";
           for ($j = 0; $j < 2; $j++)  // Options are displayed in a table with 2 columns
           {
             echo "<td width = \"225\">";
             
             if ($i < sizeof($option_results))    // Need to check this again within the for loop
             {
               $OptionID = $option_results[$i]['OptionID'];  
               $OptionName = $option_results[$i]['Name'];    
             
               $name = "\"option_select" . $i . "\"";  
               $index = "'OptionID" . $i . "'";        
               
              // ***********  D E B U G G I N G  S T A T E M E N T S ************
               //echo "\$_SESSION['OptionID0'] is " . $_SESSION['OptionID0'];
               //echo "<br />";
               //echo "\$index is " . $index;
               //echo "<br />";
               //echo "\$_SESSION[\$index] is " . $_SESSION[$index];
               //echo "<br />";
               //echo "\$OptionID is $OptionID";
               //echo "<br />";
             // *******************************************************
               
               echo "<input type = \"checkbox\" name = $name value=\"$OptionID\"";
               if($OptionID == $_SESSION[$index])  
                 echo " checked ";
               echo ">$OptionName";  
             }
             
             echo "</td>";
             
             $i++;
       }
         echo "</tr>";
       }
     }
Link to comment
Share on other sites

Your form produces -
[code]<input type = "checkbox" name = "option_select0" value="1">Air Conditioning
<input type = "checkbox" name = "option_select1" value="2">Automatic Transmission
<input type = "checkbox" name = "option_select2" value="5">Navigation System[/code]
For this to work -
$_SESSION['OptionID0'] would need to be 1
$_SESSION['OptionID1'] would need to be 2
$_SESSION['OptionID2'] would need to be 5

Either the values don't match or the $_SESSION variables are not getting set. In the form code, you can use the following to display all the session variables -
[code]<?php
echo "<pre>";
print_r($_SESSION);
echo "</pre>";
?>[/code]
Beyond this, you would need to post your form processing code that sets the $_SESSSION variables for the option checkboxes.
Link to comment
Share on other sites

I have confirmed that the $_SESSION variables contained the correct values,  but now I'm thinking that I am probably setting them in the wrong place. I had a loop to check all possible values to see if they were set, but here's an example I used immediately after the above code I had posted, just to see if I could get one to work:

if(isset($_POST['option_select0']))
      {
        $_SESSION['OptionID0'] = $option_results[0]['OptionID'];                                                     
      }


I did that because it worked for my drop-down boxes, but the drop-down boxes are using the onChange='this.form.submit()' attribute whereas the check boxes are not.  I didn't want the screen to re-load every time the user checks an option although that would have been a lot easier to program.

So the $_SESSION variables are being set correctly, I think it's just that at the point of being compared they have not yet been set. I'll go try some things.

Thanks a bunch for your help.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.