Jump to content
Larry Ullman's Book Forums

Search the Community

Showing results for tags 'foreach'.



More search options

  • Search By Tags

    Type tags separated by commas.
  • Search By Author

Content Type


Forums

  • Single Editions
    • Modern Javascript: Develop and Design
    • The Yii Book
    • Effortless Flex 4 Development
    • Building a Web Site with Ajax: Visual QuickProject
    • Ruby: Visual QuickStart Guide
    • C++ Programming: Visual QuickStart Guide
    • C Programming: Visual QuickStart Guide
    • Adobe AIR: Visual QuickPro Guide
  • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (5th Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (4th Edition)
    • PHP 6 and MySQL 5 for Dynamic Web Sites: Visual QuickPro Guide (3rd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (2nd Edition)
    • PHP and MySQL for Dynamic Web Sites: Visual QuickPro Guide (1st Edition)
  • PHP for the Web: Visual QuickStart Guide
    • PHP for the Web: Visual QuickStart Guide (5th Edition)
    • PHP for the Web: Visual QuickStart Guide (4th Edition)
    • PHP for the Web: Visual QuickStart Guide (3rd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (2nd Edition)
    • PHP for the World Wide Web: Visual QuickStart Guide (1st Edition)
  • Effortless E-commerce with PHP and MySQL
    • Effortless E-Commerce with PHP and MySQL (2nd Edition)
    • Effortless E-Commerce with PHP and MySQL
  • PHP Advanced: Visual QuickPro Guide
    • PHP Advanced and Object-Oriented Programming: Visual QuickPro Guide (3rd Edition)
    • PHP 5 Advanced: Visual QuickPro Guide (2nd Edition)
    • PHP Advanced: Visual QuickPro Guide
  • MySQL: Visual QuickStart Guide
    • MySQL: Visual QuickStart Guide (2nd Edition)
    • MySQL: Visual QuickStart Guide (1st Edition)
  • Other
    • Announcements
    • Newsletter, Blog, and Other Topics
    • Forum Issues
    • Social

Find results in...

Find results that contain...


Date Created

  • Start

    End


Last Updated

  • Start

    End


Filter by number of...

Joined

  • Start

    End


Group


Found 3 results

  1. 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.
  2. 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. Greetings, I think I understand the logic flow inherit in nested foreach loops. However, I'd like to be sure. I've annotated a nested foreach from this book with my understanding of its function commented in with the code. Insights would be appreciated. Thanks
×
×
  • Create New...