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"
Related
the below code is getting some values from DB by "select option form" , i recently added Pagination snip to limit the results, when i run the code it fetch 5 recorders as defined,but didn't show the remaining number of pages.
what im doing wrong here ?
<?php
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_POST['form_val']) && isset($_POST['form_val'])) {
$_POST['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_POST['Location']) && isset($_POST['Location'])) {
$sql = $sql . " AND location =" . $_POST['Location'];
}
$strtdate = $_POST['Sday'];
$enddate = $_POST['Eday'];
if (!empty($_POST['Sday']) && isset($_POST['Sday']) && !empty($_POST['Eday']) && isset($_POST['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_POST['Sday']) && isset($_POST['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_POST['Eday']) && isset($_POST['Eday']))
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_POST['Site']) && isset($_POST['Site'])) {
$sql = $sql . " AND u.site=" . $_POST['Site'];
}
$sql = $sql . " LIMIT $start_from, $per_page";
if (mysqli_query($conn, $sql)) {
$result = mysqli_query($conn, $sql);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$total_pages = ceil($rowcount / $per_page);
echo "<center><a href='?page=1'>" . 'First Page' . "</a> ";
for ($i = 1; $i <= $total_pages; $i++) {
echo "<a href='?page=" . $i . "'>" . $i . "</a> ";
}
echo "<a href='?page=$total_pages'>" . 'Last Page' . "</a></center> ";
} else {
echo '<p>No Results Found !!!</p>';
}
}
}
?>
As I said in my comments, for displaying pagination links:
You're counting total number of rows but incorporating LIMIT and OFFSET clauses in your SELECT query, this won't give the correct number of row count. Your SELECT query should not contain this part, ... LIMIT $start_from, $per_page.
Since you're filtering the results based on several $_POST data, you should incorporate those conditions in your pagination links as well, otherwise when you visit a different page(through pagination link), you won't get the desired result, and that's because $_POST data will not be retained when you hop from page to page. Better that you change the method of your <form> from POST to GET, because in this way it'd be easier for you to catch and manipulate things when you hop from one page to another using pagination links.
So based on the above points, your code should be like this:
$per_page = 5;
if (isset($_GET["page"])) {
$page = $_GET["page"];
} else {
$page = 1;
}
$start_from = ($page - 1) * $per_page;
if (!empty($_GET['form_val']) && isset($_GET['form_val'])) {
$_GET['form_val'] = 0;
$sql = "SELECT u.log_id , u.user_name, s.site, u.date ,u.comment , l.location, e.picture FROM `pool` u, `location_all` l , `site_all` s JOIN db2.user e
where l.location_id = u.location and s.site_id = u.site and e.user_id = u.user_id";
if (!empty($_GET['Location']) && isset($_GET['Location'])) {
$sql = $sql . " AND location =" . $_GET['Location'];
}
$strtdate = $_GET['Sday'];
$enddate = $_GET['Eday'];
if (!empty($_GET['Sday']) && isset($_GET['Sday']) && !empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date between '" . $strtdate . "' and '" . $enddate . "'";
} elseif (!empty($_GET['Sday']) && isset($_GET['Sday'])) {
$sql = $sql . " AND date>='" . $strtdate . "'";
} elseif (!empty($_GET['Eday']) && isset($_GET['Eday'])) {
$sql = $sql . " AND date<='" . $enddate . "'";
}
if (!empty($_GET['Site']) && isset($_GET['Site'])) {
$sql = $sql . " AND u.site=" . $_GET['Site'];
}
$data_query = $sql . " LIMIT $start_from, $per_page";
$result = mysqli_query($conn, $data_query);
if (mysqli_num_rows($result) >= 1) {
$rowcount = mysqli_num_rows($result);
echo '<legend> ' . $rowcount . ' Records Found !!!</legend>';
echo '<br><br>';
echo "<table class='srchtable'>
<tr>
<th>Picture</th>
<th>Date</th>
<th>User Name</th>
<th>country</th>
<th>Location</th>
<th>Site</th>
<th>Comment</th>
</tr>";
while ($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td> <img src='" . $row['picture'] . "' alt='' style='width:70%; height:auto; border-radius: 50%;'> </td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['user_name'] . "</td>";
echo "<td>" . $row['country'] . "</td>";
echo "<td>" . $row['location'] . "</td>";
echo "<td>" . $row['site'] . "</td>";
echo "<td>" . $row['comment'] . "</td>";
echo "</tr>";
}
echo "</table>";
$query_result = mysqli_query($conn, $sql);
$total_rows = mysqli_num_rows($query_result);
$total_pages = ceil($total_rows / $per_page);
parse_str($_SERVER["QUERY_STRING"], $url_array);
unset($url_array['page']);
$url = http_build_query($url_array);
?>
<center>First Page
<?php
for ($i = 1; $i <= $total_pages; $i++) {
?>
<?php echo $i; ?>
<?php
}
?>
Last Page</center>
<?php
} else {
echo '<p>No Results Found !!!</p>';
}
}
How can I put identical columns (sorted by "level" in this case) together? I am making a highscore where I list them by level from my database. I want them to have the same ID if they are the same level.
But I do not want to display the ID on the others. Only the first one.
Here is an example:
ID - Name - Level
1 - John - 5
2 - David - 4
3 - Josh - 3
- Sam - 3
4 - George - 2
So I want to put them together, but if they have the same level, only the first one displays the ID.
I don't want it to look like:
1 - John - 5
2 - David - 4
3 - Josh - 3
3 - Sam - 3
4 - George - 2
Right now, it is just listing everyone, and giving each one a unique ID. Even if they have the same "level".
How can I fix this? Here is my code:
<?php
$sql = mysql_query("SELECT * FROM rookstayers ORDER BY level DESC LIMIT 0, 500");
$id = 1;
while($row = mysql_fetch_array($sql)){
$name = $row['name'];
$level = $row['level'];
$world = $row['world'];
$account = $row['accountstatus'];
$status = $row['onlinestatus'];
$onrow = '';
$typeServ = '';
$player_name = urlencode($name);
if ($status == 1){
$status = 'Online';
$onrow = 'online';
} else {
$status = 'Offline';
$onrow = 'offline';
}
if ($account == 'Premium Account'){
$account = 'Premium';
} else {
$account = 'Free';
}
if ($world == 'Aurora' || $world == 'Aurera'){
$typeServ = 'active';
} else {
$typeServ = '';
}
echo "<tr class=" . $typeServ . ">";
echo "<td>" . $id . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
$id++;
}
echo "</tbody>";
echo "</table>";
?>
you can create a temporary variable for the current level and check it to see if the id will be shown on the output or not.
$id = 1;
$last_player_lvl = '';
while($row = mysql_fetch_array($sql)){
///....
echo "<tr class=" . $typeServ . ">";
echo "<td>" . ( ($last_player_lvl == $row['level']) ? '' : $id ) . "</td>";
echo "<td>" . $name . " <a href='http://www.tibia.com/community/?subtopic=characters&name=" . $player_name . "' target='_blank'><img src='../../img/websites/tibia.png' title='Tibia Profile'></a><a href='http://www.pskonejott.com/otc_display.php?character=" . $player_name . "' target='_blank'><img src='../../img/websites/pskonejott.png' title='Pskonejott'></a>" . "</td>";
echo "<td>" . $level . "</td>";
echo "<td>" . $world . "</td>";
echo "<td>" . $account . "</td>";
echo "<td class=" . $onrow . ">" . $status . "</td>";
echo "</tr>";
if($last_player_lvl == $row['level']){
$id = $id;
}else{
$id++;
}
$last_player_lvl = $row['level'];
//....
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>';
Here you can see the code I use to display a table from a MySQL database in a PHP webpage:
<?php
$con=mysqli_connect("localhost","aaaaaa","bbbbb","my_mk7vrlist");
$x = 1;
$result = mysqli_query($con,"SELECT * FROM 0_vrs_japan ORDER BY `vrs` DESC, `date` ASC");
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $x . "</td>";
echo "<td>" . $row['playername'] . "</td>";
echo "<td><img src='http://mk7vrlist.altervista.org/flags/" . $row['Country'] . ".gif' /></td>";
echo "<td>" . $row['contactable'] . "</td>";
echo "<td>" . $row['vrs'] . "</td>";
if($row['date'] != "-"){
$formatted = date('jS F Y', strtotime($row['date']));
} else {
$formatted = "-";
}
if($row['pic'] != "-"){
echo "<td>" . $formatted . "</td>";
} else {
echo "<td>" . $formatted . "</td>";
}
echo "</tr>";
$x = $x+1;
}
mysqli_close($con);
?>
And the result is this:
Every player with 99'999 points (which are called 'vrs') is sorted by the date. I am having troubles with that player I have just added.
His date 2014/05/22 in the database isn't in the correct position (after Megaman). Do you have any idea?
You're storing the date as a varchar so MySQL is having trouble sorting it in the way you need it to, as it's sorting lexicographically with the field being a varchar.
Convert them to proper date fields and natural date sorting will work as intended.
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>";
}