Search the Community
Showing results for tags 'foreach'.
-
I'm having a problem with a foreach loop that isn't doing what I need it to. I want to take an array of id numbers, fetch the figures related to that id number from my mysql db and sum them. Initially the problem was that my loop was 'overwriting itself' so that the only summing it did was of the last id number. Then after I fixed that, although I have used a 'unique array', it seems that the loop is still summing everything, ie if the array contains the same id number 8 times, it loops through and multiplies that figure x8 (at least I think that's what it's doing). I'm not sure why while($row_outputs=mysqli_fetch_array($run_projects)){ $prog_name = $row_outputs['prog_name']; $proj_name = $row_outputs['name']; $projectId = $row_outputs['id']; $proj_array = array($projectId); $proj_array_uni = array_unique($proj_array); $projecty = ''; foreach($proj_array as $projectile){ $projectile = trim($projectile); $projecty .= $projectile; $get_outputs_active = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='1'"; $run_outputs_active = mysqli_query($conn, $get_outputs_active); while ($row_outputs_active =mysqli_fetch_array($run_outputs_active)){ $total_outputs_active += $row_outputs_active['total_cost']; } $ftotal_outputs_active = number_format($total_outputs_active,2); $get_outputs_run = "SELECT * FROM projects WHERE project_id=$projectile AND impact_area='2'"; $run_outputs_run = mysqli_query($conn, $get_outputs_run); while ($row_outputs_run =mysqli_fetch_array($run_outputs_run)){ $total_outputs_run += $row_outputs_run['total_cost']; } } ?> Can anybody see what my problem is? Thanks.
-
Hi. First off, this is in regards to exercise dealing with using a foreach loop instead of implode(). I was having trouble with it, and of course looked up what was posted here already, but I want to do more. By the I mean I wanted to validate that the form data was not empty, had no numbers, and had a minimum of five words that will of course be alphabetized(though I'd rather being doing some analysis with it for simple patterns like the avg number of characters by word, number of vowels, number of constants, but let me just be simple about it for now). Here's the code(just a note, to give credit where it's due this is code I've altered that was posted here originally): $flag = true; $words_Array = $_POST['words']; $count1 = count($words_Array); // turn the incoming string, $_POST['words'], into an array and validate: if (empty($words_Array) || (is_numeric($words_Array) ) || ($count1 < 5) ) { print "<p>Please enter at least five words and no numbers.</p>"; $flag= false; } else { $words_Array = explode (' ', $_POST['words']); } //sort the words alphabetically if ($flag) { sort($words_Array); } if ($flag) { print "<p>An alphabetized version of your list is: "; foreach ($words_Array as $key => $value) { print "<br />\n $value "; } } print "</p>"; ?> Now, I've run this code and whether I enter five words or four words and a number, it still runs the print statement inside the first if statement "Please enter at least five words and no numbers." So what I'm I missing here logic wise? Also I'm thinking this code could much cleaner without having to be redundant by using three separate if statements, would an if-elseif-else be a good choice/best practice ? Lastly, can validation be done without using a boolean "flag" variable ? Assistance is greatly appreciated.
- 3 replies
-
- validation
- foreach
-
(and 1 more)
Tagged with: