how to display data in two column with php
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">
</td>
<td style="width: 180px"><?php echo $info['code']; ?>&l开发者_开发技巧t;/td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo $info['name']; ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo trh($info['date']); ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</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?
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">
</td>
<td style="width: 180px"><?php echo $info['code']; ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo $info['name']; ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</td>
<td style="width: 180px"><?php echo trh($info['date']); ?></td>
</tr>
<tr>
<td style="width: 45px; height: 20px;" class="style5">
</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 }
?>
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.
Unless you have a "LARGE" data set,
1) bring all the rows out into a numerically indexed array
2) divide the number of rows by two: x1 = 0; x2 = (int) count/2;
3) loop from 0 to count/2, creating a table with two columns,
3a) those indexed by x1++ in the first column
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.
<table border='1'><tr><?phpfor ($i = 0; $i<=17; $i=($i + 3)){echo "<td>$i</td>";}?></tr><tr><?phpfor ($n = 1; $n<=17; $n=($n + 3)){ echo "<td>$n</td>";}?></tr><tr><?phpfor ($m = 2; $m<=17; $m=($m + 3)){echo "<td>$m</td>";}?></tr></table>
- |0|3|6|9|12|15|
- |1|4|7|10|13|16|
- |2|5|8|11|14|17|
Thank you :)
<table border='1'><?phpfor ($n = 0; $n<=2; $n++) {echo "<tr>";for ($i = $n; $i<=17; $i=($i + 3)){ echo "<td>$i</td>";}echo "</tr>";}?></table>
just use two loop like this result
0 3 6 9 12 15
1 4 7 10 13 16
2 5 8 11 14 17
精彩评论