ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

How to? ee()->db->query(""); inside Expression Engine templates

September 17, 2013 6:47am

Subscribe [2]
  • #1 / Sep 17, 2013 6:47am

    A.Daniel

    12 posts

    Hi, how can i query using CI methods?

    let’s say we have a expression engine template
    and instead of using

    {exp:query sql="SELECT distinct field_id_200 FROM exp_channel_data"}

    using this

    ee()->db->query("SELECT distinct field_id_200 FROM exp_channel_data")

    what files should i include?
    or how can i access the db files inside config/ in order to connect to db and get the files…

    can any one give me one simple example, i do not understand CI,

     

     

  • #2 / Sep 19, 2013 12:16pm

    Focus Lab Dev Team

    1129 posts

    Before diving into an answer:

    can any one give me one simple example, i do not understand CI

    If you don’t understand CI, why do you want to query the DB in this way?

  • #3 / Sep 19, 2013 12:39pm

    A.Daniel

    12 posts

    i wanted to build a more complex query based on multiple option selected on a form, and that involved some complex conditional and query string concatenation.

    Inside exp:query tag that wasn’t just possible.

    Finally i found a solution using php code as input +query tag.

    But anyway this code $query = $this->EE->db->query($sql);

    foreach ($query->result() as $row)
    {
    echo $row->field_id_30."
    ";

    }

    is ok?
    In my case it’s working
    I have some understanding on OOP, that’s why i said i don’t understand CI, but i want to dig in, because i see the ee is not 100%  solution to all problems, even with helps from third party plug ins and extensions…

  • #4 / Sep 19, 2013 12:48pm

    Focus Lab Dev Team

    1129 posts

    Yes, that should work fine. I do have one suggestion. In the event that your query doesn’t return any results you would end up with an error in the foreach loop. To avoid that you can add a conditional which can also serve as an {if no_results} equivalent.

    $query = $this->EE->db->query($sql);
    
    if ($query->num_rows() > 0)
    {
     foreach ($query->result() as $row)
     {
      echo $row->field_id_30."
    ";
     } 
    }

    ...i see the ee is not 100%  solution to all problems, even with helps from third party plug ins and extensions…

    No software will be 100% solution to all problems 😊

  • #5 / Sep 19, 2013 3:48pm

    A.Daniel

    12 posts

    Of course , no software would be the perfect solution. But for an simple option like this http://ellislab.com/forums/viewthread/237951/ i must learn how to build a plugin, and integrate it in admin,  in order to solve my problem.

     

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases