0

This is a weird issue I have came across and was wondering if anyone my have insight. Not sure if the mktime does not function as I am trying to get it to, or what may be going on.

Last night, things were working fine - the months being displayed were correct. Today, though, for some reason the values of my $aGMonV are changing somewhere after the foreach and before the while(row_* = mysqli_fetch_array* statements.

While the var_dump returns %2014-03% as the first month (which is correct) - the table that is generated returns %2013-09% as the first month. All the queries being ran are being run with %2013-09% and NOT starting at current month.

My code is below:

$aGMon = array();

for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }

foreach ($aGMon as $aGMonK => $aGMonV)

{

$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";

$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " .     $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV     . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as     Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData     from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill     LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF     from CF6MCI where Date LIKE " . $aGMonV . ";");

var_dump($aGMonV);

while($row_E = mysqli_fetch_array($result_E))
while($row_G = mysqli_fetch_array($result_G))
while($row_P = mysqli_fetch_array($result_P))
while($row_T = mysqli_fetch_array($result_T))

{

echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: "     . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";

}

}

Results of the code are as follows:

Result of code

3
  • Do you understand how while works? Do you realize what is the statement to loop for the outer while? Commented Mar 24, 2014 at 1:06
  • I guess I don't follow. I have never had issues with multiple whiles executing properly - I was thinking since the while is in the foreach loop, the foreach loop would loop through each array value, perform the 4 whiles, and then loop to the next array value, so on until it was done looping through the array. Commented Mar 24, 2014 at 1:22
  • a hint: every while performs independently Commented Mar 24, 2014 at 1:28

1 Answer 1

2

user3260912 try removing the while, try like this:

$aGMon = array();

for ($i = 0; $i < 20; $i++)
{ $aGMon[] = date('Y-m', mktime(0,0,0,date('n')-$i,1)); }

foreach ($aGMon as $aGMonK => $aGMonV)

{

$aGMonO = $aGMonV;
$aGMonV = " '%" . $aGMonV . "%' ";

$result_E = mysqli_query($con,"select kWh_AVG from UseElecM where Month LIKE " .     $aGMonV . ";");
$result_G = mysqli_query($con,"select TotalMCF from UseGas where Month LIKE " . $aGMonV     . ";");
$result_P = mysqli_query($con,"select (A.Minutes+E.Minutes_L500+E.Minutes_Free) as     Minutes, (A.Texts+E.Texts) as Texts, (A.MMS+E.MMS) as MMS, (A.MBData+E.MBData) as MBData     from UseSprintA A left outer join UseSprintE E on A.Bill = E.Bill where A.Bill     LIKE " . $aGMonV . ";");
$result_T = mysqli_query($con,"select cast((avg(Average)) as decimal (10,1)) as ATF     from CF6MCI where Date LIKE " . $aGMonV . ";");

//var_dump($aGMonV);

$row_E = mysqli_fetch_array($result_E);
$row_G = mysqli_fetch_array($result_G);
$row_P = mysqli_fetch_array($result_P);
$row_T = mysqli_fetch_array($result_T);



echo "<td class='UUMonth'>" . ($aGMonO) . "<div class='UUMonthO'>Average temperature: "     . $row_T['ATF'] . " F</div></td>";
echo "<td>" . $row_E['kWh_AVG'] . "</td>";
echo "<td>" . $row_G['TotalMCF'] . "</td>";
echo "<td>" . $row_P['Minutes'] . "</td>";
echo "<td>" . $row_P['Texts'] . "</td>";
echo "<td>" . $row_P['MMS'] . "</td>";
echo "<td>" . $row_P['MBData'] . "</td>";
echo "</tr>";



}
Sign up to request clarification or add additional context in comments.

1 Comment

That worked perfectly. I'm also going to try joining the queries to return a single result. For the database I have, performance isn't an issue yet.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.