Welcome Guest, Not a member yet? Register   Sign In
Just the first record
#1

[eluser]Chris Williams[/eluser]
I'd like to get this:
Code:
title field

record one
record two
record three
etc

The data returned from the records has the title field in them already. But to access them I have to use a foreach statement as shown in the user guide.

I supposed I could do this with multiple queries, but is it possible to reuse results from the same query?
#2

[eluser]Michael Wales[/eluser]
I'm not exactly sure what the question is...

Your title says just the first record - to select just the first:

Code:
$query = $this->db->get('table', 1, 0);
return $query->row();

What your explanation sounds like is you are selecting titles and want to echo them out on at a time. For some reason you don't want to use foreach - not sure why, since that is what it is for...

Code:
$query = $this->db->get('table');
foreach ($query->result() as $record) {
  echo '<p>' . $record->title . '</p>';
}

Of course, this code is sloppy - there should be quite a few more checks to ensure what you expect to happen is happening, but you get the idea.
#3

[eluser]Chris Williams[/eluser]
I guess I'm trying to write something like this

Code:
//just the first record (or any really since all the records name this value)

&lt;?php some code to show field 1 from the first record only ?&gt;

//then all the records

&lt;?php foreach ($query->result() as $row): ?&gt;
    <tr>
        <td>&lt;?=$row->field2?&gt;</td>
        <td>&lt;?=$row->field3?&gt;</td>
        <td>&lt;?=$row->field4?&gt;</td>
        <td>&lt;?=$row->etc?&gt;</td>
    </tr>
&lt;?php endforeach; ?&gt;
#4

[eluser]Pascal Kriete[/eluser]
I don't know if I understand the problem. How is it different from doing this:
Code:
$row1 = $query->row();
echo $row1->field1;

//all records
#5

[eluser]Chris Williams[/eluser]
[quote author="inparo" date="1212369881"]I don't know if I understand the problem. How is it different from doing this:
Code:
$row1 = $query->row();
echo $row1->field1;

//all records
[/quote]

That's a good suggestion. A lot better than that I ended up with:

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

Which seems to work fine, if a bit hacky. :-)

Thanks!




Theme © iAndrew 2016 - Forum software by © MyBB