0

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?

2 Answers 2

5

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');
Sign up to request clarification or add additional context in comments.

2 Comments

I now have solution for the first part :). Any ideas for the second part of the problem?
This is late, but you mean looping out the second part? Sure that's not a problem however I provided you with only the function this way you could implement in into any template as needed.
0

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 ?>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.