Getting Output of Array in Predetermined way - php

<?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.

Related

group archive in php and mysql

I want to create an archive list like this:
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
2015
November 2015
testing 1 of 15
but it is showing like this:
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
2016
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
2015
July 2016
testing 5 of 16
testing 4 of 16
testing 3 of 16
testing 2 of 16
February 2016
testing 1 of 16
November 2015
testing 1 of 15
This is my source code:
$query = "SELECT * FROM blogs ORDER BY date DESC";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet)){
$newsArray = array();
echo '<ul>' . PHP_EOL;
while ($newsResult = mysql_fetch_array($resultSet)){
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$timePeriodY = date('Y',strtotime($timePeriod));
$timePeriodM = date('F',strtotime($timePeriod));
/*if (!isset($newsArray[$timePeriod])){
$newsArray[$timePeriod] = array();
}*/
$newsArray[$timePeriod][] = $newsResult;
}
//by year
foreach ($newsArray as $timePeriod => $newsItems){
$timePeriodY = date('Y',strtotime($timePeriod));
echo '<li><strong>' . $timePeriodY . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//by month
foreach ($newsArray as $timePeriod => $newsItems){
echo '<li><strong>' . $timePeriod . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//news items
foreach ($newsItems as $item){
echo '<li>';
echo ''.$item["titlename"].'';
echo '</li>' . PHP_EOL;
}
//end by month
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
//end by year
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
echo '<li> </li>' . PHP_EOL;
echo '</ul>' . PHP_EOL;
} else {
echo 'No Blog Found';
}
Please help me with this and thanks in Advance.
You're "grouping" by "F Y" when you should be "grouping" by "Y" and then "F Y"
For example :
$newsArray[$timePeriod][] = $newsResult;
Should be something like
$newsArray[$timePeriodY][timePeriod][]= $newsResult;
Your code will then become:
$query = "SELECT * FROM blogs ORDER BY date DESC";
$resultSet = mysql_query($query);
if (mysql_num_rows($resultSet)){
$newsArray = array();
echo '<ul>' . PHP_EOL;
while ($newsResult = mysql_fetch_array($resultSet)){
$newDate = $newsResult['date'] ;
$timePeriod = date('F Y ',strtotime($newDate));
$timePeriodY = date('Y',strtotime($timePeriod));
$timePeriodM = date('F',strtotime($timePeriod));
$newsArray[$timePeriodY][$timePeriod][] = $newsResult;
}
//by year
foreach ($newsArray as $timePeriodY => $newsItems){
echo '<li><strong>' . $timePeriodY . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//by month
foreach ($newsItems as $timePeriod => $items){
echo '<li><strong>' . $timePeriod . '</strong>' . PHP_EOL;
echo '<ul>' . PHP_EOL;
//news items
foreach ($items as $item){
echo '<li>';
echo ''.$item["titlename"].'';
echo '</li>' . PHP_EOL;
}
//end by month
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
//end by year
echo '</ul>' . PHP_EOL;
echo '</li>' . PHP_EOL;
}
echo '<li> </li>' . PHP_EOL;
echo '</ul>' . PHP_EOL;
} else {
echo 'No Blog Found';
}

In PHP, how do you get the text offset of a timezone

How do you get the timezone offset text? For example if I pass in:
America/New_York
...I would like to receive back:
-04:00
I created a function called getTimezoneOffsetText
<?php
function getTimezoneOffsetText($timezone){
date_default_timezone_set( "UTC" );
$daylight_savings_offset_in_seconds = timezone_offset_get(timezone_open($timezone), new DateTime());
$mod = $daylight_savings_offset_in_seconds/60 % 60;
$min = abs($mod);
return sprintf('%+03d', $daylight_savings_offset_in_seconds/60/60) . ':' . sprintf('%02d', $min);
}
$offset1 = 'America/New_York';
$offset2 = 'Asia/Kabul';
$offset3 = 'Asia/Kathmandu';
$offset4 = 'Israel';
$offset5 = 'Greenwich';
$offset6 = 'America/Caracas';
echo '<pre>';
echo $offset1 . ":\t" . getTimezoneOffsetText($offset1) . '<br/>';
echo $offset2 . ":\t\t" . getTimezoneOffsetText($offset2) . '<br/>';
echo $offset3 . ":\t\t" . getTimezoneOffsetText($offset3) . '<br/>';
echo $offset4 . ":\t\t\t" . getTimezoneOffsetText($offset4) . '<br/>';
echo $offset5 . ":\t\t" . getTimezoneOffsetText($offset5) . '<br/>';
echo $offset6 . ":\t" . getTimezoneOffsetText($offset6) . '<br/>';
echo '</pre>';
It generates this data:
America/Caracas: -04:30
Asia/Kabul: +04:30
Asia/Kathmandu: +05:45
Israel: +03:00
Greenwich: +00:00
America/New_York: -04:00
Note that it gets half hour and other "non-standard" timezones such as Caracas, Kabul and Kathmandu. I'm curious though if there is already a function that exists that accomplishes this.

conditional formatting html table in php with time stamp comparison

echo '<table style="width:100%"> <tr>';
echo '<td>Order</td>';
echo '<td>Destination</td>';
echo '<td>Location</td>';
echo '<td>Status</td>';
echo '<td>TimeStamp</td>';
echo '</tr>';
if($result) {
while($row = mysqli_fetch_assoc($result)) {
echo '<tr><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
echo '</table>';
}
I want to change the background of the row turned a different color is the time stamp is more than 60 minutes past the current time. any help would be much appreciated. i dont even know where to begin.
Thanks
edit: format of my time stamp "2015-07-17 19:17:31"
Do an if to see if time is over 60 minutes and if so assign it a class with a different background color. Since you didn't clarify I'm going to assume you are using unix timestamp time().
$currTime = time();
if($result) {
while($row = mysqli_fetch_assoc($result)) {
$calc = $currTime - $row['TimeStamp'];
if($calc > 3600){
$rowClass = "oldOrder";
} else {
$rowClass = "normalOrder";
}
echo '<tr class="'.$rowClass.'"><td>';
echo $row['OrderNumber'] . '';
echo '</td><td>';
echo $row['Destination'] . '';
echo '</td><td>';
echo $row['Location'] . '';
echo '</td><td>';
echo $row['Status'] . '';
echo '</td><td>';
echo $row['TimeStamp'] . '';
echo '</td></tr>';
}
Then add CSS to define the two classes
.oldOrder{
background-color: #ccc;
}
.normalOrder{
background-color: #fff;
}
$NowDT = date("Y-m-d H:i:s");
$NowRDT = strtotime($NowDT);
$DateTimeNF = $row['TimeStamp'];
$TimeRF = strtotime($DateTimeNF);
$TimeDifference = $NowRDT - $TimeRF;
$TimeDifferenceHours = $TimeDifference/3600;
If ($TimeDifferenceHours > "1")
{
echo '<tr bgcolor="#E60000"><td>';
}
else
{
echo '<tr><td>';
}
It seems like your timestamp is a String, you need to convert your string to a Unix TimeStamp using strtotime:
$rowTime=strtotime($row['TimeStamp']);
and then substract it from the actual time, as the previous answer, to obtain the difference in seconnds. Divide it by 60 and you get it on minutes.
$currenTime=time();
$diffSeconds=$currenTime-$rowTime;
$diffMinutes=$diffSeconds/60;
and then check if the difference is bigger than 60:
$myCSS="normalRow";
if ($diffMinutes>60) { $myCSS="differentRow"; }
and then use that CSS Style ($myCSS) in your table row:
echo '<tr class="'.$myCSS.'"><td>';
You need to define those two styles in your CSS file or your HTML Header like:
<style>
.normalRow {
background-color: #888;
}
.differentRow {
background-color: #ccc;
}
</style>
And that's it. Hope it helps.

PHP mySQL - Multiple games/events on same day display

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

sql query fetch error [duplicate]

This question already has answers here:
Reference: What is variable scope, which variables are accessible from where and what are "undefined variable" errors?
(3 answers)
Closed 3 years ago.
I'v got some code but keep getting error's also I'm new to php so any help would be great :)
Here's my code
<?php
// Create connection
$con = mysqli_connect("host","database","pswd");
// Database Connection Check
if (mysqli_connect_errno($con)) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
break;
}
echo "<table>";
echo nextweek("heren1kalender","Heren 1e Provinciale");
echo "</table>";
function nextweek($table, $ploegNaam) {
// Get this weeks dates (monday and sunday and month)
$current_day = date("N")
$days_to_sunday = 7 - $current_day;
$days_from_monday = $current_day - 1;
$monday = date("d", strtotime("- {$days_from_monday} Days"));
$sunday = date("d", strtotime("+ {$days_to_sunday} Days"));
$month = date("m", strtotime("+ {$days_to_sunday} Days"));
// SQL query
$result = mysqli_query($con,"SELECT datum, hoofd, thuisploeg, bezoekers FROM " . $table . " WHERE thuisploeg LIKE '%Lochristi%' OR bezoekers LIKE '%Lochristi%'");
while($row = mysqli_fetch_array($result)) {
$string ="";
// Get day and month from array
$dag = substr($row['datum'], 4, -6);
$maand = substr($row['datum'], 7, -3);
if ($dag >= $monday AND $dag <= $sunday AND $maand == $month) {
if (strpos($row['thuisploeg'],'Lochristi') !== false) {
$string .= "<tr>";
if (substr($row['hoofd'], 0, 1) >= 3) {
$string .= '<td class="win">' . $row['hoofd'] . "</td>";
}
else {
$string .= '<td class="loss">' . $row['hoofd'] . "</td>";
}
$string .= '<td>' . $ploegNaam . '</td>';
$string .= "<td><strong>VS</strong></td>";
$string .= '<td>' . $row['bezoekers'] . '</td>';
}
elseif (strpos($row['bezoekers'],'Lochristi') !== false) {
$string .= "<tr>";
if (substr($row['hoofd'], 0, 1) >= 3) {
$string .= '<td class="loss">' . $row['hoofd'] . "</td>";
}
else {
$string .= '<td class="win">' . $row['hoofd'] . "</td>";
}
$string .= '<td>' . $row['thuisploeg'] . '</td>';
$string .= "<td><strong>VS</strong></td>";
$string .= '<td>' . $ploegNaam . '</td>';
}
$string .= "</tr>";
}
}
return $string;
}
?>
And these are the PHP error's I'm getting:
Warning: mysqli_query() expects parameter 1 to be mysqli, null given in /home/a2902119/public_html/test.php on line 24
Warning: mysqli_fetch_array() expects parameter 1 to be mysqli_result, null given in /home/a2902119/public_html/test.php on line 26
Thanks for the help!
After fixing the other issues: Variable scope. $con is not available in the functions. Pass it in as an arg or rework into classes or something.
Your SQL connection doesn't seem correct.
It should have 4 parts.
$con=mysqli_connect(hostaddress,dbuser,dbpass,databasename);

Categories