Pregunta

I'm trying out the modulus division with a foreach loop, and I'm having a bit trouble understanding it.

$counter = 0;
foreach($result as $row){
    if(isset($row['username'])){
        if (($counter) % 2 == 0){
            echo "<tr class=\"r1\"><td class=\"center\"><a href=\"profile.php?username=" . $row['username'] . "\">" . $row['username'] . "</a></td></tr>";
        }
        else{
            echo "<tr class=\"r0\"><td class=\"center\"><a href=\"profile.php?username=" . $row['username'] . "\">" . $row['username'] . "</a></td></tr>";
        }
        $counter++;
    }
}

I want to output:

<tr class="r0">
    <td><a href="profile.php?username=Bob">Bob</a></td>
    <td><a href="profile.php?username=Daniel">Daniel</a></td>
</tr>
<tr class="r1">
    <td><a href="profile.php?username=Dylan">Dylan</a></td>
    <td><a href="profile.php?username=Bruce">Bruce</a></td>
</tr>

But currently, with my loop, I'm outputting:

<tr class="r1">
    <td<a href="profile.php?username=Bob">Bob</a></td>
</tr>
<tr class="r0">
    <td><a href="profile.php?username=Daniel">Daniel</a></td>
</tr>
<tr class="r1">
    <td><a href="profile.php?username=Dylan">Dylan</a></td>
</tr>
<tr class="r0">
    <td><a href="profile.php?username=Bruce">Bruce</a></td>
</tr>

Can someone explain to me exactly how modulus division works? Thank you.

¿Fue útil?

Solución 2

I got this to work. My problem was that my array was multidimensional, so I converted it to a single array. Afterwards, I used array_chunks.

$chunks = array_chunk($l, 2);
$i=0;
foreach($chunks as $mychunk){
if($i%2== 0){
    echo "<tr class=\"r0\">";
} else { echo "<tr class=\"r1\">"; }
$i++;
    foreach($mychunk as $newchunk) 
    {
    echo "<td class=\"center\"><a href=\"profile.php?username=" . $newchunk . "\">" . $newchunk . "</a></td>";
    }

    echo "</tr>";
}

For anyone looking to convert multidimensional arrays into single dimensional array:

function array_flatten($array) { 
  if (!is_array($array)) { 
    return FALSE; 
  } 
  $result = array(); 
  foreach ($array as $key => $value) { 
    if (is_array($value)) { 
      $result = array_merge($result, array_flatten($value)); 
    } 
    else { 
      $result[$key] = $value; 
    } 
  } 
  return $result; 
}

Otros consejos

Here, you want to display two records in one row. Actually modulus division will return the remainder of the division. Try with:

$counter = 0;
$i=1;
foreach($result as $row){
    if(isset($row['username'])){
        if (($counter) % 2 == 0){  // that is the counter value/2 doesnot returns a remainder ie, divisible by 2, then create another row
         if($i%2==0)
         {
           echo "<tr class=\"r1\">";  
         }
         else
         {
            echo "<tr class=\"r0\">";  
         }
        }
        else{
            echo "<td class=\"center\"><a href=\"profile.php?username=" . $row['username'] . "\">" . $row['username'] . "</a></td>";
        }
        if (($counter) % 2 == 0){     // close the tr if the counter doesnot return remainder
            echo "</tr>";
        }
        $counter++;
        $i++;
    }
}
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top