0

I have a database table and that table has 6 rows. What I want is to display that 6 rows in a html page using a 3 column and 2 row table.

I know how to work with php arrays and while loops. My problem is how to limit the array to put 3 items in the first row and put the other 3 in the next row.

this is what i have tried but didn't work

<div id="maincontent">
  <!-- class one -->
      <?php 
        $getSection = getSection();
        $i=0;
        while($allSection = mysql_fetch_array($getSection)){
      ?>
      <div class="subconent">
<table width="937" border="0">
  <tr>
    <td>
        <div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); $i++; ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div>
    </td>
  </tr>
</table>
</div>
<?php 
if($i==4) { ?>
<table width="937" border="0">
<tr>
<td>&nbsp;</td>
<td>&nbsp;</td>
<td>&nbsp;</td></tr>
<tr><div class="sub_image">
            <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="admin/uploads/fron_sect/<?php echo $allSection['image']; ?>" width="134" height="120" border="0" alt="HNA" class="PopBoxImageLink"  onmouseover="PopEx(this,-50,-25,205,186,20,null);" onclick="window.location='http://localhost/hants/section.php?id=<?php echo urlencode($allSection['id']); ?>'" /></a>
        </div>
            <div class="cont_txt">
              <h3><?php echo $allSection['name_full']; ?></h3>
                <p><?php echo substr($allSection['description'],0,140) . ""; ?></p>
                <br />
              <a href="section.php?id=<?php echo urlencode($allSection['id']); ?>"><img src="images/read_more.jpg" alt="Read More" width="89" height="25" border="0" /></a>
            </div><td>
<?php   }
} ?>
  </div>
2
  • i put those code in to my question now Commented Aug 27, 2012 at 17:16
  • 1
    Question is way too vague. You could show an example dataset from your table, you can add an example of the table output you want, and most important examples of what you have tried. Commented Aug 27, 2012 at 17:17

5 Answers 5

7

Use modulo operator (%):

http://www.devchunks.com/web-development/using-the-php-modulus-operator/

something like this:

<table>

<?php

$i = 0;
while ( $row = mysql_fetch_array($result) ){
    if ($i % 3 == 0){
        echo '<tr>';
    }
    echo '<td>'.$row['column_name'].'</td>';
    if ($i % 3 == 2){
        echo '</tr>';
    }
    $i++; 
}

//here is a check in case you don't have multiple of 3 rows
if ($i % 3 != 0){
    echo '</tr>';
}

?>

</table>
Sign up to request clarification or add additional context in comments.

2 Comments

thanks this is solved my problem but there is a little problem. when i use echo i have to use either "" or '' to wrap echo statement. but i have more html content that using '' and "" so what can i do
use ', i will update the answer, but I don't really see how this hurts you, the html content should still print out correctly on the screen.
1

At its base, you'll need something like this:

<table>
<tr>

<?

$count = 0;
foreach ($row) {
    echo "<td>" . $row["value"] ."</td>";
    $count++;

    if (($count % 3) == 0) && ($count > 0) {
        echo ("</tr><tr>");
    }
}

?>

</tr>
</table>

Start printing out the header of your table, and then begin iterating through the dataset. Keep track of how many you've printed out, and if this is the third one, print the HTML to finish this row and start the next one. (I've used %, so it'll wrap on every third entry, not just the first one)

Comments

0

Well, you could correctly fetch those informations in your sql-query ( just one example that could fit http://en.wikibooks.org/wiki/MySQL/Pivot_table ).

Or simply fetch everything into PHP arrays.

Oldschool: mysql_query() and while( $row = mysql_fetch_array() ) Newchool: PDO ( http://de.php.net/manual/en/book.pdo.php )

Comments

0

Awesome! Thanks a lot. It works for me, Zend. You can try something like this.

   <table width="1024px" border="0" cellspacing="2" cellpadding="2">  
  <?php
      $i = 0;
      foreach ($this->rows as $row )
      {
          $img = IMAGE_PATH . '/' . 'gallery/' . $row->gly_thumbnail;
          if ($i % 3 == 0)
          {
              echo '<tr>';
          }
  ?>
  <td align="center">
    <img src="<?php echo $img; ?>" width="300" height="215"><br/>
    <?php echo $row->gly_title; ?>
  </td>
  <?php        
          if ($i % 3 == 2)
          {
              echo '</tr>';
          }
          $i++; 
      }

      //here is a check in case you don't have multiple of 3 rows
      if ($i % 3 != 0)
      {
          echo '</tr>';
      }

  ?>
</table>

Comments

0
                    <?php if ($i++%$_columnCount==0): ?>
                        <tr>
                    <?php endif ?>
                  
                   
                    <td> <img src="<?php echo site_url('uploads/shelter_images/'.$row->shelter_id."/".$img->imagefile) ?>" alt="" width="300" ></td>
                    
                     <?php if ($i%$_columnCount==0 || $i==$totalImg): ?>
                      </tr>
                   <?php endif; ?> 
            
            <?php  } ?>

1 Comment

Welcome to Stack Overflow. Code without any explanation are rarely helpful. Stack Overflow is about learning, not providing snippets to blindly copy and paste. Please edit your question and explain how it answers the specific question being asked. See How to Answer.

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.