Welcome Guest, Not a member yet? Register   Sign In
need help,,, reverse foreach or reverse the query
#1

[eluser]H8train[/eluser]
is there a way to reverse this maybe to start from the bottom instead of the top:
<?php foreach($query->result() as $row): ?>

or to reverse the query:
$data['query'] = $this->db->get('entries');

basically each row has an incrementing id and I want to post the highest id number first, as in to post the newest id.

need help.

Thanks Rich
#2

[eluser]Alex007[/eluser]
[quote author="H8train" date="1193368234"
$data['query'] = $this->db->get('entries');
[/quote]

use the ORDER BY .. DESC clause:

Code:
$this->db->select("Field1, Field2...");
$this->db->from("entries");
$this->db->orderby("entryid", "DESC");
$query = $this->db->get();

$data['query'] = $query->result_array();
#3

[eluser]H8train[/eluser]
OK I tried this and it didn't work:
Code:
$this->db->select("id , title , body , date");
    $this->db->from("entries");
    $this->db->orderby("id", "DESC");
    $query = $this->db->get();
    
    $data['query'] = $query->result_array();
        $this->load->view('blog_view', $data);

this gets me this error:
Fatal error: Call to a member function on a non-object in
application/views/blog_view.php on line 190

which that line is my foreach:

<?php foreach($query->result() as $row): ?>
#4

[eluser]John_Betong[/eluser]
In your View try:

Code:
<?php foreach($query as $row): ?>
#5

[eluser]H8train[/eluser]
ok that at least got me some output but no content. my view looks like this:
Code:
<h3>&lt;?=$row->title?&gt;</h3>

    <small>Posted on: &lt;?=$row->date?&gt;</small><br>
    <p>&lt;?=$row->body?&gt;</p><br>
    <p>&lt;?=anchor('blog/comments/'.$row->id, 'comments');?&gt;</p>

its not grabbing the actual content and my anchor link doesnt contain the post ID anymore so the comments won't work correctly.



ok heres all that I have that works properly:

from My blog.php controller
Code:
function index()
        {
        
            $data['query'] = $this->db->get('entries');

            
            $this->load->view('blog_view', $data);
            
        }

from my blog_view.php view file:
Code:
<p>

    &lt;?php foreach($query->result() as $row): ?&gt;

    <h3>&lt;?=$row->title?&gt;</h3>

    <small>Posted on: &lt;?=$row->date?&gt;</small><br>
    <p>&lt;?=$row->body?&gt;</p><br>
    <p>&lt;?=anchor('blog/comments/'.$row->id, 'comments');?&gt;</p>
    
<hr>    

&lt;?php endforeach; ?&gt;


</p>

and my output is flowing like this:

Code:
Post 1 date 2005

Post 2 date 2006

Post 3 date 2007
but I want my output to flow like this:
Code:
Post 3 date 2007

Post 2 date 2006

Post 1 date 2005

I just want the newly posted data to be added at the top and not the bottom.

if anyone can help me to achieve this it would be greatly appreciated. I am new to PHP and would think this is an easy fix to a seasoned PHP coder.

Thank you for your replies.

Rich
#6

[eluser]Derek Allard[/eluser]
Your view looks fine. Definately this is a case of just an orderby. Try reverting back to the working code (only in the wrong order). Post your controller (don't worrry about your view). Then add the orderby as suggestioned. Try your code. Anything? Post that also for us.
#7

[eluser]H8train[/eluser]
ok heres my controller that works:
Code:
&lt;?php

    class Blog extends Controller {
    
        function Blog()
        {
            parent::Controller();
            
            $this->load->scaffolding('entries');
            
            $this->load->helper('url');
            $this->load->helper('form');
        
        
        }
    
        function index()
        {
        
            $data['query'] = $this->db->get('entries');

            
            $this->load->view('blog_view', $data);
        
            
        }
        
        function comments()
        {
        
            $this->db->where('entry_id', $this->uri->segment(3));
            $data['query'] = $this->db->get('comments');
            
            
            $this->load->view('comments_view', $data);
            
        
        }
        
        
        function comments_insert()
        {
                        
            
            $this->db->insert('comments', $_POST);
            
            redirect('blog/comments/' .$_POST['entry_id']);
            
        }
    }

?&gt;

I tried it like this with no luck
Code:
function index()
        {
        
            $data['query'] = $this->db->get('entries');
            $data['query'] = $this->db->orderby("id", "DESC");

            
            $this->load->view('blog_view', $data);
        
            
        }

I also tried a few variants of the orderby with no luck.

Thanks

Rich
#8

[eluser]John_Betong[/eluser]
Hi H8train,

Ok for the moment forget about your View file and concentrate on your data.

Try this:
Code:
function index()
        {

$this->db->select("id , title , body , date");
    
    $this->db->from("entries");
    $this->db->orderby("id", "DESC");
    $query = $this->db->get();
    
    if ($query->num_rows() > 0) {
      $lf = '<br />';
      foreach($query->result_array() as $row): // amended error
    
         echo $lf .$row->id;
         echo $lf .$row->title;
         echo $lf .$row->date;
         echo $lf .$row->comments;
     endforeach;
    }else{
       echo 'Yes we have no records';
    }//endif

    die;
    
    $data['query'] = $this->db->get('entries');
            
    $this->load->view('blog_view', $data);
            
    }//endfunc
#9

[eluser]H8train[/eluser]
I now get this error

Parse error: syntax error, unexpected ':' in /blahblah/application/controllers/blog.php on line 28

I tried changing":" to";" (well I say I tried it but I changed it in the php developer app that I'm using and the app gave me warnings that the code was wrong)with no luck, im just having a hard time learning this but I think im getting better.

So anyway how can I fix this?

Thank you for your replies.

Rich

woops forgot to say whats on that line:
Code:
foreach($query->result_array():
#10

[eluser]Rick Jolly[/eluser]
You are using "result_array" instead of "result". Result_array returns an array of data so you need to use array syntax - something like this:
Code:
&lt;?php foreach($result as $row): ?&gt;

    <h3>&lt;?=$row['title'];?&gt;</h3>

    <small>Posted on: &lt;?=$row['date'];?&gt;</small><br>
    <p>&lt;?=$row['body'];?&gt;</p><br>
    <p>&lt;?=anchor('blog/comments/'.$row['id'], 'comments');?&gt;</p>
    
<hr>    

&lt;?php endforeach; ?&gt;




Theme © iAndrew 2016 - Forum software by © MyBB