dongzhilian0188 2012-05-05 17:03
浏览 36
已采纳

使用PHP和SQL Server 2008在两列中显示动态产品列表

I am trying to dynamically display a list of products in two columns (side by side) and I am not sure how to do it with tables

Here is my code that displays all products in one column

    $productCount = sqlsrv_num_rows($stmt); // count the output amount
$columncount = 0;
$dynamicList = '<table width="744" border="0" cellpadding="6"><tr>';
while($row = sqlsrv_fetch_array($stmt)){ 
         $id = $row["productID"];
         $product_name = $row["product_name"];
         $product_price = $row["product_price"];
         $dynamicList .= '<td width="135"><a href="product_details.php?productID=' . $id . '"><img src="images/products/Small/Men/' . $id . '.jpg" alt="' . $product_name . '" width="129" height="169" border="0"></a></td>
<td width="593" valign="top">' . $product_name . '<br>
  £' . $product_price . '<br>
  <a href="product_details.php?productID=' . $id . '">View Product Details</a></td>';

  if($columncount == 2){
    $dynamicList .= '</tr><tr>';
    $columncount = 0;
  }else
    $columncount++; 
 }

$dynamicList .= '</tr></table>';

<?php echo $dynamicList; ?><br>

How do I get my products to display in two columns instead of one?

Updated my post with the working solution

  • 写回答

2条回答 默认 最新

  • dougudu3564 2012-05-05 17:08
    关注

    You are creating a table for each row in the resultset

    It should be something like this:

    $columncount = 0;
    $dynamicList = '<table width="744" border="0" cellpadding="6"><tr>';
    while($row = sqlsrv_fetch_array($stmt)){ 
             $id = $row["productID"];
             $product_name = $row["product_name"];
             $product_price = $row["product_price"];
             $dynamicList .= '<td width="135"><a href="product_details.php?productID=' . $id . '"><img src="images/products/Men/' . $id . '.jpg" alt="' . $product_name . '" width="129" height="169" border="0"></a></td>
    <td width="593" valign="top">' . $product_name . '<br>
      £' . $product_price . '<br>
      <a href="product_details.php?productID=' . $id . '">View Product Details</a></td>';
    
      if($columncount == 3){
        $dynamicList .= '</tr><tr>';
        $columncount = 0;
      }else
        $columncount++; 
     }
    
    $dynamicList .= '</tr></table>';
    
    echo $dynamicList;
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测