I am trying to get shuffle and only 19 items in a for each
loop. I used shuffle()
and if (++$i == 19) {break;}
, but the problem is one time it returns 12 items, one time 13, and another time 14. What did I do wrong?
Here is the code to check:
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
Increase $i only when a result has been printed. Check the value before starting a new foreach loop.
<?php
$i = 0;
shuffle($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
$i++;
}
if ($i == 19) {
break;
}
}
?>
Please try the following.
<?php
$counter = 0;
shuffle($children);
foreach ($children as $child)
{
if ($counter < 18)
{
break;
}
if($child['name_total'] > 0)
{
$counter++;
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : "");?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
}
?>
A simplified version of your code
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
}
if (++$i == 19) {
break;
}
}
So $i
is incremented every time, even if nothing is displayed. If $child['name_total']
is 0, nothing will be displayed - but $i
is still incremented.
If you want 19 items displayed, you need to only increment $i
when something is actually displayed:
foreach ($children as $child) {
if ($child['name_total'] > 0) {
// display stuff
if (++$i == 19) {
break;
}
}
}
I think you want to shuffle array,
Use this function,
function shuffle_assoc(&$array) {
$keys = array_keys($array);
shuffle($keys);
foreach ($keys as $key) {
$new[$key] = $array[$key];
}
$array = $new;
return true;
}
$i = 0;
shuffle_assoc($children);
foreach ($children as $child) {
if ($child['name_total'] > 0) {
?>
<li>
<?php echo ($child['filter_id'] == 2 ? "<span class='Verified'><i class='fa fa-check-circle'></i></span>" : ""); ?>
<div class="CatImg"><a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><img alt="<?php echo $child['name']; ?>" src="<?php echo $child['thumb']; ?>"/></a></div>
<a title="<?php echo $child['name']; ?>" href="<?php echo $child['href']; ?>"><?php echo $child['name']; ?><span class="Total"><?php echo $child['name_total']; ?></span></a>
</li>
<?php
}
if (++$i == 19) {
break;
}
}
?>
I hope this will help you solve your problem.