Re: [PHP] Nested foreach statement
It will probably work, and you could find out for sure by just trying it. It might be better to construct a single query using things like: $company_ids = implode(', ', $_POST['reporton_company']); $query .= WHERE company_id IN ($company_ids) ; This presumes you have already validated the company_ids. However, if the number of companies, periods, and questions is SMALL, the difference between one big query and a dozen little queries is pretty minimal, and if you find the nested loops easier to maintain, go for it. On Mon, July 31, 2006 5:02 am, Chris Grigor wrote: Have been wondering if this is possible Basically I have 3 posted arrays, $_POST['reporton_company'] (this can be various company id's. ie 3,6,7) $_POST['report_period'] (this can be various periods but a max of 4 submitted. ie 3,4,5) $_POST['questions_groups'] (this can be various - starting from 1- whatever (usually a max of 10 or 11). ie 1,2,3,4,5,6,7,8,9,10) So the select should work as 1. for each company listed go through the loop 2. for each report period listed go through loop for each company 3. for each questions group go through the loop for each report period and each company.. So I came up with this - will it work?? foreach($_POST['reporton_company'] as $cmp_ind =$arrayd_cmp_id) { foreach($_POST['report_period'] as $rep_ind =$arrayd_per_id) { foreach($_POST['questions_groups'] as $group_ind = $arrayd_group_no) { mysql_select_db($database_name, $dname); $query_get_list_of_answers = SELECT * FROM answers LEFT JOIN (questions, period) ON (questions.id=answers.ans_l_question_idAND period.per_id=ans_l_period_id) where ans_l_company_id = '$arrayd_cmp_id' AND per_id = '$arrayd_per_id' AND group_no = '$arrayd_group_no';; $get_list_of_answers = mysql_query($query_get_list_of_answers, $antiva) or die(mysql_error()); $row_get_list_of_answers = mysql_fetch_assoc($get_list_of_answers); $totalRows_get_list_of_answers = mysql_num_rows($get_list_of_answers); } } } Anyone suggest an easier way? Cheers Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php -- Like Music? http://l-i-e.com/artists.htm -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach statement
foreach($_POST['reporton_company'] as $cmp_ind =$arrayd_cmp_id) { foreach($_POST['report_period'] as $rep_ind =$arrayd_per_id) { foreach($_POST['questions_groups'] as $group_ind = $arrayd_group_no) { mysql_select_db($database_name, $dname); Why do that here? That's going to do it for each element in the arrays, lots of overhead! Move that outside the first loop. I'd probably leave it as it is and make sure your data is what you expect, ie use mysql_real_escape_string in appropriate places. You could clean up one loop by doing this: $query_get_list_of_answers = SELECT * FROM answers LEFT JOIN (questions, period) ON (questions.id=answers.ans_l_question_id AND period.per_id=ans_l_period_id) where ans_l_company_id = '$arrayd_cmp_id' AND per_id = '$arrayd_per_id' AND group_no IN ( . implode(',', $_POST['questions_groups']) . ); but if I can enter dodgy values in the questions_groups form field, you're hosed. -- Postgresql php tutorials http://www.designmagick.com/ -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
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
RE: [PHP] Nested foreach ?
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:[EMAIL PROTECTED] Sent: 18 October 2004 08:24 To: John Holmes; [EMAIL PROTECTED] 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 -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Nested foreach ?
Wish I had better news. Warning: Invalid argument supplied for foreach() in /home/lurkkcom/public_html/TestMultiTrans2.php on line 90 INSERT INTO LurkProfiles_Skicerts (ProfileID, SkilCerts, NumYear, Lused) VALUES () line 90: foreach($skills as $key = $skill) To confirm : I changed to this: // Doing this makes the code below easier to read $skills = $_SESSION['skills']; $skys = $_SESSION['skys']; $slus = $_SESSION['slus']; From this : I changed the $_SESSION['skills'] = $_POST['skill']; $_SESSION['skys'] = $_POST['sky']; $_SESSION['slus'] = $_POST['slu']; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Nested foreach ?
These lines store the FORM's posted values (arrays) into your SESSION: $_SESSION['skills'] = $_POST['skill']; $_SESSION['skys'] = $_POST['sky']; $_SESSION['slus'] = $_POST['slu']; These lines get your SESSION variables (arrays) and put them into 'local' script array variables. If you are doing these then you MUST have done the above in the previous script. $skills = $_SESSION['skills']; $skys = $_SESSION['skys']; $slus = $_SESSION['slus']; If you are doing it all in one script just use: $skills = $_POST['skills']; $skys = $_POST['skys']; $slus = $_POST['slus']; Make sense? If not, may I suggest you do a bit of reading on PHP and form processing before proceeding. I have found the PHP manual extremely useful. With it (and some googling) I have gone from zero PHP knowledge 10 months ago to being able to develop and maintain an entire PHP/MySQL based web application subscribed to by several clients. HTH Graham -Original Message- From: Stuart Felenstein [mailto:[EMAIL PROTECTED] Sent: 18 October 2004 09:37 To: Graham Cossey; [EMAIL PROTECTED] Subject: RE: [PHP] Nested foreach ? Wish I had better news. Warning: Invalid argument supplied for foreach() in /home/lurkkcom/public_html/TestMultiTrans2.php on line 90 INSERT INTO LurkProfiles_Skicerts (ProfileID, SkilCerts, NumYear, Lused) VALUES () line 90: foreach($skills as $key = $skill) To confirm : I changed to this: // Doing this makes the code below easier to read $skills = $_SESSION['skills']; $skys = $_SESSION['skys']; $slus = $_SESSION['slus']; From this : I changed the $_SESSION['skills'] = $_POST['skill']; $_SESSION['skys'] = $_POST['sky']; $_SESSION['slus'] = $_POST['slu']; -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
RE: [PHP] Nested foreach ?
Guess what , it's working ! The key (after yours and John's iteration lines) was adding these into the script: $skills = $_SESSION['skills']; $skys = $_SESSION['skys']; $slus = $_SESSION['slus']; A big thank you! to all who helped out here and for hanging in. Graham, thank you for continuing to help me on this issue. You are right about manual, and I have been reading the manual along with some PHP / MySQL books. I actually have solved a few things on my own. ;) More reading is in order! Stuart --- Graham Cossey [EMAIL PROTECTED] wrote: -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
On Sun, 2004-10-17 at 09:53 -0700, Stuart Felenstein wrote: I have 3 arrays. 3 for 3 fields in a table (all part of 1 record) array1 - field1 array2 - field2 array3 - field3 What I've been doing which works good with one array: if ( is_array( $_SESSION['foo'] ) ) { foreach ( $_SESSION['foo'] as $x ) { sql .= INSERT INTO TABLE (... VALUES ($x) Just can't seem to figure out how with three arrays. help appreciated. Stuart Can you provide a print_r() output of your $_SESSION['foo'] ? I can help you more after that. -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / signature.asc Description: This is a digitally signed message part
Re: [PHP] Nested foreach ?
Robby Here is the printout : Using- print_r ($skills); print_r ($skys); print_r ($slus); Array ( [0] = skillone [1] = skilltwo [2] = skillthree [3] = [4] = ) Array ( [0] = 2 [1] = 3 [2] = 4 [3] = [4] = ) Array ( [0] = [1] = [2] = [3] = [4] = ) 1 FYI - The skills is a string, skys and slus ints. Also I had only filled in 3 of the 5 for each. Thank you Stuart --- Robby Russell [EMAIL PROTECTED] wrote: On Sun, 2004-10-17 at 09:53 -0700, Stuart Felenstein wrote: I have 3 arrays. 3 for 3 fields in a table (all part of 1 record) array1 - field1 array2 - field2 array3 - field3 What I've been doing which works good with one array: if ( is_array( $_SESSION['foo'] ) ) { foreach ( $_SESSION['foo'] as $x ) { sql .= INSERT INTO TABLE (... VALUES ($x) Just can't seem to figure out how with three arrays. help appreciated. Stuart Can you provide a print_r() output of your $_SESSION['foo'] ? I can help you more after that. -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / ATTACHMENT part 2 application/pgp-signature name=signature.asc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
On Sun, 2004-10-17 at 12:40 -0700, Stuart Felenstein wrote: Robby Here is the printout : Using- print_r ($skills); print_r ($skys); print_r ($slus); Array ( [0] = skillone [1] = skilltwo [2] = skillthree [3] = [4] = ) Array ( [0] = 2 [1] = 3 [2] = 4 [3] = [4] = ) Array ( [0] = [1] = [2] = [3] = [4] = ) 1 FYI - The skills is a string, skys and slus ints. Also I had only filled in 3 of the 5 for each. Thank you Stuart Are these 3 arrays related somehow? are all the [0]'s related and all the [1]'s related in each array? If so, you might consider having an array of arrays -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / signature.asc Description: This is a digitally signed message part
Re: [PHP] Nested foreach ?
They are related in the sense that they are part of one record in the database. Fields Skill YearsUsed LastUsed When you say array of arrays you are referring to a multi-dimensional ? I'm not sure what you mean by are the [0] related. Thank you, Stuart --- Robby Russell [EMAIL PROTECTED] wrote: On Sun, 2004-10-17 at 12:40 -0700, Stuart Felenstein wrote: Robby Here is the printout : Using- print_r ($skills); print_r ($skys); print_r ($slus); Array ( [0] = skillone [1] = skilltwo [2] = skillthree [3] = [4] = ) Array ( [0] = 2 [1] = 3 [2] = 4 [3] = [4] = ) Array ( [0] = [1] = [2] = [3] = [4] = ) 1 FYI - The skills is a string, skys and slus ints. Also I had only filled in 3 of the 5 for each. Thank you Stuart Are these 3 arrays related somehow? are all the [0]'s related and all the [1]'s related in each array? If so, you might consider having an array of arrays -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / ATTACHMENT part 2 application/pgp-signature name=signature.asc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
On Sun, 2004-10-17 at 13:08 -0700, Stuart Felenstein wrote: They are related in the sense that they are part of one record in the database. Fields Skill YearsUsed LastUsed When you say array of arrays you are referring to a multi-dimensional ? I'm not sure what you mean by are the [0] related. Thank you, Stuart I guess that I am just trying to figure out what your issue is. Can you explain what the issue is a bit more, in plain english and perhaps I can help you from there. All I can see right now is that you are having an issue with a nested foreach, but didn't really explain what the issue was. -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / signature.asc Description: This is a digitally signed message part
Re: [PHP] Nested foreach ?
Okay, I will try - I have these three fields in a form for user input. There are 10 sets of these, i.e. Users can enter three sets of skills, with the yearsused and when last used. I have set them up as arrays, since it's probably not wise to manage 30 seperate variables. I'm using session variables: $_SESSION['skills'] = $_POST['skill']; $_SESSION['skys'] = $_POST['sky']; $_SESSION['slus'] = $_POST['slu']; My problem is I do not know how to get these into the database with one query. Hope this is clearer. Thank you , Stuart --- Robby Russell [EMAIL PROTECTED] wrote: On Sun, 2004-10-17 at 13:08 -0700, Stuart Felenstein wrote: They are related in the sense that they are part of one record in the database. Fields Skill YearsUsed LastUsed When you say array of arrays you are referring to a multi-dimensional ? I'm not sure what you mean by are the [0] related. Thank you, Stuart I guess that I am just trying to figure out what your issue is. Can you explain what the issue is a bit more, in plain english and perhaps I can help you from there. All I can see right now is that you are having an issue with a nested foreach, but didn't really explain what the issue was. -Robby -- /*** * Robby Russell | Owner.Developer.Geek * PLANET ARGON | www.planetargon.com * Portland, OR | [EMAIL PROTECTED] * 503.351.4730 | blog.planetargon.com * PHP/PostgreSQL Hosting Development / ATTACHMENT part 2 application/pgp-signature name=signature.asc -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
Stuart Felenstein wrote: Here is the printout : Using- print_r ($skills); print_r ($skys); print_r ($slus); Array ( [0] = skillone [1] = skilltwo [2] = skillthree [3] = [4] = ) Array ( [0] = 2 [1] = 3 [2] = 4 [3] = [4] = ) Array ( [0] = [1] = [2] = [3] = [4] = ) 1 FYI - The skills is a string, skys and slus ints. Also I had only filled in 3 of the 5 for each. Since we're just guessing at what you want, I'll guess something like this: foreach($skills as $key = $skill) { echo $skill - {$skys[$key]} - {$slus[$key]}\n; } -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
John, Sorry, I'm not trying to be unclear. There are 3 columns in the table. In the user form. there are 30 fields, 10 text and 20 dropdown. The 10 text are: $_SESSION['skills'] = $_POST['skill']; The first dropdown is : $_SESSION['skys'] = $_POST['sky']; The second dropdown is: $_SESSION['slus'] = $_POST['slu']; So I need to grab the input and enter into database where each set of skill, sky , slu is a new record. I hope this is clearer. Stuart --- John Holmes [EMAIL PROTECTED] wrote: Stuart Felenstein wrote: Here is the printout : Using- print_r ($skills); print_r ($skys); print_r ($slus); Array ( [0] = skillone [1] = skilltwo [2] = skillthree [3] = [4] = ) Array ( [0] = 2 [1] = 3 [2] = 4 [3] = [4] = ) Array ( [0] = [1] = [2] = [3] = [4] = ) 1 FYI - The skills is a string, skys and slus ints. Also I had only filled in 3 of the 5 for each. Since we're just guessing at what you want, I'll guess something like this: foreach($skills as $key = $skill) { echo $skill - {$skys[$key]} - {$slus[$key]}\n; } -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- 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
Re: [PHP] Nested foreach ?
Stuart, Having read what you've written so far, you may want to try something like this. In your HTML (I've left the selects empty, but you can see what I mean): input type=text name=skills[0][name] /select name=skills[0][years] /select name=skills[0][used] / input type=text name=skills[1][name] /select name=skills[1][years] /select name=skills[1][used] / input type=text name=skills[2][name] /select name=skills[2][years] /select name=skills[2][used] / ... input type=text name=skills[9][name] /select name=skills[9][years] /select name=skills[9][used] / Then in PHP ?php foreach ($_POST['skills'] as $skill) { $skillname = $skill['name']; $yearsused = $skill['years']; $lastused = $skill['used']; ... } ? Hope this helps Cheers Chris -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php
Re: [PHP] Nested foreach ?
Stuart Felenstein wrote: John, Sorry, I'm not trying to be unclear. There are 3 columns in the table. In the user form. there are 30 fields, 10 text and 20 dropdown. The 10 text are: $_SESSION['skills'] = $_POST['skill']; The first dropdown is : $_SESSION['skys'] = $_POST['sky']; The second dropdown is: $_SESSION['slus'] = $_POST['slu']; So I need to grab the input and enter into database where each set of skill, sky , slu is a new record. I hope this is clearer. Did you read the answer I gave? foreach($skills as $key = $skill) { echo $skill - {$skys[$key]} - {$slus[$key]}\n; } Now adapt that to your session variables and query: foreach($_SESSION['skills'] as $key = $skill) { $query = INSERT INTO table (skill, sky, sku) VALUES ('$skill', {$_SESSION['skys'][$key]},{$_SESSION['slus'][$key]}); //run query } or, if you can use extended inserts: $insert = array(); foreach($_SESSION['skills'] as $key = $skill) { $insert[] = ('$skill',{$_SESSION['skys'][$key]},{$_SESSION['skus'][$key]}); } $query = 'INSERT INTO table (skill,sky,sku) VALUES ' . implode(',',$insert); //run query -- ---John Holmes... Amazon Wishlist: www.amazon.com/o/registry/3BEXC84AB3A5E/ php|architect: The Magazine for PHP Professionals www.phparch.com -- PHP General Mailing List (http://www.php.net/) To unsubscribe, visit: http://www.php.net/unsub.php