I'm quite new to html and php, I've tried a few things trying to achieve what i want but it never works.
It's a shopping cart, I'm trying to display data from mysql using php in a table, it would be simple if I didn't have other elements that I want to display in the table as well ($value and $sub) which aren't stored in mysql but are calculations.
When I don't try to display it in a table it all works perfectly, and when I add the table I can only get the 'name' to display in the table, If I try to include anything else it fails.
Code below displays 'name' under the table name column, next thing to display would be '$value' under quantity and so on (haven't added anymore columns yet)
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' @ $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Possible code to render column and row properly.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. '</td>' .$value. ' @ $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'" >[Delete]</a><br />';
}
}
$total += $sub;
}
}
?>
</tr>
</table>
Thanks Amit
I moved the table outside of your loop, that will fix a majority of your problems. Another issue is you only had one <td>
wrapping the name column in your while loop, but you didn't have a <td>
wrapping the other information in that row, so I wrapped that.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' @ $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a>'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
}
}
?>
</table>
</div>
Added new code as per your comment.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<?php echo '<td>' .$get_row['name']. $value. ' @ $'.number_format($get_row['price'], 2). ' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a> <a href="cart.php?delete='.$id.'" >[Delete]</a><br /></td> </tr>';
}
}
$total += $sub;
}
}
?>
</table>
First thing i want to tell you is mysql_*
is officially deprecated as of PHP 5.5
. And removed entirely as of PHP 7.0
. So please dont use it.
Refer : http://php.net/manual/en/migration55.deprecated.php
For mysqli_*
: http://php.net/manual/en/book.mysqli.php
Now about you code :
You are using table
tag inside the loop. This will generate multiple tables. Try out this code :
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query("SELECT id_product, name, price FROM elec_guit WHERE id_product='$id'");
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
echo '<tr><td>' . $get_row['name']. '</td>' .
'<td>' . $value. ' @ $'.number_format($get_row['price'], 2).
' = $'.number_format($sub, 2).' <a href="cart.php?remove='.$id.'">[-]</a> <a href="cart.php?add='.$id.'">[+]</a>'
'<a href="cart.php?delete='.$id.'" >[Delete]</a></td></tr>';
}
}
$total += $sub;
}
}
?>
</table>
Use as below code. hope it will help you.
<div class="table">
<table>
<tr>
<td>Name</td>
<td>Quantity</td>
</tr>
<?php
foreach($_SESSION as $name => $value) {
if ($value>0) {
if (substr($name, 0, 5)=='cart_') {
$id = substr($name, 5, (strlen($name)-5));
$get = mysql_query('SELECT id_product, name, price FROM elec_guit WHERE id_product='.($id));
while ($get_row = mysql_fetch_assoc($get)) {
$sub = $get_row['price']*$value;
?>
<tr>
<td><?php echo $get_row['name']; ?></td>
<td><?php echo $value;?> @ $<?php echo number_format($get_row['price'], 2); ?> = $<?php echo number_format($sub, 2); ?> <a href="cart.php?remove=<?php echo $id; ?>">[-]</a> <a href="cart.php?add=<?php echo $id; ?>">[+]</a> <a href="cart.php?delete=<?php echo $id; ?>" >[Delete]</a><br /></td>
</tr>
<?php }
}
$total += $sub;
}
}
?>
</table>
</div>
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.