简体   繁体   English

Codeigniter将数据变量从控制器传递到视图

[英]Codeigniter pass data variables from controller to view

I am using codeigniter framework and on one of the controllers I have tis code below 我正在使用codeigniter框架,并且在其中一个控制器上有以下tis代码

public function index() {
    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

My problem is when I try to use the data users and subviews into a view. 我的问题是当我尝试在视图中使用数据用户视图时。 As you can see "subviews" has a location of another view file, and when I call "subviews" in the main view like this: 如您所见,“子视图”具有另一个视图文件的位置,当我在主视图中调用“子视图”时,如下所示:

<div class="span9">
<?php $this->load->view($subview); ?>
</div>

I get this error: 我收到此错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: subview
Filename: admin/layout_principal.php
Line Number: 19
An Error Was Encountered
Unable to load the requested file: .php*

And I said "well, its just a location I'll put it directly, and go on loading my users data, but then when I use "users" from the data like this: 我说:“好吧,它只是一个位置,我将其直接放置,然后继续加载我的用户数据,但是当我从数据中使用“用户”时,就像这样:

<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/' . $user->id, $user->email);?></td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

I get this error: 我收到此错误:

A PHP Error was encountered
Severity: Notice
Message: Undefined variable: users
Filename: usuario/index.php
Line Number: 15

The view it's not recognizing the data variables, I had searched and tried for ways to solve this but I still with the same problem, and well, I posted this for help as last resource, can you help me? 鉴于它无法识别数据变量,我已经搜索并尝试了解决此问题的方法,但我仍然遇到相同的问题,好吧,我将其发布为最后一个资源来寻求帮助,您能帮我吗? what can I do to make the view recognize the data, because I can't find what's wrong with this. 我该怎么做才能使视图识别数据,因为我找不到这有什么问题。

I can not see that you have loaded the user model 我看不到您已经加载了用户模型

$this->load->model('user_m');

Controller 控制者

public function index() {
    $this->load->model('user_m');

    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

Also you need to load view file sub view like so 另外,您需要像这样加载视图文件子视图

public function index() {
    $this->load->helper('url'); // Should autoload it.
    $this->load->model('user_m');

    $data['users'] = $this->user_m->get();
    $data['subview'] = $this->load->view('sub_view');

    //if any data in sub view
    //$data['subview'] = $this->load->view('sub_view', $data);

    $this->load->view('admin/layout_principal', $data);
}

If you need to load controller into controller you need HMVC 如果需要将控制器加载到控制器中,则需要HMVC

I also have not heard of <?php echo btn_delete();?> and <?php echo btn_edit();?> http://www.codeigniter.com/userguide2/helpers/url_helper.html 我也没有听说过<?php echo btn_delete();?><?php echo btn_edit();?> http://www.codeigniter.com/userguide2/helpers/url_helper.html

View Try 查看尝试

<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar' .'/'. $user['id']); ?></td>
<td><?php echo anchor('admin/usuario/borrar' .'/'. $user['id']); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

in your controller function 在您的控制器功能中

public function index() {
    $this->data['users'] = $this->user_m->get();
    $this->data['subview'] = 'admin/usuarios/index';
    $this->load->view('admin/layout_principal', $this->data);
}

in your view file: 在您的视图文件中:

<div class="span9">
<?php 
 if(isset($subview))
foreach($subview as $subvi)
{
  echo $subvi;
}
 ?>
</div>

for users data in your file as given below: 为用户提供文件中的数据,如下所示:

<?php 
if(count($users)!=0) (OR) if(isset($users))
foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/'.$user->id, $user->email);?>    </td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

Try to use below code in controller(i think you must have loaded the "user_m" model in function __construct(){} ) : 尝试在控制器中使用以下代码(我认为您必须已在function __construct(){}加载了“ user_m”模型):

public function index() {
    $data = array();
    $data['users'] = $this->user_m->get();

    $data['subview'] = $this->load->view('admin/usuarios/index', $data, true);
    $this->load->view('admin/layout_principal', $data);
}

And then in view('admin/layout_principal'): 然后在视图中('admin / layout_principal'):

<div class="span9">
<?php echo $subview; ?>
</div>


<?php if(count($users)): foreach($users as $user):?>        
<tr>
<td><?php echo anchor('admin/usuario/editar/' . $user->id, $user->email);?></td>
<td><?php echo btn_edit('admin/usuario/editar', $user->id); ?></td>
<td><?php echo btn_delete('admin/usuario/borrar', $user->id); ?></td>
</tr>
<?php endforeach; ?>
<?php else: ?>
<tr>
<td colspan="3">No users found</td>
</tr>
<?php endif;?>

But try to do all looping and view loading in controller itself... I can explain how ifyou want me to... because view is only for html and php variables.. it should not contain code for loop and load view... but this must part of controller code.. and load in a variable and just echo the variable in the view... must take care of this.. for maintain standard MVC ... 但是,请尝试执行所有循环并在控制器本身中加载视图...我可以解释您是否希望......因为视图仅适用于html和php变量..它不应包含用于循环和加载视图的代码...但这必须是控制器代码的一部分..并加载一个变量,然后在视图中回显该变量...必须注意这一点..以便维持标准MVC ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM