DynamicList - Multiple columns


DynamicList - Multiple columns


首先,我对此不是很熟悉。

 $dynamicListBody = "";
$sql = mysql_query("SELECT * FROM products WHERE category='Body' ORDER BY id ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
         $product_name = $row["product_name"];
         $details = $row["details"];
         $price = $row["price"];
         $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
         $dynamicListBody .= '
         <table width="95%">
              <tr>
               <td width="10%">
        <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" />
           </td>
               <td width="35%">
            <span class=itmttl>' . $product_name . '</span>
        <br />
                <span class=text>' . $details . '
                <br />
                €' . $price . '</span>
        <br />
                <form id="bd_itm1" name="bd_itm1" method="post" action="help_scripts/cart_functions.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Cart" />
                </form>
           </td>
           <td width="5%">
           </td>
           <td width="10%">
           </td>
           <td width="35%">
        <br />
        <form id="bd_itm2" name="bd_itm2" method="post" action="help_scripts/cart_functions.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Cart" />
                </form>
           </td>
              </tr>
             </table>';
    }
} else {
    $dynamicListBody = "We have no products listed in our store yet";
}
mysql_close();
?>

这只在第一列中显示我的结果。如何在第二列中显示结果
喜欢
1-a|2-b
3-c|
而不改变上面的表格布局?

x2    
<?php 
     $sql = mysql_query("SELECT * FROM products WHERE category='Body' ORDER BY id ASC");
     $productCount = mysql_num_rows($sql); // count the output amount
     if ($productCount > 0) {
        $i=0;
        $dynamicListBody = '<table width="95%">';
        while($row = mysql_fetch_array($sql)){
            $id = $row["id"];
            $product_name = $row["product_name"];
            $details = $row["details"];
            $price = $row["price"];
            $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
            $dynamicListBody .= ($i==0) ? '<tr>':''; 
            $dynamicListBody .= '
                <td width="10%">
                      <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" />
                </td>
                <td width="35%">
                  <span class=itmttl>' . $product_name . '</span>
                  <br />
                  <span class=text>' . $details . '<br />&euro;' . $price . '</span>
                   <br />
                    <form id="bd_itm1" name="bd_itm1" method="post" action="help_scripts/cart_functions.php">
                    <input type="hidden" name="pid" id="pid" value="'.$id.'" />
                    <input type="submit" name="button" id="button" value="Add to Cart" />
                    </form>
                </td>';
            $dynamicListBody .= ($i==1) ? '</tr>':''; 
            $i++;
            ($i==2) ? $i=0:'';
        } 
        $dynamicListBody.='</table>';
     } else {
        $dynamicListBody = "We have no products listed in our store yet";
     }
     mysql_close();
    ?>
//4
    <?php 
     $sql = mysql_query("SELECT * FROM products WHERE category='Body' ORDER BY id ASC");
     $productCount = mysql_num_rows($sql); // count the output amount
     if ($productCount > 0) {
        //iteration set to 0
        $i=0;
        $dynamicListBody = '<table width="95%">';
        while($row = mysql_fetch_array($sql)){
            $id = $row["id"];
            $product_name = $row["product_name"];
            $details = $row["details"];
            $price = $row["price"];
            $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
            //$i controls the first <tr> in the loop
            $dynamicListBody .= ($i==0) ? '<tr>':''; 
            $dynamicListBody .= '
                <td width="10%">
                      <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" />
                </td>
                <td width="35%">
                  <span class=itmttl>' . $product_name . '</span>
                  <br />
                  <span class=text>' . $details . '<br />&euro;' . $price . '</span>
                   <br />
                    <form id="bd_itm1" name="bd_itm1" method="post" action="help_scripts/cart_functions.php">
                    <input type="hidden" name="pid" id="pid" value="'.$id.'" />
                    <input type="submit" name="button" id="button" value="Add to Cart" />
                    </form>
                </td>';
            //is 3 as first iteration is 0 meaning 4 rows will get outputted when $i is 3 as started on 0 not 1
            $dynamicListBody .= ($i==3) ? '</tr>':''; 
            $i++;
            //note $i was set to 4(above $i++) before checking, meaning 4 rows have been outputted
            ($i==4) ? $i=0:'';
        } 
        $dynamicListBody.='</table>';
     } else {
        $dynamicListBody = "We have no products listed in our store yet";
     }
     mysql_close();
    ?>

我想你是这个意思,我希望它对u 有用

<?php
 $dynamicListBody = "";
$sql = mysql_query("SELECT * FROM products WHERE category='Body' ORDER BY id ASC");
$productCount = mysql_num_rows($sql); // count the output amount
if ($productCount > 0) {
    $i=0;
    while($row = mysql_fetch_array($sql)){
             $id = $row["id"];
         $product_name = $row["product_name"];
         $details = $row["details"];
         $price = $row["price"];
         $date_added = strftime("%b %d, %Y", strtotime($row["date_added"]));
         $dynamicListBody .= '
         <table width="95%">';
         // to Display 4 cell and n row
          if( $i%4==0 )$dynamicListBody .= ' <tr> ';
         $dynamicListBody .= '    
               <td width="10%">
        <img style="border:#666 1px solid;" src="../stock_photos/' . $id . '.png" alt="' . $product_name . '" />
           </td>
               <td width="35%">
            <span class=itmttl>' . $product_name . '</span>
        <br />
                <span class=text>' . $details . '
                <br />
                €' . $price . '</span>
        <br />
                <form id="bd_itm1" name="bd_itm1" method="post" action="help_scripts/cart_functions.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Cart" />
                </form>
           </td>
           <td width="5%">
           </td>
           <td width="10%">
           </td>
           <td width="35%">
        <br />
        <form id="bd_itm2" name="bd_itm2" method="post" action="help_scripts/cart_functions.php">
                <input type="hidden" name="pid" id="pid" value="<?php echo $id; ?>" />
                <input type="submit" name="button" id="button" value="Add to Cart" />
                </form>
           </td>';
       if( $i%4==0 )$dynamicListBody .= ' </tr> ';
      $dynamicListBody .= ' 
             </table>';
             $i++;
    }
} else {
    $dynamicListBody = "We have no products listed in our store yet";
}
mysql_close();
?>