Reputation: 444
First of all I am not very familiar with this.
$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();
?>
This displays my results in the first column only. How can I display results in the second column as well
like
1 - a | 2 - b
3 - c | ...
without changing the above table layout?
Upvotes: 0
Views: 212
Reputation: 46610
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, %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 />€' . $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();
?>
Upvotes: 1
Reputation: 141
I guess you mean this and i hope it'll be usefull for 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();
?>
Upvotes: 0