[英]php echo in html table
I'm quite new to html and php, I've tried a few things trying to achieve what i want but it never works. 我是很新的HTML和PHP,我试过几件事情要达到我想要的,但它永远不会奏效。
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. 这是一个购物车,我正在尝试使用php在表中显示mysql的数据,如果我也不想在表中显示其他元素($ value和$ sub),这将很简单不是存储在mysql中,而是计算。
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. 另一个问题是,您只有一个<td>
将名称列包装在while循环中,但是您没有<td>
将其他信息包装在该行中,因此我将其包装。
<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
. 我想告诉你的第一件事是mysql_*
从PHP 5.5
正式被弃用。 And removed entirely as of PHP 7.0
. 从PHP 7.0
完全删除。 So please dont use it. 所以请不要使用它。
Refer : http://php.net/manual/en/migration55.deprecated.php 请参阅: http : //php.net/manual/en/migration55.deprecated.php
For mysqli_*
: http://php.net/manual/en/book.mysqli.php 对于mysqli_*
: http : mysqli_*
Now about you code : 现在关于您的代码:
You are using table
tag inside the loop. 您正在循环中使用table
标签。 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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.