Generated array values changing after While statements - php

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

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>";
}

Related

Having problems coding if and counter

I am trying to figure out how to do two things. One Query a database and get a record count (NO PROBLEM WITH THIS) Next I want to display a limited number of record on a webpage. this is where I get stuck. I am pretty sure I need to add a counter to the while loop but I keep having a problem only displaying one record 1000's of times.
if ($result->num_rows > 0) {
// output data of each row
echo "<center><table>";
echo "<tr>";
echo "<th>Legal Business Name</th>";
echo "<th>DBA Name</th>";
echo "<th>Business Address</th>";
echo "<th>Website Address</th>";
echo "<th>Business Government POC</th>";
echo "</tr>";
while($row = $result->fetch_assoc()) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
}
echo "</table>";
Ok I figured this out. Sometimes you need a bigger hammer!
I added in a counter and created an if statement right after the while loop.
$int = 0;
while($row = $result->fetch_assoc()) {
if ($int < 10) {
echo "<tr><td>" . $row["LEGAL_BUSINESS_NAME"]. "</td>";
echo "<td>" . $row["DBA_NAME"]. "</td>";
echo "<td>" . $row["PHYSICAL_ADDRESS_LINE_1"]. "<br>" . $row["PHYSICAL_ADDRESS_LINE_2"]. "<br>" . $row["PHYSICAL_ADDRESS_CITY"]. ", " . $row["PHYSICAL_ADDRESS_PROVINCE_OR_STATE"]. " " . $row["PHYSICAL_ADDRESS_ZIP_POSTAL_CODE"]. "+" . $row["PHYSICAL_ADDRESS_ZIP_CODE_PLUS_4"]. " " . $row["PHYSICAL_ADDRESS_COUNTRY_CODE"]. "</td>";
echo "<td>" . $row["CORPORATE_URL"]. "</td>";
echo "<td>" . $row["GOVT_BUS_POC_FIRST_NAME"]. " " . $row["GOVT_BUS_POC_MIDDLE_INITIAL"]. " " . $row["GOVT_BUS_POC_LAST_NAME"]. "<br> P: ". $row["GOVT_BUS_POC_US_PHONE"]. "<br> E: ". $row["GOVT_BUS_POC_EMAIL"]. "</td>";
echo "</tr>";
$int = $int + 1;
}
}

Retrieving employee salaries with mysql and php using nymber_format

