I'm working on a sports website and ran into an issue. There is more than one game on different days and I'm only wanting the date to display once instead of once for every game to be played on that date.
For example: http://seohiosports.com/mastergirlsbasketballschedule2014.php
You will see immediately that there are two games listed on November 26. They are displayed like this:
November 26, 2013
Tri-Valley at Crooksville
November 26, 2013
Sheridan at John Glenn
I want it to be displayed like this:
November 26, 2013
Tri-Valley at Crooksville
Sheridan at John Glenn
Here is the code I'm working with, for some reason or another, I'm struggling to come up with the test/loop I need to make this work how I want it to.
if($num > 0)
{
while($row = mysql_fetch_array($r, MYSQLI_ASSOC))
{
echo '<b>'. date("F d, Y",strtotime($row['date'])) .' </b><br/>
'. $row['awayteam'] . ' '. $row['awayscore'] .' at '. $row['hometeam'] . ' '. $row['homescore'] .' <br/><br/>';
}
mysql_free_result($r);
}
Thanks in advance for any help. I think the answer is probably simple, but I'm struggling for one reason or another.
Thanks!
<? if ($num > 0)
{
$temp_date = '';
while ($row = mysql_fetch_array($r, MYSQLI_ASSOC))
{
if ($temp_date != $row['date']) {
$temp_date = $row['date'];
echo '<b>'. date("F d, Y",strtotime($row['date'])) .' </b><br/>';
}
echo $row['awayteam'] . ' '. $row['awayscore'] .' at '. $row['hometeam'] . ' '. $row['homescore'] .' <br/><br/>';
}
mysql_free_result($r);
}
<?php
if ($num > 0){
$dates = Array();
while ($row = mysql_fetch_array($r, MYSQLI_ASSOC)){
if(!in_array($row['date'], $dates)){
$dates[] = $row['date'];
echo $row['date'] . '</br>';
}
echo $row['awayteam'] . ' '. $row['awayscore'] .' at '. $row['hometeam'] . ' '. $row['homescore'] .' <br/><br/>';
}
mysql_free_result($r);
}
Related
I have a query which returns me some data.
//I loop through the query's result and write all data out
while($row =$result->fetch_assoc()){
echo $row['place'] . '|';
echo $row['gets_busy'] . '|';
echo $row['gets_empty '] . '|';
echo $row['time_between'] . '|';
echo "<br>";
}
ROOM01|2021-03-05 12:02:56|2021-03-05 12:04:02|00:01:06|
ROOM01|2021-03-05 12:05:42|2021-03-05 12:07:48|00:02:06|
ROOM01|2021-03-05 12:07:48|2021-03-05 12:12:54|00:05:06|
ROOM02|2021-03-05 12:15:54|2021-03-05 12:17:00|00:01:06|
ROOM02|2021-03-05 12:17:01|2021-03-05 12:23:17|00:05:16|
ROOM02|2021-03-05 12:23:59|2021-03-05 12:25:45|00:01:46|
I want something to write for the user like:
ROOM01 was busy for -> 00:08:18 during 2021-03-05
ROOM02 was busy for -> 00:08:08 during 2021-03-05
Can this be accomplished by PHP?
You can group into another array, by using the places and date as keys.
while($row = $result->fetch_assoc())
{
$place = $row['place']; // shortcut
$start = strtotime($row['gets_busy']); // start timestamp
$stop = strtotime($row['gets_empty']); // end timestamp
$time = $stop - $start; // duration in seconds
$date = date('Y-m-d', $start); // date for grouping
// group by place and date
if (!isset($data[$place][$date])) {
$data[$place][$date] = 0; // init with 0
}
$data[$place][$date] += $time; // add time.
}
// now, display data :
foreach ($data as $place => $dates) {
echo $place . ':<br>';
foreach ($dates as $date => $duration) {
$interval = new DateInterval('PT'.$duration.'S');
echo ' -> ' . $date . ' : ' . $interval->format('%h:%i:%s') . '<br>';
}
}
I don't know what are the errors of this code because it's suddenly not accurate the showing value to the user like this:
$holidays = array();
$query_holiday = "SELECT * FROM holiday_tbl";
$result = mysqli_query($dbcon, $query_holiday);
while($row_holiday = mysqli_fetch_assoc($result))
{
array_push($row_holiday, $holidays);
}
if(strtotime(date("Y-m-d")) > strtotime($row['due_date']))
{
$currentDate = date("Y-m-d");
$days = (strtotime($currentDate) - strtotime($row['due_date'])) / 86400;
$daysTemp = $days;
for($i=1;$i<$days;$i++)
{
$currentDay = date("D", strtotime("+ ".$i." days"));
$date_loop = date("Y-m-d", strtotime("+ ".$i." days"));
if($currentDay == "Sun" || $currentDay == "Sat")
{
$daysTemp--;
}
else if(in_array($date_loop, $holidays))
{
$daysTemp--;
}
}
echo $daysTemp;
}
The current implementation is rather convoluted. In these cases I find it's always better to start over and try to simplify the logic as much as possible. Here is my take on your problem:
function calculate_days_late($due_date = '', $holidays = array(), $current_date = 'today') {
$days_late = 0;
// Get the timestamps for due date and current date
$current_date_ts = strtotime($current_date);
$due_date_ts = strtotime($due_date);
// If current date is not after due date then not late
if ($current_date_ts <= $due_date_ts) {
return $days_late;
}
$loop_date_ts = $current_date_ts;
while ($loop_date_ts > $due_date_ts) {
// If the looping date is not a weekend or holiday then count it
if ( ! in_array(date('D', $loop_date_ts), array('Sat','Sun'))
&& ! in_array(date('Y-m-d', $loop_date_ts), $holidays)) {
$days_late++;
}
$loop_date_ts = strtotime('-1 day', $loop_date_ts);
}
return $days_late;
}
// Test
echo '2017-09-05 = ' . calculate_days_late('2017-09-05', array(), '2017-09-05') . "\n";
echo '2017-09-06 = ' . calculate_days_late('2017-09-05', array(), '2017-09-06') . "\n";
echo '2017-09-07 = ' . calculate_days_late('2017-09-05', array(), '2017-09-07') . "\n";
echo '2017-09-08 = ' . calculate_days_late('2017-09-05', array(), '2017-09-08') . "\n";
echo '2017-09-09 = ' . calculate_days_late('2017-09-05', array(), '2017-09-09') . "\n";
echo '2017-09-10 = ' . calculate_days_late('2017-09-05', array(), '2017-09-10') . "\n";
echo '2017-09-11 = ' . calculate_days_late('2017-09-05', array(), '2017-09-11') . "\n";
echo '2017-09-12 = ' . calculate_days_late('2017-09-05', array(), '2017-09-12') . "\n";
echo '2017-09-13 = ' . calculate_days_late('2017-09-05', array(), '2017-09-13') . "\n";
Working example: https://eval.in/856018
I have an interactive map application I am building and the database is relatively small at about 11mb. However it has a lot of rows of data the require me to go through sort them into counties and years.
My page load speeds are about 10-11 seconds trying to go through all of this data and get it organized so that I can then load it with a javascript library called mapael.
I need some help trying to get the page load speeds faster if there is any way to do that. The query doesn't really seem to be the problem. It's more the going through the data that I believe is causing the problem.
<?php
$mydb = new wpdb('uofimap_data','ob2UoV2X5tNz','uofimap_data','localhost');
$data = $mydb->get_results('
SELECT state,county,ship_date,total_cost, quantity FROM data
ORDER BY county ASC, ship_date ASC
');
$dataArray = array(
2006 => '',
2007 => '',
2008 => '',
2009 => '',
2010 => '',
2011 => '',
2012 => '',
2013 => '',
2014 => '',
);
$prevCounty = null;
$prevState = null;
$prevYear = null;
$countySum = null;
$quantitySum = null;
foreach ($data as $obj) :
//temp array to push everything into dataArray for year structure and keep all data in tact.
$tempArray = array();
//all the information to use for use
$date = date('Y', strtotime($obj->ship_date));
$state = $obj->state;
$county = $obj->county;
$cost = $obj->total_cost;
$quantity = $obj->quantity;
//Print only the needed values which is the sums of that county for that particular year.
if($prevCounty == $county){
if($prevYear == $date){
} else {
array_push($tempArray, $prevCounty, $prevState, $countySum, $quantitySum);
$dataArray[$prevYear][] = $tempArray;
}
} else {
if($prevCounty == null){
//Only needed if first value in database is a solo county / year.
//echo $county . ', ' . $state . ' : ' . $cost . ' ' . $date . ' New Null<br>';
} else {
array_push($tempArray, $prevCounty, $prevState, $countySum, $quantitySum);
$dataArray[$prevYear][] = $tempArray;
}
}
//Set everything
if($prevCounty == $county){
if($prevYear == $date){
$countySum += $cost;
$quantitySum += $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' Previous Year<br>';
} else {
$countySum = $cost;
$quantitySum += $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' Current Year<br>';
}
} else {
$countySum = $cost;
$quantitySum = $quantity;
//echo $county . ' ' . $countySum . ' '. $state . ' ' . $date . ' ' . $quantitySum . ' New County <br>';
}
$prevCounty = $county;
$prevState = $state;
$prevYear = $date;
endforeach;
$mapaelJson = '<script type="text/javascript">';
$mapaelJson .= 'data={';
foreach($dataArray as $key => $value){
$mapaelJson .= '"' . $key . '": {';
$mapaelJson .= '"areas":{';
foreach($value as $key1 => $value1){
$county = $value1[0];
$county = ucwords(strtolower($county));
$state = $value1[1];
$total = $value1[2];
$quantity = $value[3];
//var_dump($value1);
$mapaelJson .= '"'.$county . ', '. $state . '" : {';
$mapaelJson .= '"county" : "' . $county . '",';
$mapaelJson .= '"value" : "' . $total . '",';
$mapaelJson .= '"quantity" : "' . $quantity . '",';
$mapaelJson .= '},';
}
$mapaelJson .= '} },';
}
$mapaelJson .= '};</script>';
echo $mapaelJson;
?>
Have you looked into profilers? If you could create traces using the free version of Blackfire.io or cachegrind files using xdebug that would give you the best ideas to optimise.
Anyhow, for the time being you definitely need to reduce the number of loops and inner loops. Also, as much as you can, try having less indentations (keep the number of level low for more readability). You can use continue quite often in loops, it generally helps.
<?php
$result = $sth1->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row)
{
echo "<div class='listing'>";
print $row['uUName'] . '</strong><br />' .
'<strong>' . $row['listTitle'] . '</strong><br />' .
$arr = explode(':', $row['diff']);
echo "{$arr[0]} hours, {$arr[1]} minutes ago";
echo "</div>";
}
unset($sth1);
?>
Outputting
Array01 hours, 48 minutes ago
Array04 hours, 01 minutes ago
How do I get RID of :
Array01
Array04
from the beginning and just be left with:
01 hours, 48 minutes ago
04 hours, 01 minutes ago
<?php
$result = $sth1->fetchAll(PDO::FETCH_ASSOC);
foreach($result as $row)
{
echo "<div class='listing'>";
print $row['uUName'] . '</strong><br />' .
'<strong>' . $row['listTitle'] . '</strong><br />';
$arr = explode(':', $row['diff']);
echo "{$arr[0]} hours, {$arr[1]} minutes ago";
echo "</div>";
}
unset($sth1);
?>
Notice line 8, where I've removed your '. and replaced it with '; in the end.
Can someone show me how to change this date stamp and print this in an html table?
I have an input file with this time stamp format:
4-Start=20100901180002
This time format is stored like this in an array.
I print out the array like so to create an html table:
foreach ($data as $row){
$counter ++;
$class = $counter % 2 === 0 ? 'alt1' : 'alt2';
echo '<tr class="' . $class . '">';
foreach ($keys as $column)
if (isset($row[$column])){
echo '<td>' . $row[$column];
} else {
echo '<td>' . '' . '</td>';
}
}
echo '</table>';
How do I change the timestamp in this table to this? 2010-09-01 18:00:02
This is what you are looking for,
http://de2.php.net/manual/en/function.strtotime.php
There is a similar question you can get more inputs from there as well..
How do I format the date and time That is received from database
EDIT:
Yes you can use it an echo as well
echo date("Y-m-d H:i:s",strtotime('20100901180002')) ; // 2010-09-01 18:00:02
You can even use CreateFromFormat as RC said, this is using CreateFromFormat in Procedural style.
$date = date_create_from_format("YmdHis", '20100901180002');
echo date_format($date, 'Y-m-d H:i:s'); // 2010-09-01 18:00:02
Refer to http://php.net/manual/en/datetime.createfromformat.php
This part is strange, the elseif is never reached in my opinion.
if (isset($row[$column])){
echo '<td>' . $row[$column] . '</td>';
} elseif ($column == 'Condition') {
echo '<td> Error </td>';
} else {
echo '<td> </td>';
}
Regarding your format issue:
// PHP > 5.2
$date_s = "" . $row['Start'];
$date = DateTime::createFromFormat("YmdHis", $date_s);
echo $date->format("Y-m-d H:i:s"); // 2010-09-01 18:00:02