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

[eluser]H8train[/eluser]
[quote author="Rick Jolly" date="1193389860"]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:
<?php foreach($result as $row): ?>

    <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;
[/quote]

That doesnt work either.

as for the above code with the error about the unexpected ":" I tried to change the line to this
Code:
foreach($query->result_array() as $row):
wich now doesn't give me an error but also doesnt output anything to me at all.

Thanks

Rich
#12

[eluser]Rick Jolly[/eluser]
Ok, my view code should work if you assign $query->result_array() to the view in the controller as in your post #2. You had an error in your post #8 which might have caused you troubles:
Code:
foreach($query->result_array():
// should be
foreach($query->result_array()):
#13

[eluser]John_Betong[/eluser]
Hi H8Train,

I made a mistake with the first foreach variable so:

1. went back to the documentation
2. found the correct function,
3. tried the code snippet in one of my projects
4. copied and pasted the actual code snippet here.
5, and copied the results of the query.

&nbsp;
Code:
//============= TEST ============================    
  if ($query->num_rows() > 0) {
    $lf = '<br />';

    echo $lf .$query->num_rows();

    foreach($query->result() as $row): // amended error
        
             echo $lf .$row->id;
             echo $lf .$row->title;
             echo $lf .$row->date;
             // your field name
             // echo $lf .$row->comments;

             // my field name
             echo $lf .substr($row->memo, 0, 122);
         echo $lf;
            
         endforeach;
    }else{
       echo 'Yes we have no records';
    }//endif

    die;    
//============= TEST ============================

The results from the above code are:
Quote:id ==> 4
Stress management
2005-01-28 09:41:11
Stress Management Just in case you've had a rough day, here is a stress management technique recommended in all the la

id ==> 3
The hinge and the teapot
2005-01-27 11:54:11
Earl was fixing a door and he found that he needed a new hinge, so he sent his wife Mary to the hardware store. At the

id ==> 2
The elephant and the turtle
2005-01-26 12:36:43
An elephant was drinking at a watering hole when a turtle approached. The elephant looked at the turtle for a minute an

id ==> 1
The weighing machine
2005-01-25 17:25:38
While waiting at a bus stop for a bus, a woman stepped onto a weight machine that told your fortune and weight for a quart
&nbsp;
Check the documentation for the difference between $query->result() and $query->result_array().

I prefer the former since it is less typing and less chance of making a syntax error.
&nbsp;
&nbsp;
&nbsp;
&nbsp;
#14

[eluser]H8train[/eluser]
OK thank you all for your help I now have it working after tinkering with the code posted by John_Betong, my code is as follows:
Controller:
Code:
function index()
        {

            $this->db->select("id , title , body , date");
            
            $this->db->from("entries");
            $this->db->orderby("id", "DESC");
            $data['query'] = $this->db->get();
            
                                
            $this->load->view('blog_view', $data);
                    
            }

and my view:
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>
Thank you all again for your help, I really appreciate it.

Rich




Theme © iAndrew 2016 - Forum software by © MyBB