Re: Nested foreach statement

From: Date: Mon, 31 Jul 2006 13:35:40 +0000
Subject: Re: Nested foreach statement
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-239916@lists.php.net to get a copy of this message
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/

« previous php.general (#239916) next »