簡體   English   中英

在PHP和SQL Server 2008的兩列中顯示動態產品列表

[英]Display dynamic product list in two columns with PHP and SQL Server 2008

我正在嘗試在兩列(並排)中動態顯示產品列表,但不確定如何使用表格

這是我的代碼,在同一列中顯示所有產品

    $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>

如何使我的產品顯示在兩列而不是一列中?

使用工作解決方案更新了我的帖子

您正在為結果集中的每一行創建一個表

應該是這樣的:

$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