I am trying to rewrite this messy code, so that I only make one database query and eliminate the for loop. I am hoping the code will be faster with one query instead of two.
The loop exists for one reason: The date attribute, which is either “Today” or “Tomorrow” followed by the formatted date.
The main point here is that I want to keep the data structure (dayReport) the same. I want to know what date the result set belongs to (either “Today” or “Tomorrow”).
It seems silly to have a loop for that reason alone.
So here is the code. It’s in PHP, but really this is a language agnostic question:
for ($a=1; $a<=2; $a++)
{
$b = $a - 1;
$result = mysql_query("SELECT
name,
time,
date_format(time,'%M %d %Y %h:%i %p') as ftime,
date_format(time,'%l:%i %p') as ttime,
fee
FROM `foo_bar`
WHERE `cityId` = $cityId
AND time_utc > utc_timestamp()
AND time >= DATE_ADD(curdate(),INTERVAL $b day)
AND time < DATE_ADD(curdate(),INTERVAL $a day)
ORDER BY time ASC
" ) or die(mysql_error());
if ($result && mysql_num_rows($result) > 0)
{
$day = new Day();
$day->date = $a == 1 ? 'Today' . date(' - l, F d') : 'Tomorrow' . date(' - l, F d',strtotime('+'.$b.' day'));
$dayStuff = array();
while ($row = mysql_fetch_object($result))
{
$dayStuff[] = $row;
}
$day->foo = $dayStuff;
$dayReport[] = $day;
}
}
You could rewrite your query in this way, plain SQL no php code
Then you could completely remove the outer php for statement and use the
is_todayflag to distinguish between today and tomorrow records.For what regards the Day structure you can build two of them before the inner while and fill id according to the
is_todayflag, something like