doupa2871
doupa2871
2014-04-26 04:11

玩Modulus Division

已采纳

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.

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

2条回答

  • dtt27783 dtt27783 7年前

    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; 
    }
    
    点赞 评论 复制链接分享
  • dongxi0605 dongxi0605 7年前

    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++;
        }
    }
    
    点赞 评论 复制链接分享