frshjb373 Posted November 27, 2013 Share Posted November 27, 2013 I'm trying to use php to echo only the first 10 items of an array. I have the entire list displaying if there are more than 10 items, but I can't figure out how to display only 10. Here's the code. Any help is much appreciated. Thank you in advance! <?php if (count($all_machines) > 10) { echo '<ul>'; foreach($all_machines as $machine) { echo '<li>' . $machine['name'] . '</li>'; } echo '</ul>'; } else { echo "No machines"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/ Share on other sites More sharing options...
Lone_Ranger Posted November 27, 2013 Share Posted November 27, 2013 <?php if (count($all_machines) = 10) { echo '<ul>'; foreach($all_machines as $machine) { echo '<li>' . $machine['name'] . '</li>'; } echo '</ul>'; } else { echo "No machines"; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460271 Share on other sites More sharing options...
aysiu Posted November 27, 2013 Share Posted November 27, 2013 Your code is saying to fetch every result if there are more than ten. Here's another way to approach it. <?php // Count up how many are in the array $all_machines_count=count($all_machines); // Initialize a counter $i=0; // Either way, start the list echo '<ul>'; // If there are more than ten results, stop at 10 if ($all_machines_count) > 10) { while($i<10){ echo '<li>' . $machine[$i]['name'] . '</li>'; ++$i; } // If there are 10 or fewer results, stop at the last record } else { while($i<$all_machines_count){ echo '<li>' . $machine[$i]['name'] . '</li>'; ++$i; } } // Either way, end the list echo '</ul>'; ?> Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460273 Share on other sites More sharing options...
Barand Posted November 27, 2013 Share Posted November 27, 2013 $array = range(1,50); // create array with 50 items echo '<ul>'; foreach (array_slice($array,0,10) as $item) { // get first 10 items echo "<li>$item</li>"; } echo '<ul>'; Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460284 Share on other sites More sharing options...
xProteuSx Posted November 27, 2013 Share Posted November 27, 2013 How about a "for" loop? for ($i = 0; $i <= 10; $i++) {echo "<li>" . $array[$i] . "</li>";} I don't recall, but that might display the first 11 items. If that is so, simply change "$i <= 10" to "$i < 10". Cheers. Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460308 Share on other sites More sharing options...
Barand Posted November 27, 2013 Share Posted November 27, 2013 (edited) xProteuSx, If there are less than 10 items then you'll have "undefined index" problems. This would cure it echo '<ul>'; $k = min(10, count($array)); for ($i = 0; $i < $k; $i++) { echo "<li>" . $array[$i] . "</li>"; } echo '</ul>'; Edited November 27, 2013 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460310 Share on other sites More sharing options...
xProteuSx Posted November 27, 2013 Share Posted November 27, 2013 Ahhh ... thx. Quote Link to comment https://forums.phpfreaks.com/topic/284309-only-display-first-10-items-from-an-array/#findComment-1460311 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.