[PHP] can I do a for each here??

2005-03-17 Thread AndreaD
I have about 10 text boxes each taking in value (ages) , The code I have 
checks for a value and if it is set it then sets the cookie to that value. 
The else just clears the value. On the next page I use a foreach to get the 
values but I think there must be a quicker way to collect the data appart 
from 10 if-else staements.

if (isset($andrea){

setcookie(cookie[andrea], $andrea);
}

else {setcookie(cookie[$andrea], );

}


if (isset($james){

setcookie(cookie[james], $james);
}

else {setcookie(cookie[$james], );

}


AD 

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



Re: [PHP] can I do a for each here??

2005-03-17 Thread Chris Ramsay
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



Re: [PHP] can I do a for each here??

2005-03-17 Thread Marek Kilimajer
AndreaD wrote:
I have about 10 text boxes each taking in value (ages) , The code I have 
checks for a value and if it is set it then sets the cookie to that value. 
The else just clears the value. On the next page I use a foreach to get the 
values but I think there must be a quicker way to collect the data appart 
from 10 if-else staements.

if (isset($andrea){
setcookie(cookie[andrea], $andrea);
}
else {setcookie(cookie[$andrea], );
}
if (isset($james){
setcookie(cookie[james], $james);
}
else {setcookie(cookie[$james], );
}
$names = array('andrea', 'james', ...);
foreach($names as $name) {
  if (isset($_POST[$name]){
setcookie(cookie[$name], $_POST[$name]);
  } else {
setcookie(cookie[$name], );
  }
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] can I do a for each here??

2005-03-17 Thread AndreaD
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 [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]
 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



Re: [PHP] can I do a for each here??

2005-03-17 Thread Jeff Schmidt
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 [EMAIL PROTECTED] wrote in message 
news:[EMAIL PROTECTED]

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


Re: [PHP] can I do a for each here??

2005-03-17 Thread Jeff Schmidt
Yeah, after hitting the send button, I looked again at what I sent and 
realized something else. Having already caused PHP to put the stuff into 
an array, by the way I suggested constructing the form, you can 
eliminate the foreach loop, and just use the assignment operator to get 
PHP to copy the array from $_POST to $_SESSION (although, you might want 
to do some validation code, in which case you probably do want to use 
the foreach block, so that you have a chance to validate each of the 
submitted values, and the keys [sometimes it's usefull to validate the 
keys, to check to see if someone has forged a form submission with 
values other than what they are supposed to be using]).

But, assuming you aren't worried about validating, you could do like this:
$_SESSION['age'] = $_POST['age'];
But, honestly, I would still use the foreach loop, check the values of 
$name and $age to make sure they are legal, and in the correct format, 
and then assign them individually, as before.

Jeff
Jeff Schmidt wrote:
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);
}

[snip]
With that in mind, I would alter my above code to be the following:
foreach($_POST['age'] as $name = $age)
{
  $_SESSION['age'][$name] = $age;
}
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


Re: [PHP] can I do a for each here??

2005-03-17 Thread Chris Shiflett
AndreaD wrote:
I have about 10 text boxes each taking in value (ages), The code I have
checks for a value and if it is set it then sets the cookie to that value.
The else just clears the value. On the next page I use a foreach to get the
values but I think there must be a quicker way to collect the data appart
from 10 if-else staements.
if (isset($andrea){
setcookie(cookie[andrea], $andrea);
}
else {setcookie(cookie[$andrea], );
}
Let's look at your two setcookie() calls:
setcookie(cookie[andrea], $andrea)
setcookie(cookie[$andrea], )
Just for debugging purposes, assume $andrea is initialized prior to this 
as follows:

$andrea = 29;
So, your code says that you want a cookie with the following name and 
value (available on the next request):

$_COOKIE['cookie[andrea]'] = 29;
Is that really what you want?
Of course, it's very odd that if $andrea is not set, you want a cookie 
like this:

$_COOKIE['cookie[]'] = '';
In addition, you will be generating a notice, because you're using 
$andrea in the name of the cookie, although you're first making certain 
that $andrea isn't set.

I'm also a bit concerned about where $andrea originates. Is this coming 
from the user, and you're trusting it? That's a very dangerous practice.

If you explain your problem, we might be able to offer some help.
Chris
--
Chris Shiflett
Brain Bulb, The PHP Consultancy
http://brainbulb.com/
--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php