如何将数据库加载到帮助程序并在具有特定列的视图中显示

时间:2014-04-16 10:25:07

标签: codeigniter

footer_helper

<?php

function get_gadget_footer() {

//the database functions can not be called from within the helper
//so we have to explicitly load the functions we need in to an object
//that I will call ci. then we use that to access the regular stuff.
$ci=& get_instance();
$ci->load->database();

//select the required fields from the database
//$ci->db->select('name, type, setting');

//tell the db class the criteria
$ci->db->where('display', 'Footer');

//supply the table name and get the data
$query = $ci->db->get('gadgets');

foreach($query->result() as $row):


    $type['name'] = $row->name;
    $type['type'] = $row->type;
    var_dump($type);
endforeach;


return $type;

}

这是我的帮助页面。 var_dump($ type)以数组形式返回我所需的结果:

的var_dump($型)

 array (size=2)
 'name' => string 'Quick Links' (length=11)
 'type' => string '<a href='www.facebook.com'>Facebook</a><br>
 <a href='#'>Twitter</a><br>
 <a href='#'>Salyani</a><br>
 <a href='#'>B&W</a><br>' (length=123)

 array (size=2)
 'name' => string 'Social Network' (length=14)
 'type' => string '<a href='#'>Facebook</a><br>
 <a href='#'>Facebook</a><br>
 <a href='#'>Facebook</a><br>
 <a href='#'>Facebook</a><br>' (length=115)

现在在我的视图页面中。我希望在div中显示特定记录。喜欢名字记录  记录类型。

从现在开始我做了以下事情:

视图

 <?php
 $this->load->helper('footer_helper'); 
 $name = get_gadget_footer();
 ?>
 <div id="footer">
 <div id="footer_gadget_collection">


  <?php

  foreach ($name as $dat){ 

  ?>

  <?php

  ?>
    <div id="footer_subgadget">
    <div id='title'><?php echo $dat['name']; ?></div>
    <div id='description'><?php $dat['type']; ?> </div>
     </div>
   <?php

  }
  ?>

但是,它给了我错误。请帮帮我。

1 个答案:

答案 0 :(得分:0)

在您的助手中,您正在重置阵列的名称/类型,而不是构建它 应该是这样的;

<?php
//supply the table name and get the data
$query = $ci->db->get('gadgets');
$type = array();
foreach ($query->result() as $row) {
    $type[] = array('name' => $row->name, 'type' => $row->type);
}

return $type;