Codeigniter 要在我的页面中显示db值。添加和查看应该在一个页面中完成
这是我的控制器。unit_list是我编写的函数,用于从db获取所有数据并在我的查看页面中查看这些数据Codeigniter 要在我的页面中显示db值。添加和查看应该在一个页面中完成,codeigniter,Codeigniter,这是我的控制器。unit_list是我编写的函数,用于从db获取所有数据并在我的查看页面中查看这些数据 public function unit_list() { //Redirect in case of session out if (!$this->session->userdata ( 'user_id' )) { redirect ( $this->config->base_url (), 'refresh' ); r
public function unit_list()
{
//Redirect in case of session out
if (!$this->session->userdata ( 'user_id' )) {
redirect ( $this->config->base_url (), 'refresh' );
return false;
}
$this->load->model('Unit_model');
$arrWhere = array("vchr_unit_status"=>"A");
$data ["unit"]=$this->Unit_model->get_all($arrWhere);
$this->load->view('includes/header');
$this->load->view('includes/left_menu');
$this->load->view('unit/manage',$data);
$this->load->view('includes/footer');
}
这是我的模型
class Unit_model extends CI_Model{
protected $strTableName = 'suc_unit';
function __construct(){
parent::__construct ();
$CI = &get_instance();
$this->db->from($this->strTableName);
}
/**
*
*/
function get_all($arrWhere)
{
$this->db->where($arrWhere);
$q1 = $this->db->get();
$result = $q1->result_array();
return $result;
}
这是我的查看页面。请帮助我查看查看页面中的所有数据。我是codeigniter的新手,因此我不知道如何使用get_all功能查看我页面中的数据。codeigniter允许您通过几种方式将数据传递到视图。首先,如果您只需使用view方法的第二个参数,那么您的数据将作为$unit在视图中可用 在您的代码中,似乎您正在这样做。这意味着在unit/manage.php视图中,查询结果应为$unit 不过有几件事。加载模型时,对该模型的调用是小写的
<table id="tblListOfUnits" class="table table-bordered table-striped">
<tr>
<th style="width: 10px">Sl No</th>
<th>Unit Name</th>
<th>Unit Symbol</th>
</tr>
<?php
if(!isset($unit)){
$count = 1;
foreach ($unit as $key => $units) {
?>
<tr id="tr_<?php echo $units->pk_int_unit_id; ?>">
<td id="tdUntSlNo_<?php echo isset($units) ? $units['int_no_decimal']: ' '; ?>><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $count; ?>.</a></td>
<td id="tdUntNme_<?php echo $units->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_name; ?></a></td>
<td id="tdUntSymbl_<?php echo $units->pk_int_unit_id; ?>"><a href="javascript:unitEdit(<?php echo $unit->pk_int_unit_id; ?>)" ><?php echo $unit->vchr_unit_symbol; ?></a></td>
</tr>
<?php
$count ++;
}
}?>
</table>
这可能无关紧要,但这条线应该是:
$this->unit_model->get_all( $arrWhere );
$units->pk_int_unit_id
我想指出的是,在模型的方法中,假设总是会有结果,这是不好的。在尝试返回结果之前,您需要确保有结果。此外,还需要提供表名以获取:
$data["unit"] = $this->unit_model->get_all( $arrWhere );
当然,您正在尝试在模型的构造函数中设置表,但这只适用于对数据库的第一个查询。这是不对的,您应该避免这种情况,因为通常不会仅为单个查询创建模型
function get_all( $arrWhere )
{
// Make sure $arrWhere is an array
if( ! is_array( $arrWhere ) )
return FALSE;
$this->db->where( $arrWhere );
// Make sure you set the table name!
$q1 = $this->db->get( $this->strTableName );
// Make sure there is a result set!
if( $q1->num_rows() > 0 )
return $q1->result_array();
return FALSE; // or NULL, or array(), or whatever you want.
}
现在在你看来,你有一个问题$将始终设置单位,因此执行此操作时:
function __construct()
{
parent::__construct();
// If you are in a CodeIgniter model, you don't need this line
// $CI = &get_instance();
// This is bad. Don't do this ever.
// $this->db->from($this->strTableName);
}
你永远不会输入那块代码。你应该在那里做的是:
if( ! isset( $unit ) ) ...
在foreach循环中,此代码将抛出一个错误:
if( ! empty( $unit ) ) ...
这是因为您正在从db调用返回一个数组数组,所以它应该是:
$this->unit_model->get_all( $arrWhere );
$units->pk_int_unit_id
还有其他实例,您试图使用一个实际上是数组的对象。基本上,任何有$units的地方->都应该是$units['…']
如果您想使用对象而不是数组,请在内部获取您应该使用的所有内容:
$units['pk_int_unit_id']
因为result_array是数组的数组,而result是对象的数组
无论如何,希望这能有所帮助。仍然存在同样的问题。你应该做几件事。验证查询结果集是否实际包含行。我的意思是调试get_all返回的值。您可以使用var_dump,甚至更好,您可以使用ChromePHP并登录到浏览器的控制台。其次,如果你编辑你的问题来显示你修改过的代码,它可能会帮助我看到你的问题在哪里。尝试改进代码缩进,使其非常清晰。