CyberJunkie
CyberJunkie

Reputation: 22674

Count array items and add div after certain number

$specs = array ('Name' => 'Cleopatra', 'Year' => '2008', 'Length' => '20ft', 'Make' => 'manufacturer', 'Model' => 'model', 'Engines Count' => '2', 'Fuel' => 'Diesel', 'Rudder' => 'rudder', 'Keel' => 'keel', 'Price' => '$1'); 

foreach ($specs as $label => $detail) {
  echo "<tr>";  
  echo "<th>{$label}</th>";
  echo "<td>{$detail}</td>";
  echo "</tr>";
}

The foreach loop returns 1 column in each row. How can I render 4 columns per row like so

   <tr>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
    </tr>
    <tr>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
      <th>Label</th>
      <td>Detail</td>
    </tr>

Upvotes: 0

Views: 1275

Answers (4)

coffe4u
coffe4u

Reputation: 538

This carries the assumption that your index is zero based.

First we define the number of columns we want in each row. Then, we define two rules sets: when to start a new row and when to end a row.

We want to start a new row on the first iteration and anytime the current iteration count is divisible by the number of columns we want in each row.

We want to end a row when we are on the last iteration. Also we will end a row anytime that we are not on the first iteration and the total count of the collection minus the current iteration count is divisible by the number of columns we want in each row minus one.

$cols_in_row = 5;
foreach ($array as $i => $item)
{
    if ($i == 0 || $i % $cols_in_row == 0)
    {
        echo '<tr>';
    }

    echo '<td>'.$item.'</td>';

    if ($i + 1 == count($array) || ($i != 0 && count($array) - $i % ($cols_in_row - 1)))
    {
        echo '</tr>';
    }
}

This method allows you to only write the opening and closing tags once so that editors don't think that you are forgetting to open or close something.

Upvotes: 1

Toby Allen
Toby Allen

Reputation: 11213

If you remember your maths from school, you can use the mod operator to get the remainder of a division operation. This is what you need to get what you want.

 echo "<tr>"; 
foreach ($specs as $label => $detail) 
{
  $counter++;
  //get remainder of division by 4, when 1 create new row
 if ($counter % 4 == 1)
 {
  echo "</tr>"; 
  echo "<tr>"; 
 }
 echo "<th>{$label}</th>";
 echo "<td>{$detail}</td>";


}   
echo "</tr>"; 

Upvotes: 1

SlavaNov
SlavaNov

Reputation: 2485

Just add counter, something like this:

echo "<tr>";
foreach ($specs as $label => $detail) {
  if($i%4 == 0 && $i != 0) { 
    echo "</tr>";
    echo "<tr>";
  }
  echo "<th>{$label}</th>";
  echo "<td>{$detail}</td>";
  $i++;
}
echo "</tr>";

Update: Fixed edge case $i=0 and <tr>'s in right order

Upvotes: 5

gen_Eric
gen_Eric

Reputation: 227220

Set a counter, and every 4th iteration, print a new <tr>.

$i = 0;
echo '<tr>';
foreach ($specs as $label => $detail) {
  if($i !== 0 && $i%4 === 0){
     echo '</tr><tr>';
  }
  echo "<th>{$label}</th>";
  echo "<td>{$detail}</td>";
  $i++;
}
echo '</tr>';

Upvotes: 1

Related Questions