Home >Backend Development >PHP Tutorial >PHP and Vue development: How to obtain membership points for different types of goods after payment
PHP and Vue development: How to obtain membership points for different types of goods after payment
Introduction:
In e-commerce platforms, membership points are a kind of A common incentive mechanism is to earn points by purchasing goods or completing specific tasks. However, there may be differences in how points are earned for different types of products. This article will combine PHP and Vue development to introduce how to obtain membership points for different types of goods after payment, and provide specific code examples. We hope that the explanation in this article can help developers better implement this function.
1. Back-end development (PHP):
Product table (goods)
Points table(points)
//Receive parameters passed by the front end
$goodsId = $_POST['goodsId'];
/ / Query product type
$goodsType = queryGoodsType($goodsId);
// Query member points based on product type
$points = queryPointsByType($goodsType);
// Return points information to the front end
echo json_encode(['points' => $points]);
// Method to query product type
function queryGoodsType($goodsId)
{
// 自行实现,例如从数据库中查询商品类型 $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", 'root', ''); $stmt = $pdo->prepare("SELECT type FROM goods WHERE id=?"); $stmt->execute([$goodsId]); $result = $stmt->fetch(PDO::FETCH_ASSOC); return $result['type'];
}
//Method to query points information
function queryPointsByType($goodsType)
{
// 自行实现,例如从数据库中查询对应商品类型的积分信息 $pdo = new PDO("mysql:host=localhost;dbname=mydatabase", 'root', ''); $stmt = $pdo->prepare("SELECT points FROM points WHERE goods_id IN (SELECT id FROM goods WHERE type=?)"); $stmt->execute([$goodsType]); $result = $stmt->fetchAll(PDO::FETCH_ASSOC); return $result;
}
?>
In the above code, first receive the product ID parameter passed by the front end, and then query the product type through the queryGoodsType method. According to the product type, use the queryPointsByType method to query the corresponding member points information, and return the result to the front end in JSON format.
2. Front-end development (Vue):
<h2>支付成功,获取会员积分</h2>
<select v-model="selectedGoods" @change="getPoints">
<option v-for="item in goodsList" :key="item.id" :value="item.id">{{ item.name }}</option>
</select>
<p>会员积分: {{ points }}</p>
<script><br>export default {<br> data() {</script>
return { selectedGoods: '', points: null, goodsList: [ { id: 1, name: '商品A' }, { id: 2, name: '商品B' }, { id: 3, name: '商品C' }, ], };
},
methods: {
getPoints() { // 调用后端接口 axios.post('/api/getPoints.php', { goodsId: this.selectedGoods }) .then((response) => { this.points = response.data.points; }) .catch((error) => { console.error(error); }); },
},
};
In the above code, the data and computed attributes that need to be used are first defined in data, and then v-model is used in the template to bind the value of selectedGoods. When the user selects the product, the @change event will be triggered and the getPoints method will be executed. The getPoints method uses the axios library to send a POST request to the backend and passes the product ID as a parameter to the backend interface. When the backend returns the data, the results are assigned to points and displayed in the template.
Conclusion:
Through the combination of PHP and Vue, we can realize the function of obtaining membership points for different types of products after payment. In back-end development, we designed the appropriate database table structure and wrote the back-end interface code. In front-end development, we use Vue to implement the function of obtaining points after successful payment, and interact with the back-end data through the axios library. We hope that the sample code in this article can help developers realize richer e-commerce functions.
The above is the detailed content of PHP and Vue development: How to obtain membership points for different types of goods after payment. For more information, please follow other related articles on the PHP Chinese website!