我是PHP新手,目前在用PHP做飲料店的結帳畫面,請問如何用MPDF列印出選定的訂單明細
我用的資料庫是MYSQL這是我的資料表
訂單明細details
o_id (訂單列號) |
z_id (訂單編號) |
o_cup(杯數) |
o_o (處理狀態) |
x_id (商品名稱) |
1 |
1 |
1 |
1 |
1 |
2 |
1 |
2 |
1 |
2 |
3 |
2 |
2 |
1 |
1 |
商品 commodity
x_id(商品編號) |
x_name (商品名稱) |
x_price(商品價錢) |
1 |
紅茶 |
25 |
2 |
綠茶 |
25 |
訂單orders
z_id(訂單編號) |
z_price(訂單總價) |
1 |
75 |
2 |
50 |
PHP的網頁呈現
z_id| x_name |x_price | o_cup | z_price | o_o |
-- | -- | -- | -- | -- | --| --
1 |紅茶|2|1|75| 1| 結帳
1 |綠茶|2|2|75| 1| 結帳
2 |紅茶 |1|2|50 |1| 結帳
因為對PHP不是很會,所以就參考著範例改,應該是MPDF那裡搜索值和連結資料庫的問題,所以測試的結果是錯的。
這是我的程式碼
<?php
$it =!empty($_GET["it"])? $_GET["it"]:"";
$dbServer = "localhost";
$dbName = "nicetea";
$dbUser = "root";
$dbPass = "";
$conn = mysqli_connect($dbServer, $dbUser, $dbPass) or trigger_error(mysql_error(),E_USER_ERROR);
mysqli_select_db($conn,$dbName);
mysqli_query($conn,"SET NAMES utf8");
if($it==""){
echo'
<table width="1000" border="3">
<tr>
<td>訂單編號</td>
<td>飲料</td>
<td>單價</td>
<td>杯數</td>
<td>小計</td>
<td>總金額</td>
</tr>';
$sql = "SELECT details.z_id,x_name, x_price, details.o_cup, orders.z_price, details.o_o
FROM (commodity INNER JOIN details ON commodity.x_id=details.x_id)
INNER JOIN orders ON details.z_id = orders.z_id WHERE o_o ='1' ";
$result = mysqli_query($conn,$sql);
$total_records=mysqli_num_rows($result);
for($i=1;$i<=$total_records;$i++){
$row = mysqli_fetch_row($result);
$it= $row[0];
$z_id= $row[0];
$x_name= $row[1];
$x_price= $row[2];
$o_cup= $row[3];
$money=$row[2] *$row[3];
$z_price=$row[4];
echo"<tr><form>";
echo"<td>$z_id</td>";
echo"<td>$x_name</td>";
echo"<td>$x_price</td>";
echo"<td>$o_cup</td>";
echo"<td>$money</td>";
echo"<td>$z_price</td>";
echo"<td align = 'center'><input type = 'submit' name= 'Submit' value='結帳'/>
<input type='hidden' name ='it' value='$it'/>
</td>";
echo"</form></tr>";
}
echo'</table>';
}
else{
$o=!empty($_GET["o"])?$_GET["o"]:null;
$Submit=!empty($_GET["Submit"])?$_GET["Submit"]:null;
$msg='';
if($Submit=='結帳'){
$sql="UPDATE `details` SET `o_o`='1' WHERE `z_id`='$it' ";
$msg = '已處理完成';
//收據mpdf 目前echo出是錯誤的
require_once __DIR__ . '/vendor/autoload.php';
$mpdf = new \Mpdf\Mpdf([
'mode' => 'utf-8',
'format' => [57,90],
'default_font_size' => '9',
'margin_left'=>'10',
'margin_right'=>'10'
]);
$mpdf->WriteHTML('<hi>hello world</hi>');
$mpdf->WriteHTML('<p align="center" style="font-family: BIG5">Nice茶</p>');
$mpdf->WriteHTML('<p align="center" style="font-family: BIG5">訂單編號 : ' . $it . '</p>');
$tabla="";
$tabla .="<table><tr>
<th>飲料</th>
<th>單價</th>
<th>杯數</th>
<th>小計</th>
<th>總金額</th>
</tr></table>";
while($it = $rs -> fetch_assoc()){
$table .="<tr>
<td>".$x_name."</td>
<td>".$x_price."</td>
<td>".$o_cup."</td>
<td>".$money."</td>
<td>".$z_price."</td>
</tr> ";
}
$tabla . ='</table>';
return $tabla;
$html = $sql;
$pdf= new mPDF('c');
$pdf -> WriteHTML($html);
$mpdf->Output();
exit;
echo"<td><input type ='button' onclick='history.back()' value='回到上一頁'></td>";
}
else{
echo'不正常操作';
return;
echo"<td><input type ='button' onclick='history.back()' value='回到上一頁'></td>";
}
mysqli_query($conn,$sql) or die('sql執行錯誤');
echo $msg;
}
mysqli_close($conn);
?>
請問要怎麼改才能按下結帳,列印出其中一個訂單的商品名稱,杯數,總杯數?