用户
 找回密码
 入住 CI 中国社区
搜索
查看: 1950|回复: 4
收起左侧

[视图] 做的CI留言板功能,但是参数传递出现错误,求解

[复制链接]
发表于 2017-7-21 19:34:24 | 显示全部楼层 |阅读模式
这是view层,位于views/leaveword/index.php
PHP复制代码
 
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
?><!DOCTYPE html>
<html lang="en">
    <head>
        <title>leave word</title>
        <meta charset="utf-8">
        <h3>留言板</h3>
    </head>
 
    <body>
        <div id="title" style="height: 10px; weight: 100px"></div>
        <div id="show" style="height: 200px; weight: 100px">
            <?php foreach ($leavewords as $leaveword_lists): ?>
                <h5><?php echo $leaveword_lists['utitle']; ?></h5>
                <h6><?php echo $leaveword_lists['ucontent']; ?></h6>
                <h6><?php echo $leaveword_lists['uname']; ?></h6>
            <?php endforeach; ?>
        </div>
 
        <div id="put in" style="height: 100px; weight: 100px">
            <h4>请留言</h4>
            <br />
            <?php echo validation_error(); ?>
            <?php echo form_open('leaveword/home/put'); ?>
 
                <div id="utitle" style="height: 10px; weight: 80px">
                <label for="utitle">留言标题:</label>
                <input type="input" name="utitle" /><br />
                </div>
               
                <div id="ucontent" style="height: 50px; weight: 80px">
                <label for="ucontent">留言内容</label>
                <textarea name="ucontent"></textarea><br />
                </div>
 
                <div id="uname" style="height: 10px; weight: 80px">
                <label for="uname">用户名:</label>
                <input type="input" name="uname" /><br />
                </div>
               
                <input type="submit" name="submit" value="发表留言">
            </form>
 
        </div>
       
    </body>
</html>
 
复制代码


这是controller层,位于controller/leaveword/home.php
PHP复制代码
 
<?php
 
class Home extends CI_Controller
{
    // 构造函数
    public function __construct()
    {
        parent::__construct();
        $this->load->view('leaveword/index');
        $this->load->model('leaveword');
    }
 
 
    // 显示留言方法
    public function show()
    {
       
        $data['leavewords'] = $this->leaveword->retrieve();
       
        $this->load->view('leaveword/index', $data);
 
 
        if (empty($data['leaveword']))
        {
            echo '暂时还没有人留言';
        }
 
 
    }
 
    // 用户发表留言方法
    public function put()
    {
        $this->load->helper('form');
        $this->load->library('form_validaton');
 
        $this->form_validation->set_rules('utitle', 'Utitle', 'required');
        $this->form_validation->set_rules('ucontent', 'Ucontent', 'required');
        $this->form_validation->set_rules('uname', 'Uname', 'required');
 
        if ($this->form_validation->run() == FALSE)
        {
            echo '留言失败,请重新输入。';
            $this->load->view('leaveword/index');
        }
        else{
            echo '留言成功';
            $this->leaveword->create();
        }
 
    }
   
}
 
复制代码


这是model层,位于model/leaveword.php
PHP复制代码
 
<?php
class Leaveword extends CI_Model
{
    public function __construct()
    {
        parent::__construct();
        $this->load->database();
    }
 
    // 读取留言方法
    public function retrieve()
    {
       $query = $this->db->get('leaveword');
       return $query->result_array();
    }
 
    // 存储留言方法
    public function create()
    {
        // 赋值给data数组,post()可以对数据进行过滤
        $data = array(
            'utitle' => $this->input->post('utitle'),
            'ucontent' => $this->input->post('ucontent'),
            'uname' => $this->input->post('uname')
        );
 
        return $this->db->insert('leaveword', $data);
    }
}
 
复制代码


数据库就用户名、留言标题和留言内容,三个。求大神帮我看看,小白实在没法子啦
发表于 2017-7-22 02:08:57 | 显示全部楼层
具体是什么错误?
 楼主| 发表于 2017-7-22 08:17:43 | 显示全部楼层
本帖最后由 五指山大王 于 2017-7-22 08:32 编辑
Hex 发表于 2017-7-22 02:08
具体是什么错误?

Message: Undefined variable: leavewords
Filename: leaveword/index.php

 楼主| 发表于 2017-7-22 08:25:12 | 显示全部楼层
本帖最后由 五指山大王 于 2017-7-22 08:32 编辑
Hex 发表于 2017-7-22 02:08
具体是什么错误?

数据能存进数据库,但是显示就出问题了
发表于 2017-7-22 10:14:10 | 显示全部楼层
变量未定义
<?php if (isset($leavewords)) { foreach ($leavewords as $leaveword_lists): ?>
     <h5><?php echo $leaveword_lists['utitle']; ?></h5>
                <h6><?php echo $leaveword_lists['ucontent']; ?></h6>
                <h6><?php echo $leaveword_lists['uname']; ?></h6>
            <?php endforeach;  } ?>

本版积分规则