1

i need to fill a table with mysql data, but i want to add a new line for every 3 mysql records.

filling the table its easy. but how do i tell to add a <tr> to every 3 records??

so far i have this:

 <table width="30%" border="0">
  <tbody>
  <tr>
   <?php if($totalRows_rsSdk>0){
       do{
    ?>     
<td align="center" valign="middle"><input type="checkbox" class="css-checkbox" name="sdkcheck" id="sdkcheck<?php echo $row_rsSdk['id']; ?>"/><label for="sdkcheck<?php echo $row_rsSdk['id']; ?>" class="css-label2 radGroup3"><?php echo $row_rsSdk['nome']; ?></label></td>
 <td align="center" valign="middle"><img src="Images/sdk-icons/<?php echo $row_rsSdk['icon']; ?>" height="70"></td>

    <?php } while ($row_rsSdk = mysql_fetch_assoc($rsSdk));} ?>
     </tr>
  </tbody>
</table>
1

1 Answer 1

1

Try this code:

<table width="30%" border="0">
    <tbody>
    <tr>
        <?php if($totalRows_rsSdk>0){
            $counter = 0;
            do{
                ?>
                <td align="center" valign="middle"><input type="checkbox" class="css-checkbox" name="sdkcheck" id="sdkcheck<?php echo $row_rsSdk['id']; ?>"/><label for="sdkcheck<?php echo $row_rsSdk['id']; ?>" class="css-label2 radGroup3"><?php echo $row_rsSdk['nome']; ?></label></td>
                <td align="center" valign="middle"><img src="Images/sdk-icons/<?php echo $row_rsSdk['icon']; ?>" height="70"></td>

            <?php
                $blockSize = 3;
                $counter++;
                if ($counter >= $blockSize) {
                    $counter = 0;
            ?>
                </tr>
                <tr>
            <?php
                }
            } while ($row_rsSdk = mysql_fetch_assoc($rsSdk));} ?>
            <?php echo str_repeat('<td></td>',($blockSize-$counter)); ?>
    </tr>
    </tbody>
</table>
Sign up to request clarification or add additional context in comments.

5 Comments

Yes, you can use it, but topicstarted doesn't. That's why I post answer in topicstarter style. And not for "form", for HTML templater's.
@ranonymus00 Well, you should use alternate syntax when working with templates
Why do you think, that this is wrong? You have any PSR-* specs, where someone write that you can't use PHP common style in templates?
That PSR-* is not officially published yet, its in drafts I saw on google groups somewhere. Look, what you do $counter = 0; do{ ?> is kinda Word-Press style. Look any decent framework doc, why they recommend and do use alternate syntax
May be topicstarter use wordpress, to you think about this?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.