RE: [PHP] Nested foreach ?
| From: | Graham Cossey | Date: | Mon, 18 Oct 2004 08:19:57 +0000 |
| Subject: | RE: [PHP] Nested foreach ? | ||
| References: | 1 | Groups: | php.general |
| Request: | Send a blank email to php-general+get-199707@lists.php.net to get a copy of this message | ||
How about this:
// Doing this makes the code below easier to read
$skills = $_SESSION['skills'];
$skys = $_SESSION['skys'];
$slus = $_SESSION['slus'];
// Set up the fixed part of teh query
$query = "INSERT INTO table (skill, sky, sku) VALUES (";
// Loop through each set of form elements
foreach($skills as $key => $skill)
{
$query .= "'$skill','{$skys[$key]}','{$slus[$key]}',";
}
$query = rtrim($query, ','); // Remove any comma from end of $query
$query .= ')'; // Close VALUES (
echo $query; // What do you get?
// RUN QUERY HERE
Graham
> -----Original Message-----
> From: Stuart Felenstein [mailto:stuart4m@yahoo.com]
> Sent: 18 October 2004 08:24
> To: John Holmes; php-general@lists.php.net
> Subject: Re: [PHP] Nested foreach ?
>
>
> Not working.
>
> > foreach($_SESSION['skills'] as $key => $skill)
> > {
> > $query = "INSERT INTO table (skill, sky, sku)
> > VALUES ('$skill',
> >
> {$_SESSION['skys'][$key]},{$_SESSION['slus'][$key]})";
> > //run query
> > }
> The foreach is generating an invalid argument.
> I'm just going to show again what I have set up:
>
> There are five for each of these:
> <input name="skill[]" type="text" id="skill[]"></td>
> <select name="sky[]" id="sky[]">
> <select name="slu[]" id="slu[]">
>
> Then I post the session variables as:
> $_SESSION['skills'] = $_POST['skill'];
> $_SESSION['skys'] = $_POST['sky'];
> $_SESSION['slus'] = $_POST['slu'];
>
> It looks like the loop above is using the $skills
> array to advance through the other arrays ?
>
> Stuart
>
> --
> PHP General Mailing List (http://www.php.net/)
> To unsubscribe, visit: http://www.php.net/unsub.php
>
>