当前位置: 首页 > 知识库问答 >
问题:

Codeigniter中foreach循环中的未定义变量

颜祖鹤
2023-03-14

我是codeigniter的初学者,我正在制作一个用于学习的基本积垢。我在索引中遇到了一个错误。php

<h2><?=$title?></h2>

<?php foreach($posts as $post)?>
<h3><?php echo $post['title'];?></h3>o
<small class="post-date">Posted on:<?php echo $post['created at'];?>         </small><br>
<?php echo word_limiter($post['body'],60);?>
<p><a class="btn btn-default" href="<?php echo site_url('/posts/' .$post['slug']);?>">read more</a></p>
<?php endforeach;?>

遇到PHP错误严重性:注意:消息:未定义变量:posts
文件名:posts/index。php
行号:2
回溯:
文件:/opt/lampp/htdocs/codeigniter/application/views/posts/index。php
行:2
函数:\u错误\u处理程序

文件:/opt/lampp/htdocs/codeigniter/application/controllers/Posts。php
行:23
函数:视图

文件: /opt/lampp/htdocs/codeigniter/index.php
行:315
函数:require_once

我的控制器是

<?php
class Posts extends CI_Controller {

public function index(){
    $data['title']='Latest Posts';
    $data['posts']=$this->Post_model->get_posts();

    $this->load->view('templates/header');
    $this->load->view('posts/index',$data);
    $this->load->view('templates/footer');
}

public function view($slug=NULL)
{
    $data['post'] = $this->Post_model->get_posts($slug);

    if(empty($data['post'])){
        show_404();
    }

    $data['title']=$data['post']['title'];
    $this->load->view('templates/header');
    $this->load->view('posts/view', $data);
    $this->load->view('templates/footer');
}

public function create(){
    $data['title'] = 'create post';

    $this->form_validation->set_rules('title','Title','required');
    $this->form_validation->set_rules('body','body','required');

    if($this->form_validation->run() === false) {

        $this->load->view('templates/header');
        $this->load->view('posts/create',$data);
        $this->load->view('templates/footer');

    } else {

    $this->Post_model->create_post();

    redirect('posts');

    }

}

public function delete($id) {
    $this->Post_model->delete_post($id);
    redirect('posts');
}

public function edit($id)  {

    $data['post'] = $this->Post_model->get_posts($slug);

    if(empty($data['post'])){
        show_404();
    }

    $data['title'] = 'edit post';

    $this->load->view('templates/header');
    $this->load->view('posts/index',$data);
    $this->load->view('templates/footer');
}

public function update()
{
    $this->Post_model->update_post();
    redirect('posts');
}

}

我的模型文件是

<?php

class Post_model extends CI_Model{

    public function __construct(){
        $this->load->database();
    }

    public function get_posts($slug = false){

        if($slug === false) {
            $this->db->order_by('id','DESC');

            $query=$this->db->get('posts');
            return $query->result_array();

        }

        $query = $this->db->get_where('posts',array('slug' => $slug));

        return $query->row_array();
    }

    public function create_post(){
        $slug=url_title($this->input->post('title'));

        $data=array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body')
        );

        return $this->db->insert('posts',$data);
    }

    public function delete_post($id){
        $this->db->where('id',$id);
        $this->db->delete('posts');
        return true;
    }

    public function update_post() {

        $slug=url_title($this->input->post('title'));

        $data=array(
            'title' => $this->input->post('title'),
            'slug' => $slug,
            'body' => $this->input->post('body')
        );  

        $this->db->where('id',$this->input->post('id'));
        return $this->db->update('posts',$data);

    }

}

共有1个答案

柳俊健
2023-03-14

在控制器上的编辑功能中,您有edit($id)

但是,在加载模型的地方,您有get\u posts($slug)它应该是get\u posts($id)

并将$data['post']更改为$data['post']

public function edit($id)  {

    $data['posts'] = array();

    // rename slug to id
    $data['posts'] = $this->post_model->get_posts($id);

    if(empty($data['posts'])){
        show_404();
    }

    $data['title'] = 'edit post';

    $this->load->view('templates/header');

    /*
      Tip You don't have to use index.php on for view you could create a
      another view instead $this->load->view('posts/edit_view',$data);
    */

    $this->load->view('posts/index',$data); 
    $this->load->view('templates/footer');
}

关于控制器的when-load模型

改变

$this->Post_model->get_posts($id);

$this->post_model->get_posts($id);

https://www.codeigniter.com/user_guide/general/models.html#loading-a型

在utoload.php

改变

$autoload['model'] = array('Post_model');

$autoload['model'] = array('post_model');

并在自动加载中加载数据库。php让生活更轻松

$autoload['libraries'] = array('database');
 类似资料:
  • 我尝试将一个循环转换为一个循环在flutter中等待循环。我想要循环的元素是Firebase实时数据库中的快照。 我的函数如下所示: 我尝试了不同的方法,但是我没有让函数工作。 第一次尝试: 错误: 未处理的异常:键入'_InternalLinkedHashMap 第二次尝试: 错误: 未处理的异常:键入'_InternalLinkedHashMap 在所有的尝试中,我都得到了相同的错误。但是上述

  • 我想为数组中的每个项目运行一个函数。数组每次都不同,但为此我将只使用一个示例数组。它一直在说未定义的不是一个函数,我假设未定义的是未来的函数。解决这个问题的办法是什么?

  • 问题内容: 我本来打算通过 在。有几点我不了解。 迭代器函数的用途是什么?没有它,还有什么办法吗? 如下所示,键和值的意义是什么? PS: 我试图在不带参数的情况下运行此函数,但它不起作用。 这是我的: 我的档案: 另一个问题 :如果在控制台中出现条件并显示“ username is thomas”,为什么上面的功能没有打开? 问题答案: 问题1和2 因此,基本上,第一个参数是要迭代的对象。它可以

  • 值5=5 值6=6 值7=7 结果lopping1=6 结果lopping2=15 结果lopping3=24 我该怎么办?

  • foreach循环遍历列表值并将控制变量(var)依次设置为列表的每个元素 - 语法 (Syntax) Perl编程语言中foreach循环的语法是 - foreach var (list) { ... } 流程图 (Flow Diagram) 例子 (Example) #!/usr/local/bin/perl @list = (2, 20, 30, 40, 50); # foreach lo

  • 我正在迭代Java 7循环和Java 8 循环中的列表。Java 8 循环希望其中的变量不会更改。例如: 有人能解释一下为什么吗?是Java 8的弊端吗?