简体   繁体   English

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

[英]Display dynamic product list in two columns with PHP and 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 使用工作解决方案更新了我的帖子

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;
    //a quick solution will be store the number of result rows
        $nrow=number_of_rows($result);
    //set a counter of the while loop
        $count=0;
    //and in the while loop put if else condition 
        $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"];

        //display the first half of your table in teh left column
             if($coun<$nrow/2){
        $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>';
    $count++;
//display 2 item only per row
if($count%2)
$dynamicList .= '</tr>';
         }else{
//repeate the code but for the rest of the table

    }//end else
   }//end while
//close the table and display it on the browser     
        $dynamicList .='</table>';

        echo $dynamicList;

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM