开发者

Merging Code with Display on Loop

开发者 https://www.devze.com 2023-01-25 09:06 出处:网络
Trying to create DIV containers out of a mysql results which each hold chunks of 10. $balloon_count = the amount of records each div should hold.

Trying to create DIV containers out of a mysql results which each hold chunks of 10.

$balloon_count = the amount of records each div should hold.

$ui = the loop counter.

Functionality is simple, don't want to a template engine.

Trying to use the MODULUS operator to simplify the div cuts.

Doesn't work. Any direction is greatly appreciated.


Sample Code

        $ui=1;
        $balloon_holds = 10;

        while($row = mysql_fetch_array($result))
        {            


            if($ui==1||$ui%$balloon_holds != 0)
                {                    
                echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
                echo "<td style=\"font-size:small;vertical-align:text-top;\">";
                }


                echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";


                if($ui==10||$ui%$balloon_holds != 0){             
                echo '</td></tr></table></div>';                  
                }



            $ui++; 

        }

Sample Expected "HTML" Output

<div><table style="width:400px;border:2px solid gray开发者_开发问答;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">         
Record1
Record2
Record3
Record4
Record5
Record6
Record7
Record8
Record9
Record10    
</td></tr></table></div>

<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>
<td style="font-size:small;vertical-align:text-top;">         
Record11
Record12
Record13
Record14
Record15
Record16
Record17
Record18
Record19
Record20    
</td></tr></table></div>           


If I understand the question correctly, you want the <div> opener on $ui = 1,11,21,31,... and the </div> closer on 10,20,30,40,...

If this is correct, your modulus operators should be changed as such:

if($ui%$balloon_holds == 1)
{                    
    ...
}

...

if($ui%$balloon_holds == 0)
{
    ...
}


if($ui==1||$ui%$balloon_holds != 0)

This will always be true except for when $ui is a multiple of 10, so you'd be outputting your header/footer blocks for ALL rows except 0, 10, 20, etc... and the one case where $ui is 1.

Most like likely this would be easier to understand:

$row_cnt = 0;
$max_rows = 10;

while($row = ...) {
    if ($row_cnt == 0) {
       // output header
    }
    // output row data
    if ($row_cnt == 9) {
       // output row footer
    }
    $row_cnt++;
    $row_cnt %= $max_rows; // ($row_cnt resets to 0 when it reaches 10)
}


Here is my suggestion, it's also a little more readable / maintainable.

    $ui=0;
    $balloon_holds = 10;

    while ($row = mysql_fetch_array($result))
    {
        if ($ui % $balloon_holds)
        {
            if ($ui >= 10)
            {
                echo '</td></tr></table></div>';
            }

            echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
            echo "<td style=\"font-size:small;vertical-align:text-top;\">";
        }

        echo '<input disabled type="checkbox" value="$row[id]"';
        $this->ischecked($uid,$row[id]);
        echo "/>".$row['name'].'<br>'."\r\n";

        $ui++;
    }

    if ($ui > 0)
    {
        echo '</td></tr></table></div>';
    }


$ui=0;
$balloon_holds = 10;

while($row = mysql_fetch_array($result))
{            

        $exit = 0;
   if($ui==1||$ui%$balloon_holds != 0)
            {                    
            echo '<div><table style="width:400px;border:2px solid gray;border-style:dashed;"><tr>';
            echo "<td style=\"font-size:small;vertical-align:text-top;\">";
            }


        echo '<input disabled type="checkbox" value="$row[id]"'; $this->ischecked($uid,$row[id]); echo "/>".$row['name'].'<br>'."\r\n";

$ui++; 


        if($ui%$balloon_holds == 0){             
  echo '</td></tr></table></div>';          
        $exit = 1;
        }





}

if($exit == false){
    echo '</td></tr></table></div>';  
}
0

精彩评论

暂无评论...
验证码 换一张
取 消