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

使用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

我试图在两列(并排)中动态显示产品列表,我不知道如何 用表做 p>

这是我的代码,它在一列中显示所有产品 p>

  $ productCount = sqlsrv_num_rows($ stmt);  //计算输出量
 $ columncount = 0; 
 $ dynamicList ='&lt; table width =“744”border =“0”cellpadding =“6”&gt;&lt; tr&gt;'; 
while($ row  = sqlsrv_fetch_array($ stmt)){
 $ id = $ row [“productID”]; 
 $ product_name = $ row [“product_name”]; 
 $ product_price = $ row [“product_price”]; 
 $  dynamicList。='&lt; td width =“135”&gt;&lt; a href =“product_details.php?productID ='。$ id。'”&gt;&lt; img src =“images / products / Small / Men /'  。$ id。'.jpg'alt =“'。$ product_name。'”width =“129”height =“169”border =“0”&gt;&lt; / a&gt;&lt; / td&gt; 
&lt; td width  =“593”valign =“top”&gt;'  。  $ product_name。  '&lt; br&gt; 
£'。  $ product_price。  '&lt; br&gt; 
&lt; a href =“product_details.php?productID ='。$ id。'”&gt;查看商品详情&lt; / a&gt;&lt; / td&gt;'; 
 
 if($ columncount  == 2){
 $ dynamicList。='&lt; / tr&gt;&lt; tr&gt;'; 
 $ columncount = 0; 
} else 
 $ columncount ++;  
 
 
 
 $ dynamicList。='&lt; / tr&gt;&lt; / table&gt;'; 
 
&lt;?php echo $ dynamicList;  ?&gt;&lt; br&gt; 
  code>  pre> 
 
 

如何让我的产品以两列而不是一列显示? p>

使用工作解决方案更新了我的帖子 p> div>

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;