Hello I currently have this PHP Script
<?php if($purchase_dates != FALSE){?>
<?php foreach($purchase_dates as $id=>$outer_value) { ?>
<!-- Output the date here -->
<!-- Start Purchase table -->
<?
$date = strtotime($outer_value['date_purchased']);
$data = date('F jS Y', $date);
?>
<h3></h3>
<!-- Start inner loop -->
<?php foreach($purchases as $id=>$inner_value) { ?>
<?php if($inner_value['date_purchased'] == $outer_value['date_purchased']) { ?>
<div class="row">
<ul>
<li class="id"><?=$inner_value['code'];?></li>
<li class="name"><?=$inner_value['firstname'];?> <?=$inner_value['surname'];?></li>
<li class="download">Download PDF</li>
<div class="disp">
<li class="location"><?=$inner_value['city'];?></li>
<li class="status"><?=$job_status[$inner_value['job_status']];?></li>
<li class="education"><?=$education_types[$inner_value['education_level']]['nice_name'];?></li>
<li class="role">
<?php
$out = array();
if($inner_value['is_contract'] == 'Y') $out[]="Contract";
if($inner_value['is_permanent'] == 'Y') $out[]="Permanent";
if($inner_value['is_temporary'] == 'Y') $out[]="Temporary";
echo implode(", ",$out);
?>
</li>
<li class="salary"><?=$salaries[$inner_value['expected_salary_level']]['nice_name'];?></li>
</div>
</ul>
</div>
<?php } ?>
<?php } ?>
<!-- End inner loop -->
<?php } ?>
<?php }?>
<!-- End Outer Tree Loop -->
This generates something that looks like this,
22nd July
Purchase1
22nd July
Purchase 2
18th July
Purchase 3
18th July
Purchase 4
My question is, I want to group all the purchases that are on the same under that date and also if that date is todays date I want to replace '23rd July' with 'Todays Purchases'
I am already using groupby in my SQL
The easiest way to group by a date is to add an extra dimension to your arrays, so all entries that belong to the same date are in the same subarray. Then you just need to loop twice:
foreach($data as $purchase_date => $purchases){
// Print date
foreach($purchases as $id=>$inner_value){
// Print purchase
}
}
You appear to be using Unix timestamps for dates, so you can simply use date() to strip hours, minutes and seconds:
if( date('Y-m-d')==date('Y-m-d', $purcharse_date) ){
// Today
}
Related
I need to create an accordion list of news article titles grouped by year. So far the contents of the accordion panels are correct with each panel showing all of the news posts for that year.
The problem is that I'm getting multiple panels for years based on the number of articles for the year. So for example year 2003 has three news articles, so I'm getting three accordion panels for 2003, each with the three corresponding news articles for that year. For 2004 I get one accordion panel because there's only one article for 2004. 2006 has six articles so I get six panels each with the six articles inside.
What do I need to do to get only one panel with the correct number of titles inside?
Here's my SQL:
$newsposts = $wpdb->get_results("
SELECT YEAR(wp_posts.post_date) AS post_year, wp_posts.post_title, wp_posts.ID AS post_id
FROM wp_posts LEFT JOIN wp_term_relationships ON
(wp_posts.ID = wp_term_relationships.object_id)
WHERE wp_term_relationships.term_taxonomy_id = 3
AND wp_posts.post_status = 'publish'
ORDER BY post_date
", OBJECT );
Here's my markup:
<?php foreach($newsposts as $year) : ?> <?php //print_r($year); ?>
<li class="accordion-item" data-accordion-item>
<?php echo $year->post_year; ?>
<div class="accordion-content" data-tab-content>
<?php foreach($newsposts as $post) : ?> <?php //print_r($post); ?>
<?php // Continue unless years match
if ( $post->post_year != $year->post_year ) continue; ?>
<?php echo $post->post_title; ?><br/>
<?php endforeach; //$posts ?>
</div>
</li>
<?php endforeach; //$year ?>
</ul>
Somehow I need to limit the first foreach to only one for each year represented. I would appreciate any help.
I worked through this and found an answer thanks to this post:remove duplicates in foreach
In my case here is my revised markup:
<?php $yearposts = array(); ?>
<?php foreach($newsposts as $year) : ?> <?php $postyear = $year->post_year; ?>
<?php if(!in_array($postyear, $yearposts)) { ?>
<li class="accordion-item" data-accordion-item>
<?php echo $postyear; array_push($yearposts,$postyear); ?>
<div class="accordion-content" data-tab-content>
<ul class="accordlist">
<?php foreach($newsposts as $post) : ?>
<?php // Continue unless years match
if ( $post->post_year != $year->post_year ) continue; ?>
<li><a class="newstitle" href="<?php the_permalink($post->post_id); ?>"><?php echo $post->post_title; ?></a></li>
<?php endforeach; //$posts ?>
</ul>
</div>
</li>
<?php } // end in_array conditional ?>
<?php endforeach; //$year ?>
</ul>
As the indicated post says,
Basically we are just using an additional array to store the printed values. Each time we see a new value, we print it and add it to the array.
My version is broken into pieces to surround the elements that are required for the accordion formatting.
Anyway, I hope this helps someone else. I needed answers from different questions to find my solution. Now maybe someone will find everything in this one place.
Cheers
I have a page that show my orders in day, but now i want to count and sum the values from 7 / 15 and 30 days ago (if possible in list), or just count/sum in this step: <strong>Soon</strong>
$pedido->valor_pedido is my value from order
$pedido->id its id from order, i use to count.
My code:
<div class="row">
<?php
$totalPedido = 0;
$contagPedido = 0;
foreach ($pedidos as $pedido) {
$totalPedido += $pedido->valor_pedido; // sum day orders
$contagPedido += count($pedido->id); //count day orders
}
?>
<div class="span12">
<!-- Widgets -->
<ul class="widgets">
<!-- Basic widget item -->
<li class="span3">
<span class="widget-label"><span class="awe-star"></span> This Week</span>
<strong>Soon</strong>
</li>
<!-- /Basic widget item -->
<!-- Clickable widget item -->
<li class="span3">
<span class="widget-label"><span class="awe-star"></span> Totay</span>
<strong>R$ <?= ConverteReal($totalPedido) ?></strong>
</li>
<!-- /Clickable widget item -->
<!-- Widgets with graphs -->
<li class="span3">
<span class="widget-label"><span class="awe-star"></span> Orders this week</span>
<strong>Soon</strong>
</li>
<li class="span3">
<span class="widget-label"><span class="awe-star"></span> Orders today</span>
<strong><?= $contagPedido ?></strong>
</li>
<!-- /Widgets with graphs -->
</ul>
<!-- /Widgets -->
</div>
</div>
According to PHP Manual, count() will use to get the count of all elements in an array
If $pedido->id is a unique ID, you can just get the count of $pedidos array like
<?
$contagPedido = count($pedidos); // total no of orders
foreach ($pedidos as $pedido) {
$totalPedido += $pedido->valor_pedido; // sum day orders
}
?>
I want to achieve the following html dynamically:
Time period AD:
<ul>
<li>1200</li>
<li>1300
<ul>
<li>1301</li>
</ul>
<li>
</ul>
Time period BC:
<ul>
<li>-200</li>
<li>-450
<ul>
<li>-451</li>
</ul>
</li>
</ul>
In the following I have set an array so that I can, by using some custom fields, push my dates into the array, then tell the whole code to calculate in 100 of years, in order to be able to place years like 1301 as a nested ul under 1300.
<ul>
<?php
$yearsArray = [];
$centuryHash = [];
query_posts(array(
'post_type' => 'post',
'posts_per_page' => -1
));
while ( have_posts() ) : the_post();
array_push($yearsArray, get_field("year"));
if (($wp_query->current_post +1) == ($wp_query->post_count)) {
$yearsArray = array_unique($yearsArray);
sort($yearsArray);
}
endwhile;
foreach ($yearsArray as $year) {
$currentCentury = floor($year/100)*100;
if(!$centuryHash[$currentCentury]){
$centuryHash[$currentCentury] = [];
}
if($currentCentury != $year){
$centuryHash[$currentCentury][] = $year;
}
}
foreach ($centuryHash as $century => $centuryYears) { ?>
<li class="dropdown">
<?php if($centuryYears){ ?>
<a class="btn btn-default" href="#" class="dropdown-toggle" data-date="<?php echo $century; ?>" data-toggle="dropdown" role="button" aria-haspopup="true" aria-expanded="false">
<?php echo $century; ?>
<span class='caret'></span>
</a>
<ul class='dropdown-menu'>
<?php foreach ($centuryYears as $year) { ?>
<li>
<a class="btn btn-default" data-date="<?php echo $year; ?>" href="#">
<?php echo $year; ?>
</a>
</li>
<?php }
echo "</ul>";
} else { ?>
<a class="btn btn-default" data-date="<?php echo $century; ?>" href="#">
<?php echo $century; ?>
</a>
<?php } ?>
</li>
<?php }
?>
</ul>
The problem that I can't figure out is how I can say like (verbal code):
If value is within the range of -450 till 2 then go into this array otherwise go in that array instead
From my end I could set in the cms (people will be able to insert content with dates) some flags so that I can do simple conditionals like (verbal code):
"Is this date from a or b? then do this or that"
But that creates an issue because I could flag a period of time and place the wrong date in the wrong period.
Therefore the last solution that I thought is to set a range of periods of times and delimiter some arrays by create different ones where I could dynamically push the values depending on their values.
This could be a start maybe (example from docs)? But how can I set an if to control what goes there or not?
foreach (range(0, 12) as $number) {
echo $number;
}
Here is a simple answer to your question.
If value is within the range of -450 till 2 then go into this array otherwise go in that array instead
if ($value >= -450 && $value <= 2) {
// do this
} else {
// do that
}
Hope, this answers to your question (which is, unfortunately, overloaded with dozens of code that doesn't apply to the question).
I want my first date to be the selected one but as it is now the last one is the selected one, i've managed to get the years in the right order but the only problem im having now is getting the selected class on the first date instead of the ast one.
<div class="timeline">
<?php
$tl = unserialize($properties['timeline']);
$years = array();
foreach($tl as $t)
{
$e=explode("-", $t['date']);
$years[$e[2]][strtotime($t['date'])]=$t;
}
ksort($years);
?>
<div class="events-wrapper">
<div class="events">
<ol>
<?php
$i=1;
foreach($years as $y=>$t)
{
echo '<li><a href="#0" data-date="01/01/'.$y.'"'.($i==count($years)?' class="selected"':' class="older-event"').'>'.$y.'</a></li>';
$i++;
}
?>
</ol>
<span class="filling-line" aria-hidden="true"></span>
</div> <!-- .events -->
</div> <!-- .events-wrapper -->
Whats the best way to do this?
Just change your condition inside the foreach loop ($i==count($years) to ($i == 1).
foreach($years as $y=>$t){
echo '<li><a href="#0" data-date="01/01/'.$y.'"'.($i==1)?' class="selected"':' class="older-event"').'>'.$y.'</a></li>';
$i++;
}
I am using wordpress, a plugin called The Events Calendar (great plugin btw), and I'm trying to create a little widget with a small navigational element in it. The navigation would show the previous month, the current month, and the next month. For example, today the navigation would show "Apr May Jun".
What should happen is when you click "Jun" it should load the events that occur in June. I want to load these via jquery/ajax magic into a specified div.
I kinda have it working, but i have a bug and I'm not sure how to fix it. When i click one of the months that is displayed initial, it works. But then when i click another month, I'm losing my jquery function (probably due to the page not reloading completely.)
So, I'm wondering if there is a way to re-initialize the navigation so i can get more clicks.
On with the code:
My sidebar.php file contains the following code:
Some HTML
<div id="upcoming-events-box">
<div class="upcoming-events-block-inner">
<div class="upcoming-events-content">
<div id="EventLoader">
<?php get_template_part( 'content', 'the-events' ); ?>
</div>
</div>
</div>
</div>
And some Jquery:
<script type="text/javascript">
jQuery(function() {
jQuery('#upcoming-events-months li a').click(function() {
var toLoad = jQuery(this).attr('href')+' #EventLoader';
jQuery('#EventLoader').hide('fast',loadContent);
jQuery('#load').remove();
jQuery('.upcoming-events-content').append('<span id="load">LOADING...</span>');
jQuery('#load').fadeIn('normal');
function loadContent() {
jQuery('#EventLoader').load(toLoad,'',showNewContent);
}
function showNewContent() {
jQuery('#EventLoader').show('normal',hideLoader);
}
function hideLoader() {
jQuery('#load').fadeOut('normal');
}
return false;
});
});
</script>
content-the-events.php which is called into the sidebar contains the following code:
<div id="upcoming-events-months">
// Some php
global $post;
$current_page_URL = $_SERVER["SERVER_NAME"];
if (isset($_GET['setmonth'])) {
$current_month = $_GET['setmonth'];
} else {
$current_month = date('M'); // This gets the current month
}
$next_month = date('M', strtotime('+1 month', strtotime($current_month)));
$prev_month = date('M', strtotime('-1 month', strtotime($current_month)));
echo "<ul>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$prev_month."'
id='".$prev_month."'>".$prev_month."</a></li>";
echo "<li>".$current_month."</li>";
echo "<li><a href='http://".$current_page_URL."?setmonth=".$next_month."'
id='".$next_month."'>".$next_month."</a></li>";
echo "</ul>";
</div>
<div id="content"></div>
<div class="upcoming-event-scroller">
<?php
$all_events = tribe_get_events(array(
'eventDisplay'=>'all',
'posts_per_page'=>-1,
'start_date'=>'01 '.$current_month.' 2012',
'end_date'=>'31 '.$current_month.' 2012'
));
foreach($all_events as $post) {
setup_postdata($post);
?>
<div class="row <?php echo (++$j % 2 == 0) ? 'even' : 'odd'; ?>">
<p><?php the_title(); ?></p>
<p class="event-date"><?php echo tribe_get_start_date($post->ID, true, 'M j, Y'); ?> - <?php echo tribe_get_end_date($post->ID, true, 'M j, Y'); ?></p>
</div>
<?php } //endforeach ?>
<?php wp_reset_query(); ?>
</div>
Maybe I've just gotten myself really confused trying to figure this out. haha. Any help would be greatly appreciated.