Need query help please!

#1
[eluser]draconus[/eluser]
I am trying to create a practitioner search page, and I am having issues with the outputting the data from the table.
Here is my model code:
Code:
function findPractitioner(){
        $form = array(
            'name' => $this->input->post('name'),
            'city' => $this->input->post('city'),
            'state' => $this->input->post('state'),
            'country' => $this->input->post('country')
         );
    
        $sql = "SELECT * FROM users WHERE practitioner = 1
            AND city = '$form[city]' AND state = '$form[state]' AND country = '$form[country]' AND firstName like
            '%$form[name]%' or lastName like '%$form[name]%'";            
        $query = $this->db->query($sql);
        return ($query->num_rows() > 0) ? $query->row_array() : FALSE;        
    }

and my controller code:
Code:
$this->load->model('users');
$data['query'] = $this->users->findPractitioner();
$this->load->view('practitioners', $data);

and my view code:
Code:
<? foreach($query->result() as $row): ?>
<h3>&lt;?echo  $row->$firstname?&gt;</h3>
&lt;? endforeach;?&gt;

Now obviously the error is that i am calling result() on a non object, but how do I return the results as an object?

#2
[eluser]Georgi Veznev[/eluser]
In the function findPractitioner() you have :

Code:
return ($query->num_rows() > 0) ? $query->row_array() : FALSE;

Are you sure that you want to use row_array() (single row) and not result_array()?


Try to edit the view like this:

Code:
&lt;? foreach($query as $row): ?&gt;
<h3>&lt;?echo  $row->firstname?&gt;</h3>
&lt;? endforeach;?&gt;

Greetings!

#3
[eluser]Unknown[/eluser]
It doesn't work because you return $query->row_array(). Just try to return only $query variable and it should work properly. Don't change your view. It's good.

Regards, Marcin.

#4
[eluser]draconus[/eluser]
Okay, this is closer now, but I just need to figure out how to display the results:
Code:
<td>&lt;?echo $row->$lastname?&gt; &lt;?echo $row->$lastName?&gt;</td>

And it cannot define the variable...

#5
[eluser]Georgi Veznev[/eluser]
[quote author="draconus" date="1233457743"]Okay, this is closer now, but I just need to figure out how to display the results:
Code:
<td>&lt;?echo $row->$lastname?&gt; &lt;?echo $row->$lastName?&gt;</td>

And it cannot define the variable...[/quote]

Code:
<td>&lt;?echo $row->lastname?&gt; &lt;?echo $row->lastName?&gt;</td>

#6
[eluser]draconus[/eluser]
It is giving this error:

Fatal error: Cannot access empty property in view

#7
[eluser]draconus[/eluser]
I got it. thanks for all the help guys... Tongue
the view was the issue:
Code:
&lt;?echo $row->$lastName?&gt;</td>
needed to be:
Code:
&lt;?echo $row->lastName?&gt;</td>


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.