Welcome Guest, Not a member yet? Register   Sign In
Ordering DB results by date
#1

[eluser]patrick-vibes[/eluser]
Hi,

I am trying to order my results by date to show latest portfolio items limited to 10 order by most recent.

The code I have to retrieve the results, that works fine is:

Code:
$data['query'] = $this->db->get('portfolio' , 10);

but am am not sure how to order this by date - whether I need to use the more convoluted PHP way like SELECT from ORDER BY date or if something like this is the better way:

Code:
$data['query'] = $this->db->get('portfolio' , 10);
$data['query'] = $this->db->orderby("date", "ASC");

I know it might be a simple question but I have had a good trawl through the userguide and forum and can not find a simple answer.

Thanks in advance.
#2

[eluser]Yash[/eluser]
[url="http://ellislab.com/codeigniter/user-guide/database/active_record.html#select"]USER GUIDE[/url]

$this->db->order_by();

Lets you set an ORDER BY clause. The first parameter contains the name of the column you would like to order by. The second parameter lets you set the direction of the result. Options are asc or desc, or random.
$this->db->order_by("title", "desc");

// Produces: ORDER BY title DESC
#3

[eluser]patrick-vibes[/eluser]
I read that but am still bit confused as to where it requires a new line as in my second code example, or where the orderby clause goes into the original query section
#4

[eluser]Yash[/eluser]
Code:
$this->db->orderby("date", "ASC");    
$query = $this->db->get('portfolio' , 10);
$result=$query->result_array() ;
print_r($result);

This will work for u
#5

[eluser]patrick-vibes[/eluser]
Cant seem to get that working, I get an error in the browser.

Basically I am working with the basic code I created when I did the Blog tutorial.

So for my controller:
Code:
<?php

    class Blog extends Controller {
        
        function Blog()
        {
            parent::Controller();
            
            $this->load->scaffolding('portfolio');
            $this->load->helper('url');
        }
        
        function index()
        {
            $data['title'] = "Portfolio Vibes";
            $data['heading'] = "Recent Vibes";
            $data['query'] = $this->db->get('portfolio' , 10);
            $this->load->view('blog_view' , $data);
        }
    
}

?>

and for my view I have:
Code:
<?php
$this->load->view('header');
?>

<body>

<?php
$this->load->view('masthead');
?>

<h2>&lt;?php echo $heading; ?&gt;</h2>

&lt;!--Start Pieces--&gt;

&lt;?php foreach($query->result() as $row): ?&gt;
<div class="block">
<img >image ?&gt;" title="&lt;?php echo $row->title ?&gt;" class="Tips2" />
<h3>&lt;?php echo $row->title ?&gt;</h3>
<p>&lt;?php echo $row->brief ?&gt;</p>
<p>&lt;?php echo $row->technologies ?&gt;</p>
<p><a >link ?&gt;" rel="external" title="&lt;?php echo $row->link ?&gt;">Click here to view the project</a></p>
</div>
&lt;?php endforeach; ?&gt;

&lt;!--Pieces End--&gt;


&lt;?php
$this->load->view('footer');
?&gt;
#6

[eluser]Yash[/eluser]
Quote:Cant seem to get that working, I get an error in the browser.
show me error

and
Code:
&lt;?=$this->load->view('header');?&gt;

&lt;body&gt;

&lt;?=$this->load->view('masthead');?&gt;

<h2>&lt;?=$heading; ?&gt;</h2>

&lt;!--Start Pieces--&gt;

&lt;?php foreach($query->result() as $row): ?&gt;
<div class="block">
<img >image ?&gt;" title="&lt;?php echo $row->title ?&gt;" class="Tips2" />
<h3>&lt;?php echo $row->title ?&gt;</h3>
<p>&lt;?php echo $row->brief ?&gt;</p>
<p>&lt;?php echo $row->technologies ?&gt;</p>
<p><a >link ?&gt;" rel="external" title="&lt;?php echo $row->link ?&gt;">Click here to view the project</a></p>
</div>
&lt;?php endforeach; ?&gt;

&lt;!--Pieces End--&gt;


&lt;?=$this->load->view('footer');
?&gt;
I mean use short hand php
#7

[eluser]patrick-vibes[/eluser]
Using this:

Code:
&lt;?php

    class Blog extends Controller {
        
        function Blog()
        {
            parent::Controller();
            
            $this->load->scaffolding('portfolio');
            $this->load->helper('url');
        }
        
        function index()
        {
            $data['title'] = "Portfolio Vibes";
            $data['heading'] = "Recent Vibes";
            $this->db->orderby("date", "ASC");    
            $query = $this->db->get('portfolio' , 10);
            $result=$query->result_array() ;
            print_r($result);
            $this->load->view('blog_view' , $data);
        }
    
}

?&gt;


Gives this error:

Code:
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: query

Filename: views/blog_view.php

Line Number: 19

Use short hand? I am trying not too - I know its a but i was worried about server compatibility once completed and migrated.
#8

[eluser]patrick-vibes[/eluser]
Working now - using the code below. Realised that you define the sorting first, then I am calling the results into a data declaration and passing through to the view.
Code:
&lt;?php

    class Blog extends Controller {
        
        function Blog()
        {
            parent::Controller();
            
            $this->load->scaffolding('portfolio');
            $this->load->helper('url');
        }
        
        function index()
        {
            $data['title'] = "Portfolio Vibes";
            $data['heading'] = "Recent Vibes";
            
            $this->db->orderby("date", "ASC");
            $data['query'] = $this->db->get('portfolio' , 10);
            
            
            $this->load->view('blog_view' , $data);
        }
    
}

?&gt;
#9

[eluser]Yash[/eluser]
yea that was my point [sorting of db query]

you have solved the error ...but Error comes because you are passing only variable and not array.




Theme © iAndrew 2016 - Forum software by © MyBB