tags:

views:

239

answers:

4

How can I display data in two columns?

 <?php
$query = "SELECT * FROM `release` WHERE artist_id = '$rcode' AND label_id = '$id'";
$result = mysql_query($query);
$tr_no = mysql_num_rows($result);
while ($info = mysql_fetch_array($result)) {
?>  
        <div>
     <table style="width: 100%">
       <tr>
        <td ><img src="../artwork/<?php echo $info['label_id']; ?>/<?php echo $info['ID']; ?>.jpg" width="100" height="100" /></td>
        <td valign="top">
        <table style="width: 100%">
         <tr>
          <td style="width: 45px; height: 20px;" class="style5">
          &nbsp;</td>
          <td style="width: 180px"><?php echo $info['code']; ?></td>
         </tr>
         <tr>
          <td style="width: 45px; height: 20px;" class="style5">
          &nbsp;</td>
          <td style="width: 180px"><?php echo $info['name']; ?></td>
         </tr>
         <tr>
          <td style="width: 45px; height: 20px;" class="style5">
          &nbsp;</td>
          <td style="width: 180px"><?php echo trh($info['date']); ?></td>
         </tr>
         <tr>
          <td style="width: 45px; height: 20px;" class="style5">
          &nbsp;</td>
          <td style="width: 180px">

          </td>
         </tr>
        </table>
        </td>
       </tr>
      </table>
        </div>
        <?php
        }
        ?>

Normally I am using this for just one column.

What is the easiest way of displaying it in two columns with the same fields I have in this code?

A: 

You have several options depending on how fancy you want to get with your HTML/CSS. You can try to keep each to 50% width and float them left and the elements should automatically arrange themselves in two columns.

You can also be more explicit using a table-based layout. In this case, start with a and tag, then add elements inside your loop. After every two elements ( $index % 2 == 0), write a to start a new row.. then, after your loop is finished, close your . If you go this route, you'll want to make sure to add an extra if you've got an odd number of results from your query.

There may be more sophisticated ways to do this, but this is simple and straightforward enough to get the job done.

saleemshafi
A: 
Don
can you show an example if possible ?
Ahmet vardar
+1  A: 

The answer also depends on the way you'd like to show the info.

If we assume you have the following six artist_id's in your DB

1,2,3,4,5,6

in which way would you like to show them? This way

1  2
3  4
5  6

or this way?

1  4
2  5
3  6

Update: as you say you'd like to show them in the first way, the solution is pretty simple. Every pair iteration (0, 2, 4...) you should open the row and every odd iteration (1, 3, 5...) you should close it , so you'd have

<tr><td>0</td><td>1</td></tr><tr><td>2</td><td>3</td></tr>...

The code would be

<?php
$query = "SELECT * FROM `release` WHERE artist_id = '$rcode' AND label_id = '$id'";
$result = mysql_query($query);
$tr_no = mysql_num_rows($result);
$ii = 0; // Iterator
while ($info = mysql_fetch_array($result)) {
?>  
        <div>
                <table style="width: 100%">
                        <?php if ($ii%2 == 0) { // If pair, we open tr?>
                        <tr>
                        <?php } ?>
                                <td ><img src="../artwork/<?php echo $info['label_id']; ?>/<?php echo $info['ID']; ?>.jpg" width="100" height="100" /></td>
                                <td valign="top">
                                <table style="width: 100%">
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo $info['code']; ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo $info['name']; ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px"><?php echo trh($info['date']); ?></td>
                                        </tr>
                                        <tr>
                                                <td style="width: 45px; height: 20px;" class="style5">
                                                &nbsp;</td>
                                                <td style="width: 180px">

                                                </td>
                                        </tr>
                                </table>
                                </td>
                        <?php if ($ii%2 == 1) { // If odd, we close tr?>
                        </tr>
                        <?php } ?>
                </table>
        </div>
<?php $ii++; // We increment the iterator }
?>
Sergi
first one, i mean 1,3,5 (column 1) and 2,4,6 (column 2)
Ahmet vardar
it worked ! thank you so much, and how can i make it 3 or 4 columns ?
Ahmet vardar
A: 

get the total number of pictures, you can do that dynamically, and then put everything in a for loop as follows:

let's say you want 9 pictures on three columns

for ($i = 0; $i<=6; $i=($i + 3)) { $result = mysql_query("select * from users order by id desc limit $i,3 "); while (mysql_fetch_array($result)) { bla bla bla html code } }

good luck

alex