Note that there are some explanatory texts on larger screens.

plurals
  1. POPHP array to SQL string using foreach loop
    primarykey
    data
    text
    <p>Okay let me start all over again.</p> <p>First we have three tables:</p> <p>Table: transaction</p> <pre><code> meter_id | bay_number | trans_date_time | amount ----------+------------+----------------------------+-------- 1078 | 5 | 2013-06-03 09:59:32+10 | 5.00 1078 | 7 | 2013-06-03 09:12:01+10 | 5.00 1078 | 6 | 2013-06-03 08:33:11+10 | 5.00 1078 | 8 | 2013-05-27 09:48:26+10 | 5.00 1078 | 5 | 2013-05-27 09:41:42+10 | 5.00 1078 | 4 | 2013-05-27 09:41:08+10 | 5.00 1078 | 7 | 2013-05-20 17:14:15+10 | 2.00 1078 | 2 | 2013-05-20 16:19:10+10 | 2.50 </code></pre> <p>Table: service</p> <pre><code> meter_id | notified_at | finished_at ----------+------------------------+------------------------ 1078 | 2013-05-30 15:02:27+10 | 2013-05-30 15:32:20+10 1078 | 2013-05-30 12:32:43+10 | 2013-05-30 14:50:14+10 1078 | 2013-05-30 08:31:27+10 | 2013-05-30 10:25:56+10 1078 | 2013-05-29 07:36:31+10 | 2013-05-29 11:35:47+10 </code></pre> <p>Table: relocated_meter</p> <pre><code> relocation_date | meter_id | bay_number | source_meter_id | source_bay_number -----------------+----------+------------+-----------------+------------------- 2013-04-24 | 1078 | 1 | 1078 | 1 2013-04-24 | 1078 | 2 | 1078 | 2 2013-04-24 | 1078 | 3 | 1078 | 3 2013-04-24 | 1078 | 4 | 1078 | 4 2013-04-24 | 1078 | 5 | 1078 | 5 2013-04-24 | 1078 | 6 | 1078 | 6 2013-04-24 | 1078 | 7 | 1078 | 7 2013-04-24 | 1078 | 8 | 1067 | 5 2013-04-24 | 1078 | 9 | 1067 | 6 </code></pre> <p>What I am trying to write is some elegant function/s inside a PHP class to generate the following:</p> <p>For each service records, what is the income received by each meter? Then, generate income for the the last 52 weeks (eg, if the meter 1078 was down from 10:00 to 10:30 this tuesday, what is the income for the same meter on last tuesday between 10:00 and 10:30 and the week before that all through 52 weeks). This is so I get an annual average for each meter during the same time period.</p> <p>Now, a few meters have inherited some bays and with the introduction of the third table called relocated_meter, I'd like to get the weekly amount PLUS income for another meter (1067 bay 5 and 1067 bay 6) because they're now part of meter 1078 bay 8 and 9, so average income should include the other bays too for dates before relocated_date</p> <p>I hope this makes sense?</p> <hr> <p> <pre><code>function getWeeks() { $meter_combo = array( array(1234 =&gt; 1), array(1234 =&gt; 2), array(1234 =&gt; 3), array(1234 =&gt; 4), ); $notified_time = '2013-05-17 12:20:48'; $completed_time = '2013-05-17 12:52:07'; $relocation_date = '2013-04-30 00:00'; $old_meter_combo = array( array(4444 =&gt; 1), array(4444 =&gt; 2), array(4444 =&gt; 8), array(4444 =&gt; 9), ); $notified_time = strtotime($notified_time); $completed_time = strtotime($completed_time); $relocation_date = date($relocation_date); $weeks = 2; $combined = array(); for($i=0;$i&lt;=$weeks;$i++) { $start_series = strtotime("- $i weeks", $notified_time); $end_series = strtotime("- $i weeks", $completed_time); if(strtotime($relocation_date) &lt; $start_series) { $combined[] = array( 'week_id' =&gt; $i, 'start_time' =&gt; date('Y-m-d H:i:s',$start_series), 'end_time' =&gt; date('Y-m-d H:i:s',$end_series), 'relocation' =&gt; $relocation_date, 'meter_id' =&gt; $meter_combo, ); } else { $combined[] = array( 'week_id' =&gt; $i, 'start_time' =&gt; date('Y-m-d H:i:s',$start_series), 'end_time' =&gt; date('Y-m-d H:i:s',$end_series), 'relocation' =&gt; $relocation_date, 'meter_id' =&gt; $old_meter_combo, ); } } $sql1 = "SUM(CASE WHEN ("; foreach($combined as $k1 =&gt; $v1) { $sql2 = "AND trans_date_time BETWEEN " . $v1['start_time'] . " AND " .$v1['end_time']. "&lt;br/&gt;" ; $sql3 = "THEN amount ELSE 0 END) AS week_" . $v1['week_id'] . "&lt;br /&gt;" ; foreach ($v1['meter_id'] as $v2) { foreach ($v2 as $meter =&gt; $bay) { $sql4[] = "meter_id = $meter and bay_number = $bay &lt;br/&gt;"; } } } $sql4 = implode(" OR ", $sql4); echo $sql1.$sql4.$sql2.$sql3; } </code></pre> <p>Any other elegant want of doing this is greatly welcomed :)</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload