Welcome Guest, Not a member yet? Register   Sign In
Query array or object
#1

[eluser]überfuzz[/eluser]
At the moment I'm sorting the data like this, to be able to echo it out in the view-file.
Code:
$temp_news = $this->news->get_one_news($key); //array
$this->data['id'] = $temp_news['ID'];
$this->data['event'] = $temp_news['event'];
$this->data['datum'] = $temp_news['datum'];
//etc...
$this->load->view('login/template', $this->data);


I tried echoing it out but passed as an array, in the data-array. Like this:
Code:
$this->data['news'] = $this->news->get_one_news($key); //array
But I don't know how to echo it in the view-file.

get_one_news() is returning an array now, is it easier to work with an object?
Code:
return $query->row_array();
return $query->row();
#2

[eluser]imn.codeartist[/eluser]
If it returns as an array then you can use the variable as in array in view file
#3

[eluser]rogierb[/eluser]
I guess that depends on your personal preference. I use an object for 99% of the time.
Since your result is one record, I would go for an array.

As for you code:
<code>
$this->data['news']
</code>

Would result to
<code>
echo $news['id'];
echo $news['event']
</code>
In the view file.
#4

[eluser]überfuzz[/eluser]
Mhmm, as simple as that..?
Thax dude!!!
#5

[eluser]überfuzz[/eluser]
Lets say I'm returning it like an object. How would I echo it then?
#6

[eluser]rogierb[/eluser]
<code>

if(isset($your_result_object) && $your_result_object->num_rows()>0)
{
//only one row:
$row= $your_result_object->row()
echo $row->id;
echo $row->event

//multiple rows
foreach($your_result_object->result() as $row )
{
echo $row->id;
echo $row->event
}
}
}

</code>

or something similar;
#7

[eluser]überfuzz[/eluser]
That is smooth!

Just to straighten some ?-marks: If I where to query a result that I need to tinker with. Like altering an array, would that exclude getting the result as an object? (I can't seem to come up with a GOOD example, but please, bear with me.) Anyway, lets say we have this:

Code:
if ($query->num_rows() > 0)
{
    $bad_array = $query->result_array();
    foreach($bad_array AS $value)
    {
        $nice_array[$value['ID']] = explode('[br]',$value['event']);
    }
    return $nice_array;
}
A simple form-to-html/text function gives the submitter the option of adding a [bb]-tag. In one view-file we like to echo the first part and in an other view-file looping out every text-paragraph. Is it possible to do this as easy with object..?




Theme © iAndrew 2016 - Forum software by © MyBB