Change selected class to first instead of last date - php

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

Related

Use PHP foreach loop to display data in many html div tags

I have the following data and would like to display it in different containers in html.
Name Price Difference Signal
CA.PA 15.85 3.5609257364073 MACD
AZN.ST 896 3.4881049471963 MACD
AMGN 258.57 1.6391533819031 SMA 50/200
The containers are winner_1. As of right now the first winner_1 display the last Name from the above table.
How can I get it to say CA.PA in the first winner_1, and AZN.ST in the second winner_1, and AMGN in the last winner_1.
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r){
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
}
?>
<div class="winner_1">
<?php echo $name; ?>
</div>
<div class="winner_1">
<?php echo $name +1; ?>
</div>
<div class="winner_1">
</div>
</div>
</div>
The page can be seen here:
https://signal-invest.com/markets-today/
One option is generated div tags using php:
<div class="overview">
<h1>Winners</h1>
<div class="winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'>";
echo "<a href='#'>{$name}</a>";
echo "</div>";
}
?>
</div>
</div>
You should see following logic and try doing this way. Hopefully your problem will be resolved.
<div class = "overview">
<h1>Winners</h1>
<div class = "winner">
<?php
foreach ($res_winners_weekly as $r) {
$name = $r["Name"];
$Price = $r['Price'];
$percent_diff = $r['Difference'];
$signal = $r['Signal'];
echo "<div class='winner_1'><a href='#'> $name </a></div>";
}
?>
</div>
</div>

How to make 2 columns in loop data

