[英]Codeigniter 2.1, PHP - combining two functions into one
I have following two function, and I would like to know, is there any way I can combine them into one. 我有以下两个功能,我想知道,有什么方法可以将它们组合为一个。
function galerija_get_all(){
$q = $this->db->get('galerija');
return $q = $q->result_array();
}
function galerija_get_all_slike(){
$q = $this->db->get('slike');
return $q = $q->result_array();
}
And in my view file I have this code: 在我的查看文件中,我有以下代码:
<?php foreach ($galerija as $gal): ?>
<figure>
<?php foreach ($galerija_slike as $img): ?>
<?php if ($gal['id_galerija'] == $img['galerija_id']): ?>
<?php endif ?>
<?php endforeach ?>
<img src="" >
<figcaption></figcaption>
</figure>
<?php endforeach ?>
Is it possible (and if it is, how) this two codes? 这两个代码是否可能(如果可能,如何)?
You could always do this.. 您可以随时这样做。
function galerija_get_all($type){
$q = $this->db->get($type);
return $q->result_array();
}
Then you can use it like so: 然后,您可以像这样使用它:
falerija_get_all('slike');
or 要么
falerija_get_all('galerija');
You can store both results in an array and then return the array as a single result. 您可以将两个结果都存储在数组中,然后将数组作为单个结果返回。
function get_all(){
$galerija = $this->db->get('galerija')->result_array();
$slike = $this->db->get('slike')->result_array();
$result = array('galerija'=> $galerija,
'slike' => $slike);
return $result;
}
Then in the view you can do this 然后在视图中可以执行此操作
<?php foreach ($result['galerija'] as $gal): ?>
<figure>
<?php foreach ($result['slike'] as $img): ?>
<?php if ($gal['id_galerija'] == $img['galerija_id']): ?>
<?php endif ?>
<?php endforeach ?>
<img src="" >
<figcaption></figcaption>
</figure>
<?php endforeach ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.