Foreach中的未定义属性 [英] Undefined property in Foreach
问题描述
我要登录到系统,登录后的想法是根据用户个人资料的类型来更新菜单,为此我正在数据库中进行操作,并通过Foreach显示菜单。但是在Foreach中,当将行中的一个与一个数字进行比较时,即与一个ID比较时,我会收到此错误消息和其他类似的消息:
I'm logging into my system and once I login, the idea is that the menu is updated according to the type of user profile, for which I'm doing from the database and showing the menu through a Foreach. However within the Foreach, when comparing one of the rows with a number, that is, with an id I get this error message and other similar ones:
Message: Undefined property: mysqli::$id_man
所以我认为我是m无法识别该行的值,为什么会发生,我该如何纠正呢?
So I think that I'm not recognizing the value of the row, why is it happening, how could I correct it?
Controller
Controller
public function ingresar(){
$nombre_usu= $this->input->post('nombre_usu');
$pass_usu= $this->input->post('pass_usu');
$res= $this->M_Login->ingresar($nombre_usu,$pass_usu);
if ($res ==1) {
$this->load->view('layouts/Header.php');
// if the user exists, show the menu according to his profile
$data['menu_barra'] = $this->M_Login->show_menu();
$this->load->view('layouts/Menu.php', $data);
$this->load->view('usuarios/V_Index.php');
$this->load->view('layouts/Footer.php');
} else {
$data['mensaje'] = "Usuario o contraseña Incorrecta";
$this->load->view('V_Login',$data);
}
}
模型
public function show_menu(){
$id_tip= $this->session->userdata('tipo');
$this->db->select('id_mc, id_tip, id_man');
$this->db->from('mantenedores_cuenta');
$this->db->where('id_tip',$id_tip);
$menu = $this->db->get();
$horarios ="";
$informes="";
foreach ($menu as $row) {
if ($row->id_man == 1 ) {
$horarios .='<li class="active"><a href="<?= base_url("index.php/C_Horarios"); ?>"><i class="fa fa-circle-o"></i>Administrar Horarios</a></li>';
}
if ($row->id_man == 2 ) {
$horarios .='<li><a href="<?= base_url("index.php/C_Calendar"); ?>"><i class="fa fa-circle-o"></i>Agendar Citas</a></li>';
}
if ($row->id_man == 3 ) {
$horarios .='<li><a href="<?= base_url("index.php/C_Citas"); ?>"><i class="fa fa-circle-o"></i>Consultar Citas</a></li>';
}
if ($row->id_man == 4 ) {
$informes .='<li class="active"><a href="<?= base_url("index.php/C_Porcentaje"); ?>"><i class="fa fa-circle-o"></i>Porcentaje de Citas</a></li>';
}
} //Fin del Foreach
$menu_barra="
<ul class='sidebar-menu'>
<li class='header'>MENU</li>
<li class='active treeview'>
<a href='#'>
<i class='fa fa-fw fa-calendar'></i> <span>Horarios</span>
<span class='pull-right-container'>
<i class='fa fa-angle-left pull-right'></i>
</span>
</a>
<ul class='treeview-menu'>
'$horarios'
</ul>
</li>
<li class='treeview'>
<a href='#'>
<span class='glyphicon glyphicon-stats'></span><span>Informes</span> <i class='fa fa-angle-left pull-right'></i>
</a>
<ul class='treeview-menu'>
'$informes'
</ul>
</li>
<li><a href='documentation/index.html'><i class='fa fa-book'></i> <span>Documentación</span></a></li>
<li><a href='documentation/index.html'><i class='fa fa-fw fa-sign-out'></i><span>Salir</span></a></li>
</ul> ";
return ($menu_barra);
}
视图(菜单)
<aside class="main-sidebar">
<section class="sidebar">
<div class="user-panel">
<div class="pull-left image">
<img src="<?php echo base_url();?>assets/dist/img/user2-160x160.jpg" class="img-circle" alt="User Image">
</div>
<div class="pull-left info">
<p><?php echo $this->session->userdata('s_usuario');?></p>
<a href="#"><i class="fa fa-circle text-success"></i><?php echo $this->session->userdata('cuenta');?></a>
</div>
</div>
// show the menu according to your profile
<?php echo $menu_barra; ?>
</section>
<!-- /.sidebar -->
我也试图以这种方式显示它,但是不允许我进入数组,或者至少那不是消息所说的,例如:$ row [ id_man] == 3
I also tried to show it in this way, but I was not allowed in an array or at least that was what the message said, example: $row["id_man"] == 3
推荐答案
更改
foreach ($menu as $row) {
to
foreach ($menu->result() as $row) {
有关文档,请参见此处
您还可以使用
foreach ($menu->result_array() as $row) {
并访问
$row['id_man'];
这篇关于Foreach中的未定义属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!