[英]How to loop sql table data inside a html table
我有一张用户订单的表格,我想通过电子邮件向他们发送订单摘要。 但是,我设法创建了一个显示客户详细信息的表格,现在我想循环用户的所有订单。 我的电子邮件正文存储在变量 $emailbody 中,我想在其中包含用户订购的所有项目。 我不
<?php
include"db.php";
if (isset($_POST['invoicebtn'])) {
$invoice=$_POST['invoice'];
$result1 = mysqli_query($con,"SELECT * FROM users WHERE invoice='$invoice' ");
$rowinvoice = $result1->fetch_assoc();
$name = $rowinvoice['name'];
$email = $rowinvoice['email'];
$phone = $rowinvoice['phone'];
$address = $rowinvoice['address'];
$carttotal = $rowinvoice['orderTotal'];
date_default_timezone_set('Africa/Nairobi');
$date = date('d-m-y h:i:s');
$sql = mysqli_query($con,"SELECT * FROM orders WHERE invoice='$invoice' ");
while($row = mysqli_fetch_array($sql)){
/*Data corespond to table columns as bolow*/
$item=$row['item'];
$qty=$row['quantity'];
$price=$row['price'];
$subtotal=$row['subtotal'];
/*I would like to loop all the data from the page and use it in the table below which I will be sending out in an email body*/
}
$emailbody="<table style='margin:10px auto; border-collapse: collapse; border: 1px solid black; padding-left:10px;'>
<tr>
<th colspan='4' style='border: 1px solid black; padding-left:10px; padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; font-weight:400;'>
<h4>Invoice Order No. # $invoice </h4>
</th>
</tr>
<tr>
<td colspan='4' style='text-align:left; border: 1px solid #ddd; padding: 5px 20px;'>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $name </p>
<p style='text-align:right; padding:5px 15px;'> $address </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $phone </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $email</p>
<p style='text-align:left;line-height: 0.2; padding:5px 15px;'>Invoice Date: $date </p>
</td>
</tr>
<tr style='padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; padding-left:10px; font-weight:400;'>
<th style='border: 0px solid;padding-left:10px;'>ITEM</th>
<th style='border: 0px solid;padding-left:10px;'>QUANTITY</th>
<th style='border: 0px solid;padding-left:10px;'>PRICE</th>
<th style='border: 0px solid;padding-left:10px;'>TOTAL<ksh></th>
</tr>
<!--loophere the table contents here as td in a tr-->
<!--loop here-->
<td colspan='4' style='background-color:#0a0512a8;color:white; text-align: center;padding:10px 25px;'><strong>Total: $ $carttotal </strong></td>
</tr>
</table>";
}else{
header("Location:orders.php?Anauthorized=true");
};
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!--im echooing the variable emailbody to see what will be sent-->
<?php echo $emailbody; ?>
</body>
</html>
知道如何在表中进行
大多数人可能会建议您使用某种模板引擎,例如 twig ( https://twig.symfony.com/ ) 或 mustache ( https://mustache.github.io/ ) 或其他模板引擎。
但是,如果您确实想在没有模板引擎的情况下执行此操作,则可以执行以下操作。
...
<!--loophere the table contents here as td in a tr-->
<?php foreach($orders as $key=>$order): ?>
<tr>
<td>
content 1
</td>
<td>
content 2
</td>
</tr>
<?php endforeach; ?>
...
正如你在这段代码中看到的那样,我们正在使用控制结构的替代语法,这看起来像if(some condition):
然后你可以用endif;
其中:
和endif;
基本上替换您通常使用的大括号。
请注意,上面的代码只会打印为 html 而不是变量,然后回显。
所以你的总 html 看起来像这样:
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<table style='margin:10px auto; border-collapse: collapse; border: 1px solid black; padding-left:10px;'>
<tr>
<th colspan='4' style='border: 1px solid black; padding-left:10px; padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; font-weight:400;'>
<h4>Invoice Order No. # <?php echo $invoice ?> </h4>
</th>
</tr>
<tr>
<td colspan='4' style='text-align:left; border: 1px solid #ddd; padding: 5px 20px;'>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $name ?></p>
<p style='text-align:right; padding:5px 15px;'> <?php echo $address ?> </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $phone ?> </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> <?php echo $email ?></p>
<p style='text-align:left;line-height: 0.2; padding:5px 15px;'>Invoice Date: <?php echo $date ?></p>
</td>
</tr>
<tr style='padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; padding-left:10px; font-weight:400;'>
<th style='border: 0px solid;padding-left:10px;'>ITEM</th>
<th style='border: 0px solid;padding-left:10px;'>QUANTITY</th>
<th style='border: 0px solid;padding-left:10px;'>PRICE</th>
<th style='border: 0px solid;padding-left:10px;'>TOTAL</th>
</tr>
<!--loophere the table contents here as td in a tr-->
<?php foreach($orders as $key=>$order): ?>
<tr>
<td>
content 1
</td>
<td>
content 2
</td>
</tr>
<?php endforeach; ?>
<!--loop here-->
<td colspan='4' style='background-color:#0a0512a8;color:white; text-align: center;padding:10px 25px;'><strong>Total: <?php echo $carttotal ?> </strong></td>
</tr>
</table>
</body>
</html>
此替代表示法适用于多种情况:
PHP 为它的一些控制结构提供了另一种语法; 即 if、while、for、foreach 和 switch。 在每种情况下,备用语法的基本形式是将左大括号更改为冒号 (:),将右大括号更改为 endif;、endwhile;、endfor;、endforeach; 或 endswitch;。
另请参阅 ilanco 提供的答案PHP simple foreach loop with HTML
这里怎么样?
<?php
include "db.php";
if (isset($_POST['invoicebtn'])) {
$invoice = $_POST['invoice'];
$result1 = mysqli_query($con, "SELECT * FROM users WHERE invoice='$invoice' ");
$rowinvoice = $result1->fetch_assoc();
$name = $rowinvoice['name'];
$email = $rowinvoice['email'];
$phone = $rowinvoice['phone'];
$address = $rowinvoice['address'];
$carttotal = $rowinvoice['orderTotal'];
date_default_timezone_set('Africa/Nairobi');
$date = date('d-m-y h:i:s');
$sql = mysqli_query($con, "SELECT * FROM orders WHERE invoice='$invoice' ");
$emailbody = "<table style='margin:10px auto; border-collapse: collapse; border: 1px solid black; padding-left:10px;'>
<tr>
<th colspan='4' style='border: 1px solid black; padding-left:10px; padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; font-weight:400;'>
<h4>Invoice Order No. # $invoice </h4>
</th>
</tr>
<tr>
<td colspan='4' style='text-align:left; border: 1px solid #ddd; padding: 5px 20px;'>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $name </p>
<p style='text-align:right; padding:5px 15px;'> $address </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $phone </p>
<p style='text-align:right;line-height: 0.2; padding:5px 15px;'> $email</p>
<p style='text-align:left;line-height: 0.2; padding:5px 15px;'>Invoice Date: $date </p>
</td>
</tr>
<tr style='padding-top: 6px; padding-bottom: 6px; background-color: #00000061; color: White; padding-left:10px; font-weight:400;'>
<th style='border: 0px solid;padding-left:10px;'>ITEM</th>
<th style='border: 0px solid;padding-left:10px;'>QUANTITY</th>
<th style='border: 0px solid;padding-left:10px;'>PRICE</th>
<th style='border: 0px solid;padding-left:10px;'>TOTAL<ksh></th>
</tr>";
while ($row = mysqli_fetch_assoc($sql)) {
/*Data corespond to table columns as bolow*/
$emailbody .= "
<tr>
<td>" . $row['item'] . "</td>
<td>" . $row['quantity'] . "</td>
<td>" . $row['price'] . "</td>
<td>" . $row['quantity'] * $row['price'] . "</td>
</tr>
";
/*I would like to loop all the data from the page and use it in the table below which I will be sending out in an email body*/
}
$emailbody .= "
<!--loophere the table contents here as td in a tr-->
<!--loop here-->
<td colspan='4' style='background-color:#0a0512a8;color:white; text-align: center;padding:10px 25px;'><strong>Total: $ $carttotal </strong></td>
</tr>
</table>";
} else {
header("Location:orders.php?Anauthorized=true");
};
?>
<!DOCTYPE html>
<html>
<head>
<title></title>
</head>
<body>
<!--im echooing the variable emailbody to see what will be sent-->
<?php echo $emailbody; ?>
</body>
</html>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.