RE: Nested foreach ?

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

 



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@xxxxxxxxx]
> Sent: 18 October 2004 08:24
> To: John Holmes; php-general@xxxxxxxxxxxxx
> Subject: Re:  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
> 
> 

-- 
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