CodeIgniter Forums
passing variable in model - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: passing variable in model (/showthread.php?tid=78684)



passing variable in model - eleumas - 02-25-2021

PHP Code:
function get_articles() {
    $articles $this->db->query("SELECT * FROM tbl_articles WHERE status = '1' AND lang = '$this->locale' ORDER BY id DESC");    foreach($articles->getResult('array') as $a) {
      $articleId $a['id']; // I would like pass this variable to get_ip function
    }
    return $articles;
  }  

function get_ip() {
    $test $this->db->query("SELECT * FROM tbl_images_preview WHERE article_id = ".$articleId."");
    return $test;
  

I have this snippet in my model. How can I pass the variable $articleId to get_ip function?

Thanks. Smile


RE: passing variable in model - Chroma - 02-25-2021

Before the return of $articles, use a loop to iterate through each article int he list one at a time and call get_ip() one each one in the loop. Then do something with the result that is passed back.