First, make a simple homepage, query several products from the database, display them on the homepage, and then add a purchase button
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(); //从数据库获取商品信息存入$goods二维数组 $i = 0; //这里请换上自己的数据库相关信息 $conn = mysqli_connect('localhost','username','password','test'); mysqli_set_charset($conn,"utf8"); $res = mysqli_query($conn,'select * from good'); //这里把商品信息放到$goods二维数组,每一维存的是单个 //商品的信息,比如商品称、价格。 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.
Next Section