本文實例講述了微信小程序實現用table顯示數據庫反饋的多條數據功能。分享給大家供大家參考,具體如下:
解決了微信小程序自定義表格,并顯示的多條數據的問題。
index.wxml
<view> <text>我的調查問卷</text> <scroll-view scroll-x="true" style=" white-space: nowrap; display: flex"> <view class="table"> <view class="tr bg-header"> <view class="th">姓名</view> <view class="th">性別</view> <view class="th">年齡</view> </view> <view class="tr bg-items" wx:for = "{{items}}" wx:key=""> <text class="td">{{item.name}}</text> <text class="td">{{item.gender}}</text> <text class="td">{{item.age}}</text> </view> </view> </scroll-view> <button bindtap="change">查看我的數據</button></view>
index.js
Page({ /** * 頁面的初始數據 */ data: { items:[]//定義變長數組 }, /** * 生命周期函數--監聽頁面加載 */ onLoad: function () { }, /** * 生命周期函數--監聽頁面顯示 */ onShow: function () { }, /** * 生命周期函數--監聽頁面隱藏 */ onHide: function () { }, change:function(e){ var that=this; wx.request({ url: 'https://../data.php',//自己的服務器地址 header: { "Content-Type": "application/json" }, method: "POST", success: function (res) { for (var i = 0, len = res.data.length; i < len; i++){ that.data.items[i] = res.data[i]; } that.setData({ items: that.data.items }) }, fail: function (res) { wx.showToast({ 'title': 'request fail' }) } }) }})
data.php
<?php$servername = "localhost";$username = "root";$password = "***";//數據庫連接密碼$dbname = "mydb";// 創建連接$conn = new mysqli($servername, $username, $password, $dbname);// 檢測連接if ($conn->connect_error) { die("connect server fail: " . $conn->connect_error);}$query = "select * from table";$result = mysqli_query($conn,$query);$data = array();while ($rows = mysqli_fetch_assoc($result)){$data[] = $rows;}echo json_encode($data);$conn->close();?>
文章有借鑒,并非全部自創
希望本文所述對大家微信小程序開發有所幫助。
新聞熱點
疑難解答