CodeIgniter 表类:从生成的单元添加链接
我正在使用表类,该类根据从数据库中提取的数据数组自动生成一个表。
模型:
function get_reports_by_user_id($userid)
{
return $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
}
控制器:
function index()
{
echo $this->table->generate($this->mymodel->get_reports_by_user_id('1234'));
}
当我让它工作时,控制器最终将被移动到视图。 这会很好地生成表格,但我想添加指向字段的链接。 例如,id
列允许我链接到仅包含该报告 ID 的数据页面。 我知道我可以手动以老式方式输出表格。 然后我可以添加我想要的任何链接,但我希望能够尽可能地使用自动生成。 必须有一种方法来完成像链接表格单元格这样常见的事情。 有人有什么想法吗?
编辑:
用户Java PHP大部分都在下面。 这是使其工作的代码:
function get_reports_by_user_id($userid)
{
$rows = $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
foreach ($rows as $count => $row)
{
$rows[$count]['id'] = anchor('report/'.$row['id'],$row['id']);
}
return $rows;
}
我只需要用锚文本版本替换原始数组中的值。
I'm using the table class that auto-generates a table for me from an array of data pulled from my database.
Model:
function get_reports_by_user_id($userid)
{
return $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
}
Controller:
function index()
{
echo $this->table->generate($this->mymodel->get_reports_by_user_id('1234'));
}
The controller will eventually be moved to a view when I have it working. This generates the table just fine, but I'd like to add a link to a field. For example, the id
column that would allow me to link to a page of data for just that report's id. I know I can just output the table the old fashioned way by hand. I can then add whatever links I want, but I'd love to be able to use the auto-generation as much as possible. There's got to be a way to do something as common as linking a table cell. Does anyone have any ideas?
EDIT:
User Java PHP has it mostly right below. Here's the code that makes it work:
function get_reports_by_user_id($userid)
{
$rows = $this->db->get_where('ss2_report',array('userid' => $userid))->result_array();
foreach ($rows as $count => $row)
{
$rows[$count]['id'] = anchor('report/'.$row['id'],$row['id']);
}
return $rows;
}
I just needed to replace the value in the original array with the anchor text version.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
唯一的方法是,在函数
get_reports_by_user_id()
中,您将循环遍历所有结果并将标记添加到 ids 中。 像这样的事情:
我不使用 CodeIgniter 的数据库库,所以我不确定它返回
$rows
的格式,但上面的代码应该让您大致了解需要做什么。The only way is, in the function
get_reports_by_user_id()
, you would loop through all the results and add the<a href>
tag to the ids. Something like this:I don't use CodeIgniter's database library so I'm not sure of what format it returns
$rows
in, but the code above should give you the general idea of what you need to do.一个想法可能是做类似的事情..
One idea might be to do something like..