Re: can I do a for each here??

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



I would be tempted to do the following.

First, I would setup the html form so that the text boxes are named something like 'age[Andrea]', 'age[Bob]', etc. When the form submitted, this will give you an array, accessible as $_POST['age'] (or $_GET['age'] depending on whether you used POST or GET form submission method).

This array would look like ("Andrea" => "25", "Bob" => "13", etc).

Then, I would use the following:

foreach($_POST['age'] as $name => $age)
{
  setcookie("cookie[$name]", $age);
}


Although, unfortunately, this approach you take of using seperate cookies for all your different stored value is what I call cookie bloat. I would suggest you look at PHP's built-in session handling facilities.


http://www.php.net/manual/en/ref.session.php

The way the session handling works is, in a nutshell, at the start of each of your scripts, you call session_start(). Then, you can access session variables as $_SESSION['name']. You can use the isset() operator to test to see if you already set a session variable, and you can use any of the normal access methods to manipulating the variable. You assign to it with $_SESSION['name'] = "value", you can get the value just by using $_SESSION['name'] anywhere you would otherwise use a variable or constant (e.g. if($_SESSION['ages']['Andrea'] > 18) {echo "You have been selected for Jury duty.";}.

(All this assumes your hosting provider has setup PHP for you, and configured the session handler. If that is not the case, then cookies might be an easier way to go. Depending on how ambitious you feel, and how much control you have of your site, you might also setup session handling yourself - it's not incredibly difficult, just read the documentation).

With that in mind, I would alter my above code to be the following:

foreach($_POST['age'] as $name => $age)
{
  $_SESSION['age'][$name] = $age;
}

AndreaD wrote:

Think what I want to do then is create two arrays, one for the values of the age and one for the correponding name e.g.

$age  = array() // this is the inputed textbox values
$name= array('john', 'bob', 'tim')

Then I need to assign the the ages to cookie of the persons name by using a for or do-while loop.

loop{
if (isset($age[x])){setcookie("cookie[ $name[x] ]", "$age[x]");}
else {
setcookie("cookie[ name[x] ]", "");}

}end loop

Any suggestion how I would execute this would be fantactic. Not to worry I can just have 10 lines if need be.

AD


"Chris Ramsay" <raz.net@xxxxxxxxx> wrote in message news:828f82cb05031704547c60c71@xxxxxxxxxxxxxxxxx


Difficult to be definitive without seeing your code, but I would be
tempted by the use of arrays...

cheers



-- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux