Re: Where am i screwing up?
| From: | CPT John W. Holmes | Date: | Thu, 31 Jul 2003 15:01:08 +0000 |
| Subject: | Re: Where am i screwing up? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-157744@lists.php.net to get a copy of this message | ||
From: "Ryan A" <ryan@jumac.com>
> I am just screwing around and getting used to arrays with a "foreach"
thanks
> to Michael,Jan and Evan from this list and have run into problems.
>
> My requirment is pretty simple (code is below too)
> 1)unset all the sessions that might have been set with reference to the
> hardcoded array ($vars)
> 2)if any checkboxes have been set from the previous form then set that
> session
> needless to say, its not working, am too new at this to know where my
fault
> is so any help is greatly appreciated.
>
> I have even added a few comments as to what i was thinking....tell me if i
> was wrong.
>
> **************Start code************************
> <?php
> $vars =
> Array('noPlatform','noPrice','noSfee','noSpace'); // this
> corresponds to the "name=" of each checkbox
>
> foreach ($vars as $key) // clear all previous sessions
> {
> if(isset($_SESSION['$key']))
> {
> unset($_SESSION['$key']);
> }
> }
> echo "done1"; //just checking program execution
>
> foreach ( $vars as $vvvv ) // if any checkboxes were checked create a
> session for them
> {
> ${$vvvv} = ( isset($_POST[$vvvv]) ? 1 : 0 );
>
> if($vvvv==1)
> {
> $_SESSION[$vvvv];
> echo $vvvv; //getting no output from here...I just put it here for
> testing
> }
> }
> echo "done2"; //just checking program execution
> ?>
>
> ****************End code************************
How about you name your checkboxes as:
name="setting[xx]"
where xx is from your $vars array above. You'll end up with
name="setting[noPlatform]" value="1"
name="setting[noPrice]" value="1"
etc...
Now, you'll have $_POST['setting'] that'll contain the checked boxes.
To put those in session:
$_SESSION['setting'] = $_POST['setting'];
To see which boxes were checked...
$checked = array_keys($_POST['setting']);
or
$checked = array_keys($_SESSION['setting']);
$checked now an array similar to your $vars above but it only contains the
values that were checked.
To find out which checkboxes were NOT checked, you could use:
$not_checked = array_diff($vars,$checked);
Easy, eh?? Hope that helps.
---John Holmes...