2011-07-28 34 views
0

我想使數據在2列,而不是一個在這裏展示的代碼是我目前使用的1個欄,顯示的數據:如何使MySQL數據顯示在2列中?

<?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 
_________ | _________ 

回答

0

爲什麼它的價值,我把MarcB的代碼合併到你的原代碼中。你已經在用你的行數進行修改了。我想你想是這樣的:

<?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/>"; 
} 

>

+0

嗨,我收到以下錯誤: 解析錯誤:語法錯誤,意想不到的T_IF,期待「」或‘;’在線40上的/home/.../public_html/values/show_rares.php – Dean

+0

固定的「;」在最後的後失蹤「,非常感謝。 – Dean

+0

沒問題院長,我更新了代碼以反映 – DaOgre

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 
    } 
} 
相關問題