I am using SimplePie to display the RSS results from multiple sources, however I'm finding that there's several I don't need to see. How do I only show the results that are 5 days old or younger?
Here's my current PHP code:
foreach ($feed->get_items(0) as $item):
$url = $item->get_permalink();
$title = $item->get_title();
$date = $item->get_date('F j, Y - g:i a');
$description = $item->get_description();
if (strpos($url,'craigslist') !== false) {
echo ' <div id="linkCell" style="width: 100%;">';
echo ' <div id="vAlign">';
echo ' <p class="linkTitle">';
echo ' '.$title.'';
echo ' </p><br />';
echo ' <span class="date">'.$date.'</span><br>';
echo ' <p class="description">'.$description.'</p>';
echo ' </div>';
echo ' </div>';
}
endforeach;
Thanks in advance!
Something like this should work. Need to convert date to unix timestamps, find the difference, and then convert this to days.
foreach ($feed->get_items(0) as $item):
$url = $item->get_permalink();
$title = $item->get_title();
$date = $item->get_date('F j, Y - g:i a');
$datestrtotime = strtotime($date);
$now = time();
$datediff = $now - datestrtotime;
$daysdifference = floor($datediff/(60*60*24));
$description = $item->get_description();
if($daysdifference>5)
{
if (strpos($url,'craigslist') !== false) {
echo ' <div id="linkCell" style="width: 100%;">';
echo ' <div id="vAlign">';
echo ' <p class="linkTitle">';
echo ' '.$title.'';
echo ' </p><br />';
echo ' <span class="date">'.$date.'</span><br>';
echo ' <p class="description">'.$description.'</p>';
echo ' </div>';
echo ' </div>';
}
}
endforeach;
Related
i have an issues in my code below.
In days and time i want to add a loop, it s because i want to be able to get multiple different operating hours row. I ll add a repeater in my custom field.
And for now it s allow me to get only one row.
<ul>
<?php foreach($locations as $location) {
$title = get_the_title($location);
$address = '';
$address .= get_field('address', $location) . '<br>';
$address .= get_field('city', $location) . ', ';
$address .= get_field('state', $location) . ', ';
$address .= get_field('zip_code', $location);
$hours = get_field('days_open', $location);
$phone = get_field('contact_number', $location);
$email = get_field('contact_email', $location);
?>
<li>
<h3><?php echo $title; ?></h3>
<address><?php echo $address; ?></address>
<?php if($phone) { ?>
<?php echo $phone; ?>
<?php }; ?>
<?php if($email) { ?>
<?php echo $email; ?>
<?php }; ?>
<?php
if($hours && $hours[0]['days']
) {
echo $hours[0]['days'] . ': ';
}
?>
<?php
if($hours && $hours[0]['times']
) {
echo $hours[0]['times'];
}
?>
</li>
<?php
}
?>
</ul>
So i think my solution it s to add a foreach loop, can you help for that?
Thank you in advance cheers!
May be this is what you are looking for?
if(is_array($hours[0]['days'])){
foreach($hours[0]['days'] as $day)
{
echo $day . ': ';
}
}
if(is_array($hours[0]['times'])){
foreach($hours[0]['times'] as $time)
{
echo $time . ': ';
}
}
So I found the solutions it was :
<?php
if(is_array($hours)){
foreach($hours as $day)
{
echo $day['days'] . ': ';
echo $day['times'];
echo '<br />';
}
} ?>
I am having a difficult time figuring out how to break a section out of a an else-statement that is nested within a foreach loop.
The section I want to break out of the foreach loop is :
echo '
<div class="moreEventsContainer">
<div id="moreEventsWrap" class="total-center">
<span class="moreEventsLink">SEE ALL EVENTS</span>
<div class="rightArrow"></div>
</div>
</div>'
;
The thing is that it is part of the else statement, but leaving it in the loop just makes it duplicate itself over and over when I just want it to appear once.
Does anyone see how I can do this?
I tried doing the alternative endforeach nested before the end of the else statement, but it just broke the code.
Any ideas?
foreach ($event_rows as $event_row) {
$event_name = $event_row['event_name'];
$display_date = $event_row['display_date'];
$event_description = $event_row['small_desc'];
$end_date = new DateTime($event_row['end_date']);
$date = new DateTime('now');
if ($date >= $end_date) {
//$noEvents = 'No events are scheduled yet.';
$noEvents = '
<div id="noEvents">
</div>
';
} else {
echo '<div class="eventBlock">';
echo '<div class="total-center eventBlockWrap">';
echo '<span class="displayDate">'. $display_date .'</span>';
echo '<span class="eventName">'. $event_name .'</span>';
echo '<p class="dGsmall margNone">'. $event_description .'</p>';
echo '</div>';
echo '</div>';
echo '<div class="moreEventsContainer">
<div id="moreEventsWrap" class="total-center">
<span class="moreEventsLink">SEE ALL EVENTS</span>
<div class="rightArrow"></div>
</div>
</div>'
;
}
}
if ($noEvents != NULL) {
echo $noEvents;
} else {
}
Update:
foreach ($event_rows as $event_row) {
$event_name = $event_row['event_name'];
$display_date = $event_row['display_date'];
$event_description = $event_row['small_desc'];
$end_date = new DateTime($event_row['end_date']);
$date = new DateTime('now');
if ($date >= $end_date) {
//$noEvents = 'No events are scheduled yet.';
$noEvents = '
<div id="noEvents">
</div>
';
} else {
echo '<div class="eventBlock">';
echo '<div class="total-center eventBlockWrap">';
echo '<span class="displayDate">'. $display_date .'</span>';
echo '<span class="eventName">'. $event_name .'</span>';
echo '<p class="dGsmall margNone">'. $event_description .'</p>';
echo '</div>';
echo '</div>';
break;
echo '<div class="moreEventsContainer">
<div id="moreEventsWrap" class="total-center">
<span class="moreEventsLink">SEE ALL EVENTS</span>
<div class="rightArrow"></div>
</div>
</div>'
;
}
}
According to your update,
I suppose you want this code
echo '<div class="moreEventsContainer">
<div id="moreEventsWrap" class="total-center">
<span class="moreEventsLink">SEE ALL EVENTS</span>
<div class="rightArrow"></div>
</div>
</div>';
to execute once, after all of your records are shown.
So, I suggest using this instead of the break statement
if($i==sizeof($event_rows)){
echo '<div class="moreEventsContainer">
<div id="moreEventsWrap" class="total-center">
<span class="moreEventsLink">SEE ALL EVENTS</span>
<div class="rightArrow"></div>
</div>
</div>';
}
Where $i is initialized to 1 and incremented each time the loop executes.
Hi i have a code that displays title, date and location. I want it to put on a div, unfortunately there is some wrong in my code and it loops my div.
I just want all may data to be inside "may-container" i just don't know how to create this container.
Hope you could help me with this. Thanks
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
Here is my code
$counter = 0;
while ( $startdate <= $enddate) {
if ( date("F",strtotime($result['date'])) == "May" && date("m, Y",strtotime($result['date'])) >= date("m, Y") )
{
if ($counter < 1)
{
echo "<div class='May-container'>";
$counter++;
}
echo "<div class= 'May'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
}
$startdate = strtotime("+120 day", $startdate);
}
if your code date("F",strtotime($result['date'])) produces a month in words then why do you have to put if and else?
why not:
while ( $startdate <= $enddate) {
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
EDIT: to answer your comment, you can try this code:
This code is only applicable if your Data is sorted out by date
$last_month = '';
$is_first = true;
while ( $startdate <= $enddate) {
if($last_month != date('F',strtotime($result['date']))){
echo '</div>';
$is_first = true;
}
if($is_first){
$last_month = date('F',strtotime($result['date']));
echo "<div class='". strtolower($last_month) . "-container'>";
$is_first = false;
}
echo "<div class='" . date('F',strtotime($result['date'])) . "'>";
echo $result['title'],"<br>";
echo date ('F \ j,\ Y',strtotime($result['date']) ), "<br>";
echo $result['location'];
echo "</div>";
$startdate = strtotime("+120 day", $startdate);
}
if the code above code runs as what i expected(sorry i didnt run it for i dont have enough time to) it will yields something like:
<div class="may-container">
<div class="May">
title
date
location
</div>
<div class="May">
title
date
location
</div>
</div>
<div class="june-container">
<div class="June">
title
date
location
</div>
<div class="June">
title
date
location
</div>
</div>
I have a Joomla 3.1 site that is going to show reports about contributions made. The reports should be divided into groups by month and year. I have a code that pulls data from database but cannot make it put each month into a separate <div> and each year into a parent <div> to its months. How is it done? Here is my code.
<?php
$db = JFactory::getDbo();
$query = $db->getQuery(true);
$query->select(array($db->quoteName('contribution_date'), $db->quoteName('contribution_amount'), $db->quoteName('source')));
$query->from($db->quoteName('contribution'));
$db->setQuery($query);
$results = $db->loadObjectList();
?>
<table class='financial-reports'><th>aaa</th><th>bbb</th><th>ccc</th>
<?php
foreach ( $results as $result) {
echo '<tr>' . '<td class="fin-rep-date">' . JFactory::getDate($result->contribution_date)->format('d M, Y') . '</td>' .
'<td class="fin-rep-sum">' . $result->contribution_amount . ' ' .'руб.' . '</td>' .
'<td class="fin-rep-source">' . $result->source . '</td>' . '</tr>';
}
?>
</table>
I think the easies way would be convert results into multi-dimensional array like this:
foreach ($results as $result) {
$resultsArray[JFactory::getDate($result->contribution_date)->format('Y')][JFactory::getDate($result->contribution_date)->format('M')][] = $result;
}
And then you can use it like that:
<?php foreach ($resultsArray as $year) : ?>
<div>
<?php foreach ($year as $month) : ?>
<div>
<?php foreach ($month as $item) : ?>
<div>YOUR OUTPUT <?php echo $item->contribution_date; ?></div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
</div>
<?php endforeach; ?>
In the code below, I'm pulling all upcoming training classes from my database. I'm checking to see if the endDate has passed and I'm also checking if the status !='2'
I want this to return the 4 most recent results. It works fine until statusdoes =2. I understand that the loop is technically running 4 times, but only displaying the results with status !='2'
How can I change this so that if status = '2' the loop will continue until it finds 4 results that meet the criteria?
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php }
$count++;
}
?>
You must put the $count++ inside the if loop, otherwise it will always get incremented.
As in:
<?php
$today = date("Y-m-d");
$count = 0;
$sth = $dbh->query('SELECT * from training ORDER BY startDate ASC');
$sth->setFetchMode(PDO::FETCH_ASSOC);
while($count <= 4 && $row = $sth->fetch()) {
if($row['endDate'] > $today && $row['status'] != '2') {?>
<li>
<img class="post_thumb" src="/images/img.jpg" alt="" />
<div class="post_description">
<small class="details">
<?php echo date("m/d/Y", strtotime($row['startDate'])) . ' - ' . date("m/d/Y", strtotime($row['endDate'])) ?>
</small>
<a class="post_caption" href="/register.php?course_id=<?php echo $row['courseId'] . '&id=' . $row['id'] ?>"><?php echo $row['title'] ?></a>
</div>
</li>
<?php
$count++;
}
}
?>
You can break out out of the loop, if its four
while($row = $sth->fetch()) {
....
if($row['status']=='2' && $count >="4") break;
$count++;
}