• 以php为后端,vue为前端的租房微信小程序


    租房微信小程序是一个非常有用的应用,它不仅可以帮助人们快速找到心仪的房屋,还可以提供便捷的房屋租赁服务。本文将介绍如何使用PHP作为后端语言和Vue作为前端框架来开发一个租房微信小程序。

    1. 环境搭建

    首先,需要在本地或云上安装并配置PHP和Vue环境。可以使用XAMPP、WAMP、MAMP等集成的开发环境,也可以手动安装和配置PHP环境。Vue则需要使用Node.js提供的npm包管理器进行安装和配置。

    1. 创建数据库

    在PHPMyAdmin或其他数据库管理工具中创建一个名为"rental"的数据库,并创建一个名为"house"的表,用于存储房屋信息。表结构如下:

    | id | title | price | area | address | image | description |

    1. 编写后端代码

    使用PHP编写后端代码,建立与数据库连接,并提供RESTful API。代码示例:

    
        header("Access-Control-Allow-Origin: *");
        header("Content-Type: application/json; charset=UTF-8");
    
        $servername = "localhost";
        $username = "root";
        $password = "";
        $dbname = "rental";
    
        $conn = new mysqli($servername, $username, $password, $dbname);
    
        if ($conn->connect_error) {
            die("Connection failed: " . $conn->connect_error);
        }
    
        $method = $_SERVER['REQUEST_METHOD'];
    
        if($method == "GET") {
    
            $sql = "SELECT * FROM house";
            $result = $conn->query($sql);
    
            $rows = array();
    
            if ($result->num_rows > 0) {
                while($row = $result->fetch_assoc()) {
                    $rows[] = $row;
                }
            }
    
            echo json_encode($rows);
    
        } else if($method == "POST") {
    
            $data = json_decode(file_get_contents("php://input"));
    
            $title = $data->title;
            $price = $data->price;
            $area = $data->area;
            $address = $data->address;
            $image = $data->image;
            $description = $data->description;
    
            $sql = "INSERT INTO house (title, price, area, address, image, description)
            VALUES ('$title', '$price', '$area', '$address', '$image', '$description')";
    
            if ($conn->query($sql) === TRUE) {
                echo "New record created successfully";
            } else {
                echo "Error: " . $sql . "
    "
    . $conn->error; } } $conn->close(); ?>
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    1. 编写前端代码

    使用Vue编写前端代码,实现小程序界面和数据交互。代码示例:

    <template>
      <div class="house-list">
        <div class="house" v-for="house in houses" :key="house.id">
          <div class="image">
            <img :src="house.image" alt="house">
          div>
          <div class="info">
            <h3>{{ house.title }}h3>
            <p>价格:{{ house.price }}元/月p>
            <p>面积:{{ house.area }}平米p>
            <p>地址:{{ house.address }}p>
            <p>描述:{{ house.description }}p>
          div>
        div>
        <div class="add-house">
          <h3>新增房屋信息h3>
          <form @submit.prevent="submit">
            <input type="text" v-model="title" placeholder="请输入标题">
            <input type="number" v-model="price" placeholder="请输入价格">
            <input type="number" v-model="area" placeholder="请输入面积">
            <input type="text" v-model="address" placeholder="请输入地址">
            <input type="text" v-model="image" placeholder="请输入图片地址">
            <input type="text" v-model="description" placeholder="请输入描述">
            <button type="submit">提交button>
          form>
        div>
      div>
    template>
    
    <script>
    export default {
      data() {
        return {
          houses: [],
          title: '',
          price: '',
          area: '',
          address: '',
          image: '',
          description: ''
        }
      },
      created() {
        this.getHouses()
      },
      methods: {
        getHouses() {
          fetch('http://localhost/rental/api.php')
            .then(response => response.json())
            .then(data => {
              this.houses = data
            })
        },
        submit() {
          let data = {
            title: this.title,
            price: this.price,
            area: this.area,
            address: this.address,
            image: this.image,
            description: this.description
          }
          fetch('http://localhost/rental/api.php', {
            method: 'POST',
            headers: {
              'Content-Type': 'application/json'
            },
            body: JSON.stringify(data)
          })
            .then(() => {
              this.getHouses()
              this.title = ''
              this.price = ''
              this.area = ''
              this.address = ''
              this.image = ''
              this.description = ''
            })
        }
      }
    }
    script>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    1. 运行小程序

    使用微信小程序开发工具创建一个新的小程序应用,并将前端代码导入。运行小程序,观察效果。

    以上就是使用PHP和Vue开发租房微信小程序的详细过程。开发者可以根据自己的需求和实际情况对代码进行修改和完善。

  • 相关阅读:
    51单片机的hello world之点灯
    Python爬虫——爬取近3个月绵阳市降水量数据源
    利用tensorflow.js在线实现图像要素识别提取
    删除word文档中的空白页
    如何在 Windows 上安装 Docker Desktop
    基于PHP的在线英语学习平台
    抖音矩阵系统,抖音矩阵系统,抖音矩阵系统,抖音矩阵系统,抖音矩阵系统,抖音矩阵系统,抖音矩阵系统,抖音矩阵系统。
    Day42 尚硅谷JUC——Fork_Join分支合并框架
    算法笔记:0-1背包问题
    TBOX开发需求说明
  • 原文地址:https://blog.csdn.net/qq_42154707/article/details/132897344