Re: Nested foreach statement

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

 



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_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 = 			'$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


[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