【问题标题】:How to pass data from an customized helper to a view with codeigniter如何使用 codeigniter 将数据从自定义助手传递到视图
【发布时间】:2015-05-06 08:41:14
【问题描述】:

我创建了一个帮助类来显示来自数据库的值。

助手

if (!function_exists('items')){
function show_items($item)    {


$ci = get_instance();
$sql = " SELECT * FROM table WHERE items = '$item'";
$result = $ci->db->query($sql);
return $result->row();
}

}

在视图中我使用这样的函数:

echo show_items($item)->main_image);

上面的回显显然只显示了main_image的第一行。

我的目标是显示所选 $item 的所有行。

如果我像这样更改助手

$sql = " SELECT * FROM table WHERE items = '$item'";
$result = $ci->db->query($sql);
return $result->result();// first option
return $result->result_array();//second option
}

}

如何使用 for istance a foreach 在视图中循环结果?

【问题讨论】:

  • 我注意到一件事......在你的助手中,你的 function_exists 不应该检查函数 show_items 而不是项目吗?

标签: php codeigniter


【解决方案1】:

你的帮助代码

$query->result_array():

//这是你的视图代码

 <?php $result = show_items($item); ?>// call helper

// DATA RECALL FROM THE HELPER
    <?php foreach($result as $data): ?>
       <?php echo $data['main_image']; ?>
    <?php endforeach; ?>

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-16
    • 1970-01-01
    • 2020-04-18
    • 2014-02-08
    • 1970-01-01
    • 1970-01-01
    • 2011-09-09
    相关资源
    最近更新 更多