问题描述
|
首先,我对此不太熟悉。
$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 />€\' . $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,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 />€\' . $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();
?>
,我想你是说这个,我希望它对你有用
<?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,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();
?>