CodeIgniter Forums
Database query order - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Development (https://forum.codeigniter.com/forumdisplay.php?fid=6)
+--- Forum: Issues (https://forum.codeigniter.com/forumdisplay.php?fid=19)
+--- Thread: Database query order (/showthread.php?tid=74653)



Database query order - chrisco - 10-20-2019

Hello,

I have the following code:
Code:
<?php echo $this->template->shortcode('pages', array('template' => 'default', 'id' => '45', 'limit' => '3')) ?>
Which showing 3 of publications from my pages table. But I need to show the newest 3 publications so can I order them by date and how can I do this in the code above?

And one more this, what exactly means $this->template->shortcode

Cheers!


RE: Database query order - jreklund - 10-20-2019

That's not part of CodeIgniter, you can try to find the function by searching for "function shortcode" and see what parameters it accepts.


RE: Database query order - php_rocs - 10-20-2019

@chrisco,

Also, we need more information/code. The code that you displayed is it in a view? If so, what controller is calling the view? Has this code worked before? Are you seeing error messages? What version of CI are you using?


RE: Database query order - chrisco - 10-20-2019

(10-20-2019, 06:21 AM)jreklund Wrote: That's not part of CodeIgniter, you can try to find the function by searching for "function shortcode" and see what parameters it accepts.

Hello jreklund, I found the function
Code:
function shortcode($path = 'pages'){
        $this->load->helper('file');

        $data = [
            //['id' => '', 'text' => '-- Default --'],
        ];
        $layout_files = get_dir_file_info(APPPATH . '/views/shortcodes/'.$path.'/', FALSE);

        if (is_array($layout_files))
        {
            sort($layout_files);
            foreach($layout_files as $key => $file_info)
            {
                $pathinfo = pathinfo($file_info['name']);
                if($pathinfo['extension'] == 'php' && $pathinfo['filename'] != 'default'){
                    $data[] = ['id' => $pathinfo['filename'], 'text' => $pathinfo['filename']];
                }
            }
        }
        header('Content-Type:application/json; charset=utf-8');
        exit(json_encode($data));
    }


(10-20-2019, 06:43 AM)php_rocs Wrote: @chrisco,

Also, we need more information/code.  The code that you displayed is it in a view?  If so, what controller is calling the view?  Has this code worked before?  Are you seeing error messages?  What version of CI are you using?

Hi php_rocs,
Yes, the code that I displayed is in the view, how can I found out which controller is calling the view? The code is working, but I need it to display the newest 3 publications and not just the first 3 publications in a database like it is now. 
CI version is 3.1.10

Cheers!


RE: Database query order - php_rocs - 10-20-2019

@chrisco,

Simply do a search in your code to find "view('THE_NAME_OF_THE_VIEW_FILE')" where THE_NAME_OF_THE_VIEW_FILE is the name of the view file without the .php suffix. So for example if the view name is header.php then the statement to search for would be... "view('header')".


RE: Database query order - jreklund - 10-20-2019

@chrisco,

Do you have any more function by that name? As it dosen't have a second parameter.


RE: Database query order - chrisco - 10-20-2019

(10-20-2019, 10:15 AM)jreklund Wrote: @chrisco,

Do you have any more function by that name? As it dosen't have a second parameter.

Yep I found this in Template.php in the libraries folder
Code:
function shortcode($model, $data = array()){
        $model = "{$model}_model";
        $this->CI->load->model($model);
        $c = $this->CI->$model->run($data);
        return $c;
    }



RE: Database query order - jreklund - 10-21-2019

Okey, that means you should look in the "run" function inside Pages_model (possible in application/models).


RE: Database query order - chrisco - 10-22-2019

(10-21-2019, 10:47 AM)jreklund Wrote: Okey, that means you should look in the "run" function inside Pages_model (possible in application/models).

This is what I did yesterday and fix my problem by adding this to run function:

Code:
public function run($params = array())
    {
        //-----run function code above------------

        if(!empty($params['order'])){
            $this->db->order_by($params['order'], 'DESC');
        }
    }

and append "order" param in my view to order them by date

Code:
$this->template->shortcode('pages', array('template' => 'default', 'id' => '45', 'order' => 'date', 'limit' => '3'))

Thanks for your help!