[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 226
  • Last Modified:

Base PhP questions Why use Foreach over just a basic loop?

Hi Experts - I have been trying to learn some PHP to integrate some of our in-house applications with the major application used by our company (Written by others) - I have come across a few basic questions and I seem to be having a problem getting an answer to.

Most of our (ok, all) of our work revolves around getting data from a database and displaying it to the user - I have been using APEX for oracle, so I'm much more familiar with that than anything in PHP.

When I look at some of the reports that are written by the company, I notice when they are doing total counts of some of our tables (which are fairly large, 60-90 columns each with a few million rows per table) they are using a "Foreach" - (I will include a snippit of code where they are counting the days of the week an incident occurs)

Couldn't you just do this in the "main" while loop with an IF statement?(they use case)
What is the value add of doing the foreach?  Is it a "best practice"?

Is this a question you can answer with the provided information or do I need to provide more?

Thanks
while($row=$result->fetchrow(DB_FETCHMODE_ASSOC))

		{

			$district =substr($row["rm4_report_district"],0,2);

			$da = substr($row["rm4_report_date"],6,2);

			$mo = substr($row["rm4_report_date"],4,2);

			$yr = substr($row["rm4_report_date"],0,4);

			$day_of_week = date("l", mktime(0, 0, 0, $mo, $da, $yr));

			if ($firstime == "Y"){

				$district_ar[$num] = $district;

				$firstime = "N";

			}

			$found_district="N";

			$count_array=0;

			$day_of_week = strtolower($day_of_week);

			debug_write("---------report date------=".$row["rm4_report_date"]);

			debug_write("---------day of week after------=".$day_of_week);

			foreach ($district_ar as $key => $choice){

				if ($district == $choice){

					$count_array = $key;

					$found_district="Y";

					$num_of_off[$key] = $num_of_off[$key] +1;

					switch ($day_of_week)

					{

						case "monday":

							$monday[$key] = $monday[$key] + 1;

							$total_monday = $total_monday + 1;

							break;

						case "tuesday":

							$tuesday[$key] = $tuesday[$key] + 1;

							$total_tuesday = $total_tuesday + 1;

							break;

						case "wednesday":

Open in new window

0
hej613
Asked:
hej613
  • 2
2 Solutions
 
Aaron TomoskyTechnology ConsultantCommented:
The while loop is for each row. The for each is on the district array. So or each row it loops through the district array and matches some stuff. This looks really inefficient to me at first glance.
0
 
hej613Author Commented:
Understand - I know very little about PHP programming, but I have had to re-write a lot of PL/SQL for oracle because everything is very "bloated"  - I would not doubt if the php is like that also...
0
 
Aaron TomoskyTechnology ConsultantCommented:
I frequently find in these situations that modifying the query to return the data they want instead of using php to loop a bunch of times will significantly speed thing up.
0
 
Ray PaseurCommented:
What Aaron said at ID:36994171!

Here is the man page that describes foreach()
http://php.net/manual/en/control-structures.foreach.php

And here is an article showing how to handle DATETIME values in PHP and MySQL.  Hint: You don't do it the way that programming shows!
http://www.experts-exchange.com/Web_Development/Web_Languages-Standards/PHP/A_201-Handling-date-and-time-in-PHP-and-MySQL.html
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now