使用样式显示Mysql表中的数据

时间:2013-04-08 08:32:55

标签: php mysql html-table

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

Table Format

这是它的源代码:

  <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表中的所有行但是它有多个单元格,我是否必须使用嵌套循环来打印它?

2 个答案:

答案 0 :(得分: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>';
   }
?>

答案 1 :(得分:0)

你呈现的是一个循环,所以使用一个循环(甚至多个)。将所有这些值插入到db行中,添加必要的列并使用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)