rob.m rob.m - 12 days ago 5
PHP Question

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

Answer

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

Comments