如何使MySQL数据显示在2列?

时间:2011-07-28 15:51:59

标签: php mysql

我想让数据显示在2列而不是1列中,这是我目前用于在1列中显示数据的代码:

    <?php 
include("config.php");
include("opendb.php");
$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
$get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
echo("<h2>".$cats['name']."</h2><br>
<table width=\"100%\" border=\"0\">
<tr>
<td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
<td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
<td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
<td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
</tr>
");
$color1 = $info[stripe1]; 
$color2 = $info[stripe2];
$row_count = 0;
while($rare = mysql_fetch_array($get_rares)) {
$row_color = ($row_count % 2) ? $color1 : $color2;
?>
<tr>
<td width="5%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><img alt="" src="<?php echo("".$info[imagepath]."".$rare['image'].""); ?>"></td>
<td width="20%" style="text-align:left;background-color:#<?php echo $row_color; ?>"><?php echo $rare['name']; ?></td>
<td width="20%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['value']; ?> Credits</td>
<td width="10%" style="text-align:center;background-color:#<?php echo $row_color; ?>"><?php echo $rare['lastedited']; ?></td>
</tr>
<?php 
$row_count++;
}
echo("</table><br>

");
}
?>

目前显示为:

1
_________

2
_________

3
_________

4
_________

5
_________

6
_________

我希望它显示如下:

1         | 2
_________ | _________
3         | 4
_________ | _________
5         | 6
_________ | _________

2 个答案:

答案 0 :(得分:1)

这根本与MySQL没有任何关系。 MySQL只是数据的来源。你想要这样的东西:

$record = 0;
while($rare = mysql_fetch_array($get_rares)) {
    if ($record % 2 == 0) {
       echo "<tr>"; // if on an 'even' record, start a new row
    }
    echo "<td>{$rare['something']}</td>";
    $record++;
    if ($record % 2 == 0) {
       echo "</tr>"; // close the row if we're on an even record
    }
}

答案 1 :(得分:0)

为了它的价值,我将MarcB的代码整合到您的原始代码中。你已经在用你的行计数做mod了。我想你想要这样的东西:

<?php 
include("config.php");
include("opendb.php");

$get_cats = mysql_query("SELECT * FROM `categories` ORDER BY `displayorder`") or die(mysql_error());
$get_info = mysql_query("SELECT * FROM `systeminfo`") or die(mysql_error());
$info = mysql_fetch_array($get_info);
while($cats = mysql_fetch_array($get_cats)) {
        $get_rares = mysql_query("SELECT * FROM `rares` WHERE `catid`='".$cats['id']."'") or die(mysql_error());
        echo("<h2>".$cats['name']."</h2><br>
                <table width=\"100%\" border=\"0\">
                <tr>
                        <td width=\"20%\" style=\"text-align:center\"><b>Image</b></td>
                        <td width=\"40%\" style=\"text-align:left\"><b>Item Name</b></td>
                        <td width=\"10%\" style=\"text-align:center\"><b>Value</b></td>
                        <td width=\"30%\" style=\"text-align:center\"><b>Last Updated</b></td>
                        <td width=\"20%\" style=\"text-align:center\"><b>Image2</b></td>
                        <td width=\"40%\" style=\"text-align:left\"><b>Item Name2</b></td>
                        <td width=\"10%\" style=\"text-align:center\"><b>Value2</b></td>
                        <td width=\"30%\" style=\"text-align:center\"><b>Last Updated2</b></td>
                </tr>
                ");

        $color1 = $info[stripe1]; 
        $color2 = $info[stripe2];

        $row_count = 0;

        while($rare = mysql_fetch_array($get_rares)) {
                $row_color = ($row_count % 2) ? $color1 : $color2;
                if ($row_count % 2 == 0) {
                        echo "<tr>"; // if on an 'even' record, start a new row
                }

                echo "<td width='5%' style='text-align:center;background-color:#$row_color;'><img alt='' src='{$info[imagepath]}{$rare['image']}'></td>
                                <td width='20%' style='text-align:left;background-color:#$row_color;'>{$rare['name']}</td>
                                <td width='20%' style='text-align:center;background-color:#$row_color;'>{$rare['value']} Credits</td>
                                <td width='10%' style='text-align:center;background-color:#$row_color;'>{$rare['lastedited']}</td>";

                if ($row_count % 2 == 0) {
                        echo "</tr>"; // close the row if we're on an even record
                }

                $row_count++;
        }

        echo "</table><br/>";
}

&GT;