Welcome Guest, Not a member yet? Register   Sign In
Problem with MVC and passing the query data
#1

[eluser]michalo[/eluser]
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):
Code:
<?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
        
        $this->load->model('propozycje_model');
        $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):
Code:
<?php
class Propozycje_model extends CI_Model {


     function __construct()
     {
         // Call the Model constructor
         parent::__construct();
     }
    
     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();
     }


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

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

&lt;/body&gt;
&lt;/html&gt;

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

[eluser]osci[/eluser]
your query is wrong.
Code:
<=wymaganePieniadze
//??????
#3

[eluser]michalo[/eluser]
Sorry for stupid question, but what exaclly is wrong? wymaganePieniadze is INT field in table propozycje.

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

[eluser]osci[/eluser]
sorry, didn't read carefully.
disregard my last post.

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

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

[eluser]osci[/eluser]
I believe where syntax is
WHERE column_name operator value

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

and in your view you would do
Code:
if (isset($wynik) & ($wynik <> NULL))
{
//do your loop here
}
#7

[eluser]michalo[/eluser]
Thank's for good advice. When I manualy assign variables from form it works.

I placed
Code:
$pieniadze=$_POST['pieniadze'];
$dla_kogo=$_POST['dla_kogo'];
instead of
Code:
extract($_POST);

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

[eluser]toopay[/eluser]
*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
Code:
// in some controller function
        $pieniadze = 1;
        $dla_kogo = 'foo';
        $this->load->library('dbhelper');
        $test_query = $this->dbhelper->select_fields('nazwa, opis')
                                     ->_from('propozycje')
                                     ->_where_open()
                                     ->_where(array('wymaganePieniadze','>=', $pieniadze))
                                     ->_and()
                                     ->_where(array('dlaKogo','=', $dla_kogo))
                                     ->_where_close()
                                     ->_order_by('id')
                                     ->query();
        var_dump($test_query);
If you cool with your query, you can run it by change '->query()' to '->execute()'. Happy debugging.




Theme © iAndrew 2016 - Forum software by © MyBB