亚洲国产日韩欧美一区二区三区,精品亚洲国产成人av在线,国产99视频精品免视看7,99国产精品久久久久久久成人热,欧美日韩亚洲国产综合乱

PHP develops simple shopping cart function for product display

First, make a simple homepage, query several products from the database, display them on the homepage, and then add a purchase button

1602.png

This is mainly for the database Operation, the SQL statement SELECT queries the product information in the database table, saves it to the goods array, and displays it in a loop on the product page

Obtain product information from the database and store it in the $goods two-dimensional array --> Remove the product Information is displayed on the page and purchasing functionality is added.

The name of the homepage is list.php

<?php

$goods = array();
//從數(shù)據(jù)庫獲取商品信息存入$goods二維數(shù)組
$i = 0;
//這里請換上自己的數(shù)據(jù)庫相關(guān)信息
$conn = mysqli_connect('localhost','username','password','test');
mysqli_set_charset($conn,"utf8");
$res = mysqli_query($conn,'select * from good');

//這里把商品信息放到$goods二維數(shù)組,每一維存的是單個
//商品的信息,比如商品稱、價格。
while ($row = mysqli_fetch_assoc($res)) {
  $goods[$i]['id'] = $row['id'];
  $goods[$i]['name'] = $row['name'];
  $goods[$i]['price'] = $row['price'];
  $i++ ;
}

?>
<!DOCTYPE html>
<html>
<head>
  <meta http-equiv="Content-Type" content="text/html;charset=utf-8">
  <title>PHP商品展示</title>
</head>
<body>
<?php
//取出商品信息顯示在頁面上,并添加購買功能
foreach ($goods as $value) {
  echo ' 商品名: '. $value['name'] . " " . ' 價格: ' . $value['price'] . " " ;
  echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購買</a>";
  echo '<br /><br />';
}
?>
<a href="cart.php">查看您的購物車</a>
</body>
</html>

Note:

buy.php is the php page for purchasing goods, cart.php is the shopping cart page, and the following Will explain in detail.

Continuing Learning
||
<?php $goods = array(); //從數(shù)據(jù)庫獲取商品信息存入$goods二維數(shù)組 $i = 0; //這里請換上自己的數(shù)據(jù)庫相關(guān)信息 $conn = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($conn,"utf8"); $res = mysqli_query($conn,'select * from good'); //這里把商品信息放到$goods二維數(shù)組,每一維存的是單個 //商品的信息,比如商品稱、價格。 while ($row = mysqli_fetch_assoc($res)) { $goods[$i]['id'] = $row['id']; $goods[$i]['name'] = $row['name']; $goods[$i]['price'] = $row['price']; $i++ ; } ?> <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" content="text/html;charset=utf-8"> <title>PHP商品展示</title> </head> <body> <?php //取出商品信息顯示在頁面上,并添加購買功能 foreach ($goods as $value) { echo ' 商品名: '. $value['name'] . " " . ' 價格: ' . $value['price'] . " " ; echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購買</a>"; echo '<br /><br />'; } ?> <a href="cart.php">查看您的購物車</a> </body> </html>
submitReset Code