I am returning employee information from a musql db using php. I am getting the salary to returned with a formatted comma using the number_format() method but when the data is returned, all employees have the same salary. How do I get php to return individual salaries from the employees table?
PHP/MySQL
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
number_format doesn't work on an array. Do it like this:
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
//$salary = $row['salary'];
//$rows_sal = number_format($salary);
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . number_format($row['salary']) . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
That's because you're working with the salary outside the foreach loop. (And, by the way, you're leaving the first row out of the table).
Before I suggest you how to do it, let me explain what it's happening with your actual code: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//here you're asking THE FIRST row:
$row = mysqli_fetch_array($results, MYSQL_BOTH) or die(mysql_error());
$salary = $row['salary'];
//so $rows_sal is the first salary. All this have nothing to do with the iteration below.
$rows_sal = number_format($salary);
echo("<table>");
//here you're asking for the next results (leaving 1st row outside the table!)
while($row = mysqli_fetch_array($results, MYSQL_BOTH))
{
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
//and here you're referencing the value set before the while.
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Now this is what you should do: (follow the comments)
<?php
require_once("db.php");
$sql = "SELECT `*` FROM `employees`";
$results = mysqli_query($connect, $sql) or die(mysql_error());
//don't ask for results yet!
echo("<table>");
while($row = mysqli_fetch_array($results, MYSQL_BOTH)) //including 1st row
{
//now $row is EACH row, NOW you can format your salary:
$salary = $row['salary'];
$rows_sal = number_format($salary);
echo("<tr>");
echo "<td>" . $row['empid'] . '</td>' .
"<td>" . $row['lastname'] . '</td>' .
"<td>" . $row['firstname'] . '</td>' .
"<td>" . $row['department'] . '</td>' .
"<td>" . $row['position'] . '</td>' .
"<td>" . $rows_sal . '</td>';
echo '</br>';
echo('</tr>');
}
echo("</table>");
?>
Hope it helps.

PHP/SQL Per-Person Search

How would i do a 'per-person search' using PHP? I have it at the moment working, but i want to be able to go to this: /profile/JohnSmith, or something similar.
At the moment i have it like this (probs not the most efficient way but it works):
$sql = "SELECT * FROM data_players_sg WHERE player LIKE '%" . $name . "%'";
$result = $conn->query($sql);
echo "<br /><h3>Survival Games Stats</h2>";
echo "<div id=containter class=CSSTableGenerator>";
echo "<table id=player_profile cellspacing=15><tr><th>Points</th><th>Wins</th><th>Losses</th><th>Kills</th><th>Deaths</th><th>KDR</th></tr>";
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
if($row["player"] == $name) {
$UUID = $row["uuid"];
echo "<tr><td>" . $row["points"] . "</td>";
echo "<td>" . $row["wins"] . "</td>";
echo "<td>" . $row["losses"] . "</td>";
$kills = $row["kills"];
$deaths = $row["deaths"];
$kdr = $deaths != 0 ? $kills / $deaths : $kills;
echo "<td>" . $kills. "</td>";
echo "<td>" . $deaths. "</td>";
echo "<td>" . $kdr. "</td></tr>";
}
}
} else {
echo "<tr><td>Player not found</td><td></td><td></td><td></td></tr>";
}
echo "</table></div>";
Which is called when they search, and this works fine. Except it means you have to search in the search bar every time you want to get to this page, instead of being able to go straight to the page with a URL.
So in short i want it to create a page per person, which is their profile.
First of all, you can start by creating a new page, called profile.php or something similar. Next, use a combination of a .htaccess rewrite of the url (for example, example.com/player-name will be rewritten to example.com/profile.php?url=player-name, and a GET statement in your profile.php to retrieve this player-name. Use this name, id or url to match with data in your database. Use a query like this:
$x=$_GET["url"];
$query = "SELECT * FROM data_players_sg WHERE playername =".$x;
if ($result = mysqli_query($link, $query))
{
while ($row = mysqli_fetch_assoc($result))
{
$UUID = $row["uuid"];
echo "<tr><td>" . $row["points"] . "</td>";
echo "<td>" . $row["wins"] . "</td>";
echo "<td>" . $row["losses"] . "</td>";
$kills = $row["kills"];
$deaths = $row["deaths"];
$kdr = $deaths != 0 ? $kills / $deaths : $kills;
echo "<td>" . $kills. "</td>";
echo "<td>" . $deaths. "</td>";
echo "<td>" . $kdr. "</td></tr>";
}
}
Hope this helps.
Best regards,
Motbrok

php foreach get all data from database table

I would like to get all the values of: echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>"; Some how it only returns the first on. After that i would like to calculate the sum of them.
At the moment the code only gets the first.
echo "<table class=\"zebra\">";
$sum1=0;
$sum2=0;
$numbering =1;
$query2 = "SELECT pisteet_1 As PIY, pisteet_2 as PIK, nimi As NIM, opisto As OPI, pisteet.kaupunki_id As KA FROM
pisteet INNER JOIN joukkueet ON joukkueet.id = pisteet.team_id INNER JOIN oppilaitokset ON oppilaitokset.opisto_id = joukkueet.opisto_id GROUP BY nimi, team_id ORDER BY team_id ASC";
foreach ($db->query($query2) as $row2) {
echo "<tr class=\"all " . $row2['KA'] . "\">";
echo "<td>" . $numbering . "</td>";
echo "<td>" . $row2['NIM'] ."<span>" . $row2['OPI'] ."</span></td>";
//--------should get all the points----------
echo "<td class=\"points\">" . $row2['PIY'] . "/" . $row2['PIK'] . "</td>";
$sum1 +=$row2['PIY'];
$sum2 +=$row2['PIK'];
echo '<td class="Sum">'.$sum1.'/'.$sum2."</td>";
//-------------------------------------------
echo "</tr>";
$numbering ++;
}
echo '</table>';

Sub total according to date

i try to solve a problem but did not succeed.
I have a index page and users search. for example
What i want to do is to have subtotal. I mean i want to have a total according to day.
for example user search for 2012-02-22 and 2012-02-23 but i need total for 22 and 23 of month .
To do that: I did this it calculate on t
while ($info = mysql_fetch_array($dbResult)) {
$total+= $info['totalEvents'];
$total2 = 0;
echo "<tr>";
echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "' onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td>
<label id ='nameID'><td>" . $info['id'] . " " . $info['profileName'] . "</td>";
echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>";
echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>";
if ($info['Date'] != $previousDate) {
echo "<td><b>" . $info['Date'] . "</b></td></tr>";
$total2++;
} else {
echo "<td></td>";
}
$previousDate = $info['Date'];
}
but it calculates like that
finally i come to this
but i calculates wrongly
else if ($_POST['group1'] == 'VideoFinish') {
$dbResult = mysql_query("SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory FROM videofinish la INNER JOIN profiles st ON st.id=la.id INNER JOIN eventcategory ft ON ft.id = la.eventCategoryID where Date BETWEEN '" . $startDate . "' and '" . $endDate . "'");
if (isset($_POST['checkPremium'])) {
$dbResult = mysql_query("SELECT la.id,st.profileName, la.totalEvents,la.Date,ft.eventCategory FROM videofinish la INNER JOIN profiles st ON st.id=la.id and st.isPremium=1 INNER JOIN eventcategory ft ON ft.id = la.eventCategoryID where Date BETWEEN '" . $startDate . "' and '" . $endDate . "'");
}
$previousDate = '';
$totalDay = 0;
while ($info = mysql_fetch_array($dbResult)) {
$total+= $info['totalEvents'];
echo "<tr>";
echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "' onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td>
<label id ='nameID'><td>" . $info['id'] . " " . $info['profileName'] . "</td>";
echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>";
echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>";
if ($info['Date'] != $previousDate && $info['Date'] != $previousDate) {
echo "<td><b>" . $info['Date'] . "</b></td></tr>";
echo "<b>".$info['Date']."</b>:";
echo $totalDay."<br />";
} else {
echo "<td></td>";
}
$previousDate = $info['Date'];
$totalDay += $info['totalEvents'];
}
echo "<td><b id='total'>" . $total . "</b></td>";
}
Try this .. i put comments in the code for your understanding.
// initialize counter and previous date
$totalDay = 0;
$previousDate = NULL;
// we need to change the loop structure so that the loop code is executed
// a last time when there are no further rows. Otherwise, we would have
// no total on the last day!
while (true) {
// we break the loop when there are no further rows after sub total is printed
$info = mysql_fetch_array($dbResult);
// when date changes, print total line and reset counter
if ( $previousDate <> NULL &&
( !isset($info['Date']) || $info['Date'] <> $previousDate ) ) {
echo '<tr>';
// no checkbox
echo '<td></td>';
// label in second cell
echo '<td><b>Total for day ' . $previousDate .'</b></td>';
// total in last cell
echo '<td colspan="4" align="right"><b>' . $totalDay . '</b></td>';
echo '</tr>';
// day has changed, so reset the sub total for next day
$totalDay = 0;
}
// now if the have no further rows, break the loop
if ($info == false)
break;
// add current value to sub total
$totalDay += $info['totalEvents'];
echo "<tr>";
echo "<td><input type=checkbox name='check1' id='check1' value='" . $info['eventCategory'] . "'
onclick=recal(" . $info['totalEvents'] . ",this.checked) checked></td>";
echo "<label id ='nameID'><td>" . $info['id'] . " " . $info['profileName'] . "</td>";
echo "<td><label id='eventCategory'>" . $info['eventCategory'] . "</td>";
echo "<td><label id='totalEvents'>" . $info['totalEvents'] . "</label></td>";
echo "<td>";
// when date changes (also on first day), print date
if ($info['Date'] <> $previousDate) {
echo "<b>" . $info['Date'] . "</b>";
}
echo "</td>";
echo "</tr>";
// remember current date for next row calculation
$previousDate = $info['Date'];
}
You can do GROUP BY your_date_field WHERE your_date_field in (22,23)` in your mysql statement.
try this query
select * from table
GROUP BY date_column
WHERE date_column between "2001-01-05" and "2001-01-10"

Categories