我们如何将我的数据从控制器发送到视图,以及我们如何检测我的功能中的错误


How we send my data from controller to view and how we detect an error in my function

我们如何将数据从控制器发送到视图,以及我们如何检测函数中的错误。

public function ratings() { 
    $results['value']= $this->Home_registeration->get_ratings($_POST);
    $this->load->view ('display_search_result', $results); 
    Echo "";
    print_R ($results)
}

我提供了一个示例代码,介绍如何将数据从控制器发送到视图:您必须在控制器功能中编写以下代码

$data['title']='ABC';
$data['page']='home';
$this->load->view('home',$data);

现在在主视图文件中编写以下代码:

echo "The title is => ".$title;  //The title is => ABC
echo "The page is => ".$page;    //The page is => home

在您的情况下,只需打开视图文件,即(display_search_result.php)并写入:

print_r($value);

首先,您不应该在codeigniter中使用$_POST,而应该使用$this->input->post()库。通过传递视图中的第二个参数并循环数组值,或者使用类vars:,可以将数据发布到视图中

<?php
$data = [
    "id" => 234,
    "name" => "John Smith",
    "status" => 2
];
$this->data->id = 234;
$this->data->name = "John Smith";
$this->data->status = 2;
?>

(a) 然后调用您的视图:

<?php
    $this->load->view('viewname', $data);
?>

(b) 或:

<?php
    $this->load->view('viewname');
?>

(a) 然后在您的视图文件中:

<p><?= $id ?></p>
<p><?= $name ?></p>
<p><?= $status ?></p>

(b) 或者,如果您使用$this->data->id

<p><?= $this->data->id ?></p>
<p><?= $this->data->name ?></p>
<p><?= $this->data->status ?></p>

希望能有所帮助。

在调用要处理的函数之前,您需要检查所有需求和所有可能的失败。

示例:

public function ratings() { 
    $parameter = $this->input->post(null, TRUE); // null to get all index and TRUE to xss clean
    $results = array();
    if (empty($parameter))
    {
        $results['value'] = "Please input your parameter first";
    }
    else
    {
        // Asume parameter exist and safe enough to be proccess
        $results['value']= $this->Home_registeration->get_ratings($parameter);
        // you can also check result from get_ratings function
        // asume you will set rating 0 on empty return value from function
        if (empty($results['value'])) $results['value'] = 0;
    }

    $this->load->view ('display_search_result', $results); 
    echo "<pre>";
    print_r ($results);        
    echo "</pre>";
}