1

我已经编写了一个可以使用 PHP 从 mysql 保存数据的代码,那么如何使用 CSS 样式的表格来显示它。存储在我的服务器中的数据是:产品名称、产品规格、价格和产品图片链接。如何以这种格式显示所有这些:

表格格式

这是它的源代码:

  <body>

<div class="gridContainer clearfix">
<div id="LayoutDiv1">
<center><font color="White" size="+12"><u>PROCESSORS</u></font></center>
<table border=0 align="center">
<th colspan="3"><h2><font color="White">AMD PROCESSORS</font></h2></th>
<th></th>
<th colspan="3"><h2><font color="White">INTEL PROCESSORS</font></h2></th>
<tr><td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
 <th align="center" colspan="2" bgcolor="#FF9900"> AMD ATHLON II X2 260</th>
<tr><td >Specs:</td><td>3.20Ghz<br />2 cores<br />2Mb Cache<br />Supports x86 and x64 
OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td >Price:</td><td>2500</td></tr>
<tr><td >Quantity:</td><td><input type=text name=quant></td></tr>
<tr><td >Add to cart?</td><td ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="AMD">
<input type=hidden name=prdname value="AMD ATHLON II X2 260">
<input type=hidden name=prc value="2500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd1.jpg" />
</td>

<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">INTEL i3</th>
<tr><td >Specs:</td><td >3.20Ghz<br />2 cores<br />3Mb Cache<br />Supports x86 and x64 
OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td >Price:</td><td >2500</td></tr>
<tr><td >Quantity:</td><td  ><input type=text name=quant></td></tr>
<tr><td >Add to cart?</td><td  ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="INTEL">
<input type=hidden name=prdname value="INTEL i3">
<input type=hidden name=prc value="2500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd5.jpg" />
</td>
</tr>
<tr><td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">AMD SEMPRON</th>
<tr><td >Specs:</td><td >2.20Ghz<br />4 cores<br />4Mb Cache<br />Supports x86 and x64 
OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td  >Price:</td><td  >3000</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
<tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="AMD">
<input type=hidden name=prdname value="AMD SEMPRON">
<input type=hidden name=prc value="3000">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd2.jpg" />
</td>

<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">INTEL i5</th>
<tr><td  >Specs:</td><td  >3.40Ghz<br />4 cores<br />6Mb Cache<br />Supports x86 and 
x64 OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td  >Price:</td><td  >5500</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
<tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="INTEL">
<input type=hidden name=prdname value="INTEL i5">
<input type=hidden name=prc value="5500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd6.jpg" />
</td>

</tr>
<tr><td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">AMD PHENOM II x6</th>
<tr><td  >Specs:</td><td  >3.7 GHz<br />6 cores<br />6Mb Cache<br />Supports x86 and 
x64 OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td  >Price:</td><td  >4500</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
<tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="AMD">
<input type=hidden name=prdname value="AMD PHENOM II x6">
<input type=hidden name=prc value="4500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd3.jpg" />
</td>

<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">INTEL i7</th>
<tr><td  >Specs:</td><td  >4.0Ghz<br />4 cores<br />8Mb Cache<br />Supports x86 and x64
OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td  >Price:</td><td  >6500</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
<tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="INTEL">
<input type=hidden name=prdname value="INTEL i7">
<input type=hidden name=prc value="6500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd7.jpg" />
</td>

</tr>
<tr><td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">AMD OPTERON x16</th>
  <tr><td  >Specs:</td><td  >3.30Ghz<br />16 cores<br />2Mb Cache<br />Supports x86 and
 x64 OS<br />w/ Cool'n Quiet Tech</td></tr>
  <tr><td  >Price:</td><td  >7500</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
 <tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="AMD">
<input type=hidden name=prdname value="AMD OPTERON x16">
<input type=hidden name=prc value="7500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd4.jpg" />
</td>

<td>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</td>
<td>
<form action=cds.php method=get>
<table border="0" bordercolorlight="#FFFF99">
  <th align="center" colspan="2" bgcolor="#FF9900">INTEL PENTIUM 4 G870</th>
<tr><td  >Specs:</td><td  >3.20Ghz<br />2 cores<br />2Mb Cache<br />Supports x86 and 
x64 OS<br />w/ Cool'n Quiet Tech</td></tr>
<tr><td  >Price:</td><td  >3500</td></tr>
<tr><td  >Quantity:</td><td   ><input type=text name=quant></td></tr>
<tr><td  >Add to cart?</td><td   ><input type="submit" value="Yes"></td></tr>
</table>
<input type=hidden name=comp value="INTEL">
<input type=hidden name=prdname value="INTEL PENTIUM G870">
<input type=hidden name=prc value="3500">
</form>
</td>
<br />
<td>&nbsp;&nbsp;&nbsp;</td>
<td>
<img src="img/prd8.jpg" />
    </td>

</tr>
<tr><td><br /><br /></td></tr>
</table>
  </div>
</div>
</body>

我知道它需要循环来打印 mysql 表中的所有行,但它有多个单元格,我必须使用嵌套循环来打印吗?

4

2 回答 2

0
    <?php
    //get data from database related to that product 
   while($row=mysqli_fetch_array($result,MYSQLI_ASSCO))
   {
    echo '<table>';
    echo '<tr>;
    echo '<td>';
    echo 'Prodcut name';
    echo '</td>';
    echo '</tr> </table>';
   }
?>
于 2013-04-08T08:42:38.363 回答
0

你呈现的是一个循环,所以使用一个循环(甚至多个)。将所有这些值插入到数据库行中,添加必要的列并使用 WHILE LOOP 来获取数据。在循环中仅包含一个表格 html,因此您无需重复代码。

更快的方法是使用产品名称创建一个从数据库中获取数据的函数。这将为您节省大量时间

更正表格,关闭输入字段上的引号

<input type=hidden name="comp" value="">
    <input type=hidden name="prdname" value="">
    <input type=hidden name="prc" value="">

and <form action="cds.php" method="get">
(for this need, I would prefer method POST)
于 2013-04-08T08:55:01.163 回答