Jump to content

Array Count


bschultz

Recommended Posts

I need to figure out a way to count the number of items in an array (which is easy)...and then echo the results in an ordered fashion.  I only want 8 results to show up per line.

 

I'm sure it's some form of for loop, but I don't know where to start.

 

This works...but it can't be the best way....plus what if I add more names to the array...???

 

<?php
$count = count($umpires);

if ($count <= 
    echo "<a href = 'schedule_names.php?name=$umpires'>$umpires</a>          ";

elseif ($count >= 9)
echo "<a href = 'schedule_names.php?name=$umpires[0]'>$umpires[0]</a>
          
<a href = 'schedule_names.php?name=$umpires[1]'>$umpires[1]</a>
          
<a href = 'schedule_names.php?name=$umpires[2]'>$umpires[2]</a>
          
<a href = 'schedule_names.php?name=$umpires[3]'>$umpires[3]</a>
          
<a href = 'schedule_names.php?name=$umpires[4]'>$umpires[4]</a>
          
<a href = 'schedule_names.php?name=$umpires[5]'>$umpires[5]</a>
          
<a href = 'schedule_names.php?name=$umpires[6]'>$umpires[6]</a>
          
<a href = 'schedule_names.php?name=$umpires[7]'>$umpires[7]</a>
          
<a href = 'schedule_names.php?name=$umpires[8]'>$umpires[8]</a>
<br /><br />
<a href = 'schedule_names.php?name=$umpires[9]'>$umpires[9]</a>
          
<a href = 'schedule_names.php?name=$umpires[10]'>$umpires[10]</a>
          
<a href = 'schedule_names.php?name=$umpires[11]'>$umpires[11]</a>
          
<a href = 'schedule_names.php?name=$umpires[12]'>$umpires[12]</a>
          
<a href = 'schedule_names.php?name=$umpires[13]'>$umpires[13]</a>
          
<a href = 'schedule_names.php?name=$umpires[14]'>$umpires[14]</a>
          
<a href = 'schedule_names.php?name=$umpires[15]'>$umpires[15]</a>
          
<a href = 'schedule_names.php?name=$umpires[16]'>$umpires[16]</a>
<br /><br />
<a href = 'schedule_names.php?name=$umpires[17]'>$umpires[17]</a>
          
<a href = 'schedule_names.php?name=$umpires[18]'>$umpires[18]</a>
          
<a href = 'schedule_names.php?name=$umpires[19]'>$umpires[19]</a>
";


?>

Link to comment
https://forums.phpfreaks.com/topic/201414-array-count/
Share on other sites

This would work for that. I didn't read your original question.

$num = 1;
foreach($umpires as $data) {
    echo '<a href="schedule_names.php?name=' . $data . '">' . $data . '</a>
                  ';
    $num++;
   
    if($num ==  {
        echo '<br />';
        $num = 1;
    }
}

Link to comment
https://forums.phpfreaks.com/topic/201414-array-count/#findComment-1056750
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.