Welcome Guest, Not a member yet? Register   Sign In
passing query results into $data array
#1

[eluser]stef569[/eluser]
Hi, I'm trying to put values from one row into the view.
Code:
$sql = 'SELECT user_name, event_Bday, event_EDay FROM events,users WHERE event_id = ? AND events.user_id = users.user_id';
$query = $this->db->query($sql, array($event_id));
$data['row'] = $query->result_array();

But when I use the following code in the view:

Code:
echo $row['event_Bday'];

then I get the following error:

Quote:Message: Undefined index: event_Bday

a print_r of $data returns
Code:
Array ( [row] => Array ( [0] => Array ( [user_name] => Angela [event_Bday] => 2 [event_EDay] => 9 [event_Bmonth] => 11 [event_Emonth] => 11 [event_year] => 2007 ) ) [heading] => Bekijk een Event ) Data: 1
heading is accessable.
row will always contain only one row
#2

[eluser]Phil Sturgeon[/eluser]
The variable you pass to data is extract'ed. Basically this means that each array index is now its own variable.

In your view use:

Code:
echo $event_Bday;
#3

[eluser]gtech[/eluser]
I have interpreted what your code is doing differently
I have assumed your passing $data into the view?

in which case in the view you need to do somthing like:

view code
Code:
<?php foreach($row as $row_item):?>
    <b>Event Birthday:</b>&lt;?=$row_item['event_Bday']?&gt;<br>
  &lt;?php endforeach?&gt;

result_array returns an array of arrays so you can iterate through the results of a select statement that may return multiple rows of data.
e.g.
[0]['tablecolumn1']
[1]['tablecolumn1']


IF you don't want to do a foreach then you use row_array to build a 1 dimentional array INSTEAD of result_array

SO to save confusion:


Code:
..
//CONTROLLER:


$sql = 'SELECT user_name, event_Bday, event_EDay FROM events,users
        WHERE event_id = ? AND events.user_id = users.user_id';

$query = $this->db->query($sql, array($event_id));
$data['row'] = $query->row_array();
$this->load->view('view', $data);

=============================

//VIEW (view.php):

<b>Event Birthday:</b>&lt;?=$row['event_Bday']?&gt;<br>
#4

[eluser]Phil Sturgeon[/eluser]
He's right you know. My reply was a bunch of crap, I hadnt noticed you were returning result_array() to a variable called $row, that strikes me as a tad silly :p
#5

[eluser]gtech[/eluser]
easily done Smile ... thepyromaniac as your a senior member and going off the thread slightly do you know anything about sqlite and configuring it with codeigniter, as I have been trying to help somebody out in another thread (troubles conecting to a sqlite database) without much joy.

[edit] to save extra posts... Thanks anyway
#6

[eluser]Phil Sturgeon[/eluser]
Nope!




Theme © iAndrew 2016 - Forum software by © MyBB