国产av日韩一区二区三区精品,成人性爱视频在线观看,国产,欧美,日韩,一区,www.成色av久久成人,2222eeee成人天堂

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

//這里把商品信息放到$goods二維數(shù)組,每一維存的是單個(gè)
//商品的信息,比如商品稱、價(jià)格。
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
//取出商品信息顯示在頁(yè)面上,并添加購(gòu)買(mǎi)功能
foreach ($goods as $value) {
  echo ' 商品名: '. $value['name'] . " " . ' 價(jià)格: ' . $value['price'] . " " ;
  echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購(gòu)買(mǎi)</a>";
  echo '<br /><br />';
}
?>
<a href="cart.php">查看您的購(gòu)物車(chē)</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ù)庫(kù)獲取商品信息存入$goods二維數(shù)組 $i = 0; //這里請(qǐng)換上自己的數(shù)據(jù)庫(kù)相關(guān)信息 $conn = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($conn,"utf8"); $res = mysqli_query($conn,'select * from good'); //這里把商品信息放到$goods二維數(shù)組,每一維存的是單個(gè) //商品的信息,比如商品稱、價(jià)格。 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 //取出商品信息顯示在頁(yè)面上,并添加購(gòu)買(mǎi)功能 foreach ($goods as $value) { echo ' 商品名: '. $value['name'] . " " . ' 價(jià)格: ' . $value['price'] . " " ; echo "<a href=buy.php?name=" . $value['name'] . '&price=' . $value['price'] .">購(gòu)買(mǎi)</a>"; echo '<br /><br />'; } ?> <a href="cart.php">查看您的購(gòu)物車(chē)</a> </body> </html>
submitReset Code