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';
}
Related
I have created a page that asks the user for a date and outputs results from an api. The api requires a start date and end date that can be no more than 7days apart. I have set it up so a user can enter a date and the end date will automatically be set for 7 days later.
I am having issues using the date function, It appears now that the code will automatically use todays date before the user can input their choose.
I want the user to be able to choose there date whether it be todays or a future date, I want my api call to wait for users input but not sure how this can be done.
<?php
$startDate = date('Y-m-d', strtotime(isset($_GET['start'])? $_GET['start'] :date('Y-m-d')));
$endDate = date('Y-m-d', strtotime('+7 days', strtotime($startDate)));
if($startDate){
echo "$endDate";
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
echo "<h1>Near-Earth Object (NEO) Report between " . $params['start_date'] . " and " . $params['end_date'] . "</h1>";
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
}
?>
It is nearly at what you wanted to begin with. Just need to add an else to the if statement and update the start/end dates to return false when no date is entered. Note: I also moved the header above the if and added a tertiary condition to display the date if it is already entered so that it is always displayed.
<?php
$startDate = isset($_GET['start']) ? date('Y-m-d', strtotime($_GET['start'] )) : false;
$endDate = $startDate ? date('Y-m-d', strtotime('+7 days', strtotime($startDate))) : false;
$params = array(
'start_date' => $startDate,
'end_date' => $endDate,
'api_key' => 'coXJeNygdeuxVKs9yJLecWbfuXsY54Wi9gq37HuN'
);
echo '<h1>Near-Earth Object (NEO) Report',
( $startDate ? ' between ' . $params['start_date'] . ' and ' . $params['end_date'] . '</h1>' : '</h1>');
if($startDate) {
echo "$endDate";
$data = json_decode(callAPI('GET', 'https://api.nasa.gov/neo/rest/v1/feed', $params));
foreach ($data->near_earth_objects as $date => $count) {
echo "<p>" . sizeof($count) . " objects detected on $date</p>";
echo "<ol>";
foreach ($data->near_earth_objects->$date as $near_earth_object) {
echo "<li>" . $near_earth_object->name . " <a href='" . $near_earth_object->nasa_jpl_url . "'>" . $near_earth_object->nasa_jpl_url . "</a><br>";
echo "Estimated Diameter: " . $near_earth_object->estimated_diameter->meters->estimated_diameter_min . "-" . $near_earth_object->estimated_diameter->meters->estimated_diameter_max . " metres<br>";
echo "<ul>";
foreach ($near_earth_object->close_approach_data as $close_approach) {
echo "<li>Close approach on " . $close_approach->close_approach_date . " velocity " . $close_approach->relative_velocity->kilometers_per_hour . " km/h " . "missing " . $close_approach->orbiting_body . " by " . $close_approach->miss_distance->kilometers . " km</li> ";
}
echo "</ul></li>";
}
echo "</ol>";
}
} else {
?><form action="" method="GET">
<label for="startdate">Please enter a start date (end date will be 7 days after the start date):</label>
<input id="startdate" type="date" name="start" />
<input type="submit" />
</form><?php
}
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.
So i have a log/txt file wich contains the following :
31 dec. 2014 11:56 - UserA: Hi31 dec. 2014 11:56 - UserB: Hi to you31 dec. 2014 11:56 - UserA: Whats your name?31 dec. 2014 11:57 - UserB: Nancy
Im trying to get this into a more readable format.....
So im trying the following :
<?php
//load the logfile
$txt_file = file_get_contents('test.txt');
$txtfile2 = preg_split( "/ ( |:) /", $txt_file );
$rows = explode("\n", $txtfile2[0]);
foreach($rows as $row => $data)
{
$row_data = explode(':', $data);
$info[$row]['when'] = $row_data[0];
$info[$row]['name'] = $row_data[1];
$info[$row]['description'] = $row_data[2];
//display data
echo ' WHEN: ' . $info[$row]['when'] . '<br />';
echo ' WHO: ' . $info[$row]['name'] . '<br />';
echo ' MESSAGE: ' . $info[$row]['description'] . '<br /><br />';
}
?>
This works partially....
When executed the output would be :
WHEN: 31 dec. 2014 11
WHO: 56 - UserA
MESSAGE: Hi
WHEN: 31 dec. 2014 11
WHO: 56 - UserB
MESSAGE: Hi to you
This is close to what i want, but as you can see, because im using the delimiter ":" it will also cut the time and place the minutes in the who.
How can i explode on the second ":" only?
i hope this makes sense?
See how this works for you
<?php
$filename = "logfile.txt"; //data file to open
$fp = fopen( $filename, "r" ) or die("Couldn't open $filename");
while ( ! feof( $fp ) )
{
$line = fgets( $fp, 1024 );
$line_array = preg_split("/[:-]+/", $line);
$line_timestamp = $line_array[0] . ":" . $line_array[1];
$line_user = $line_array[2];
$line_message = $line_array[3];
echo ' WHEN: ' . $line_timestamp . '<br />';
echo ' WHO: ' . $line_user . '<br />';
echo ' MESSAGE: ' . $line_message . '<br /><br />';
}
?>
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);
}
<?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.