I do foreach loop to output my data so my data will be like this
Dog
Cat
Mouse
Bird
Egg
Eagle
Fish
using this code
<div class="col-md-6">
<?php foreach ($treefields as $key=>$item): ?>
<?php if($key==0);?>
<?php if($item['title'] == $estate_data_option_1057): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong> <?php echo $options_name_1057; ?>:</strong><?php echo $estate_data_option_1057;?></span>
</div>
<?php if (count($item['childs_4']) > 0) foreach ($item['childs_4'] as $child): ?>
<div class="additional-amenities">
<span class="available"><i class="fa fa-check-square"></i></span><strong><?php _che($child['description']); ?></strong><span><?php echo $child['title']; ?></span>
</div>
<?php endforeach; ?>
<?php else: ?>
<?php ?>
<?php endif;?>
<?php endforeach; ?> </div></div>
How can i make it 2 columns like this?
- Dog - Egg
- Cat - Eagle
- Mouse - Fish
- Bird
First count the number of items in your array, then figure out what half of that would be and ceil it, so it's not a float. Then use that value in a conditional to echo your markup to break it into multiple columns.
For example, here is the technique boiled down to put the array into two different ul elements.
<ul>
<?php
$animals = array('Dog','Cat','Mouse','Bird','Egg','Eagle','Fish');
$numAnimals = count($animals);
$maxAnimalsPerColumn = ceil($numAnimals/2);
for($i=0; $i < $numAnimals; $i++) {
echo "<li>".$animals[$i]."</li>";
if ($i+1 == $maxAnimalsPerColumn ) {
echo "</ul><ul>";
}
}
?>
</ul>
$numAnimals would be 7
$maxAnimalsPerColumn would be 4 ($numAnimals divided by 2, ceil'd)
When the loop value (plus 1 since it starts at zero) matches $maxAnimalsPerColumn, it will echo a closing ul tag and a new one to start the second.
The resulting markup would be roughly:
<ul>
<li>Dog</li>
<li>Cat</li>
<li>Mouse</li>
<li>Bird</li>
</ul>
<ul>
<li>Egg</li>
<li>Eagle</li>
<li>Fish</li>
</ul>
Use a counter. Example:
$nbrOfColumns = 2;
$rowsPerColumn = ceil(count($treefields)/$nbrOfColumns);
$counter = 1;
<div class="col-md-6">
foreach ($treefields as $key=>$item){
if($counter === $rowsPerColumn){
echo "</div><div class="col-md-6">";
$counter = 0;
}
(generate your HTML here)
$counter++;
}
</div>

How can set different arrays that deals with different range of values?

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

Place DIV's in a containing DIV based on a numeric value

Ive got the follow PHP:
<div class="slide-background">
<div class="slide">
<?php foreach (array_chunk($items->submenu, $linkCount) as $items): ?>
<?php if (12 / $cols == 1):?>
<div class="col-md-12">
<?php else: ?>
<div class="col-md-<?php echo 12 / $cols; ?>">
<?php endif; ?>
<ul>
<?php foreach($items as $submenu): ?>
<?php echo $submenu; ?>
<?php endforeach; ?>
</ul>
</div>
<?php endforeach; ?>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
basically it calculates how many links to display and how many columns, but i now need to place the links in <div class="slide"></div>, but based on the columns.. so basically i need to say if $cols = 2 place two div's in a div and close.. so its basically how many every $cols it should place so many div's in that div..
Its Confusing for me to even explain.. I think Ive explained it rather well above.. If not place say so and ill try again..
Any Help Greatly Appreciated..
UPDATE:
thanks to Hans ive now have the following:
<?php $linksPerColumn = ceil($linkCount / $cols); $linkCounter = 0;?>
<div class="slide-background">
<div class="slide">
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php foreach ($items->submenu as $link): ?>
<?php $linkCounter++;?>
<?php if($linkCounter % $linksPerColumn == 0):?>
</ul>
</div>
<div class="col-md-<?php echo 12 / $cols ?>">
<ul>
<?php endif; ?>
<?php echo $link; ?>
<?php endforeach; ?>
</ul>
</div>
</div>
<ul class="pager">
<li>prev</li>
<li>next</li>
</ul>
</div>
</div>
only problem is when there's only one column and i need 2 links and then for it to close the div and the ul and start new ones.. right now it does that except for everyone and not for every two links...
You could use modulus for this one. You should calculate how many items you need per column. And then create a close and open div for example:
<?
$linksPerColumn = 4;
if ($linkCount > 4){
$linksPerColumn = ceil ($linkCount / $amountOfColums);
}
$linkCounter = 0;
?>
<div class="slide-background">
<div class="slide">
<?
foreach ($links as $link)
{
$linkCounter++;
?>
// Do your HTML Here.
<?
if($linkCounter % $linksPerColumn = 0)
{
?>
</div>
<div class="slide">
<?
}
?>
</div>
</div>
// Rest of the HTML here.
I think this should do the trick for you.

Add custom rows in a foreach list

This is my foreach list:
foreach ($items as $key => $item):
if (++$i == 21) break;
$output.='<div class="row-fluid"><div class="span12 block">
<div class="pull-left">
'.$item->title.'
</div>
<div class="pull-right">
<p class="muted">'.date("m/d/Y", $item->date).'</p>
</div>
<div class="clearfix"></div>
</div></div>';
endforeach;
echo $output;
The result is a well orderd list of 21 items picked from an xml feed. What i'm trying to do is to add a custom row between (for example) line 10 and 11.
Can anyone suggest me a good way?
You can do that by adding a simple if statement. If you want every 10th line to have that you can use mod operator %.
foreach ($items as $key => $item):
if (++$i == 21) break;
if ($i == 9) {
$output .= '<div>NEW LINE </div>';
}
$output.='<div class="row-fluid"><div class="span12 block">
<div class="pull-left">
'.$item->title.'
</div>
<div class="pull-right">
<p class="muted">'.date("m/d/Y", $item->date).'</p>
</div>
<div class="clearfix"></div>
</div></div>';
endforeach;
echo $output;
You can use flag variables.
Before loop set initial value of a variable $rowcount=1
and between loop you can increment this variable from +1.
and check
<code>
if($rowcount=10)
{
//do something
}
</code>
Use this solution

Categories