• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Problem with MVC and passing the query data

Hi, I'm new in Code Igniter framework so perhaps my problem is very easy to you, but I viewed many post and I couldn't resolve it.

I have following error:
Fatal error: Call to a member function get_search_results() on a non-object in D:\Program Files\VertrigoServ\www\application\controllers\propozycje.php

A PHP Error was encountered
Message: Undefined property: Propozycje::$Propozycje_model
Filename: controllers/propozycje.php

My controller file (located in: controllers/propozycje.php):
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Propozycje extends CI_Controller {
    public function search()
        extract($_POST); //here I have variables: $pieniadze, $dla_kogo
        $data['wynik'] =  $this->Propozycje_model->get_search_results($pieniadze, $dla_kogo);
        $this->load->view('propozycje_view', $data);

Here is my model file (located in: models/propozycje_model.php):
class Propozycje_model extends CI_Model {

     function __construct()
         // Call the Model constructor
     function get_search_results($pieniadze, $dla_kogo)
    $query= $this->db->query('SELECT nazwa, opis FROM propozycje WHERE '.$pieniadze.'<=wymaganePieniadze AND dlaKogo='.$dla_kogo.';');
        return $query->result();

And my view (located in: views/propozycje_view.php):
<!DOCTYPE html>
&lt;html lang="en"&gt;
    &lt;meta charset="utf-8"&gt;

foreach($wynik as $row)    
  echo  $row->nazwa . '<br />';
  echo  $row->opis . '<br />';
} ?&gt;


Could you help me what's wrong in code above?

your query is wrong.

Sorry for stupid question, but what exaclly is wrong? wymaganePieniadze is INT field in table propozycje.

When I change query to:
'SELECT * FROM propozycje';
nothing happend and I have the same error.

sorry, didn't read carefully.
disregard my last post.

$data['wynik'] =  $this->Propozycje_model->get_search_results($pieniadze, $dla_kogo);
//model name should be lowercase here.

I change it. Now it gives me: Array to string conversion error in the same line:
$data['wynik'] =  $this->propozycje_model->get_search_results($pieniadze, $dla_kogo);

I believe where syntax is
WHERE column_name operator value

also you should check if query holds any data and then return ->result()
return ($query->num_rows() > 0) ? $query->result() : FALSE;

and in your view you would do
if (isset($wynik) & ($wynik <> NULL))
//do your loop here

Thank's for good advice. When I manualy assign variables from form it works.

I placed
instead of

I have no idea why I can't use extract function but now it works fine. One more time thank you Smile

*sigh. You can use my dbhelper, just to check if you already created the right query, then you can try to run it too. It will be like this
// in some controller function
        $pieniadze = 1;
        $dla_kogo = 'foo';
        $test_query = $this->dbhelper->select_fields('nazwa, opis')
                                     ->_where(array('wymaganePieniadze','>=', $pieniadze))
                                     ->_where(array('dlaKogo','=', $dla_kogo))
If you cool with your query, you can run it by change '->query()' to '->execute()'. Happy debugging.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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