Jump to content

Help needed with Array?


Solarpitch

Recommended Posts

Hi,

 

I need to create an array to hold a series of hours of the day. I want each array value to hold a start and finish time. So for example...

 

$array['hours'] = array ($start => '6:00', $finish => '7:00')
$array['hours'] = array ($start => '8:00', $finish => '9:00')
$array['hours'] = array ($start => '9:00', $finish => '10:00')

 

Is something like this possible? Becasue I then want to loop through the array for each value and run this query:

 

function hourly_breakdown($date, $start, $finish)
{   
$query = $this->client->query('SELECT SUM(price) as hour_total FROM golfpro_barsales WHERE date = '.$date.' AND time > '.$start.' AND time < '.$finish.' GROUP BY `price`');
$row = $query->row();
return $row->hour_total;
}

 

This is so I can get a breakdown of how much was sold for each hour in the system. So effectively I will then format it like:

 

6:00 - 7:00: Total €245.00

7:00 - 8:00: Total €124.00 .. etc

 

Link to comment
https://forums.phpfreaks.com/topic/140648-help-needed-with-array/
Share on other sites

Actually, you could probably accomplish this with just a mysql query...

http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_hour

 

It's probably very similar to this thread: http://www.phpfreaks.com/forums/index.php/topic,233831.0.html

 

 

 

Otherwise:

$hours = array(
  array ("start" => '6:00', "finish" => '7:00'),
  array ("start" => '8:00', "finish" => '9:00')
)

Thanks for that lads,

 

@xtopolis ... could probably use the hour format thing, but for example... if I got the value from 8:00 - 9:00.. then next value would need to be 9:01 - 10:00 so it might not work.

 

In the example you shown, how would I loop through this array then? I might be way off here but would it be like...

 

<?php

foreach($hours as $e)
{
$start = $e->start;
$finish = $e->finish;

$query = $this->client->query('SELECT SUM(price) as hour_total FROM golfpro_barsales WHERE date = '.$date.' AND time > '.$start.' AND time < '.$finish.' GROUP BY `price`');
$row = $query->row();
return $row->hour_total;


}

?>

You need to set variable's. So you could use something like this similar to the array's.

 

<?php

$hours = array
(
1 	=> 	array
		(
			'start' 	=> '6:00',
			'finish'	=> '7:00',
		),
2	=>	array
		(
			'start'		=> '8:00',
			'finish'	=> '9:00',
		)
);

$rows = count($hours);
for ($i = 1; $i <= $rows; $i++)
{
$query = $this->client->query('SELECT SUM(`price`) AS `hour_total` FROM `golfpro_barsales` WHERE `date`='.$date.' AND `time` > '.$hours[$i]['start'].' AND `time` < '.$hours[$i]['finish'].' GROUP BY `price`');
$row   = $query->row();
echo $row->hour_total.'<br>';
}

?>

 

Hi Thanks for that,

 

I came up with a simpler solution...

 


<?php

	$hours = array(
	array ("start" => '14:00:00', "finish" => '15:00:00'),
	array ("start" => '15:00:00', "finish" => '16:00:00')
	);

	foreach($hours as $e)
	{
	$start = $e['start']; $finish = $e['finish'];
	$hour_total = $this->report_model->hourly_breakdown($date, $start, $finish);
                }

?>

As for mine you can use this:

 

<?php
$hours = array
(
   1    =>    array
         (
            'start'    => '6:00',
            'finish'   => '7:00',
         ),
   2   =>   array
         (
            'start'      => '8:00',
            'finish'   => '9:00',
         )
);

echo '<pre>';
print_r($hours);
echo '</pre>';

?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.