Welcome Guest, Not a member yet? Register   Sign In
Work with result in controller
#1

[eluser]MaxEisley[/eluser]
Hello guys,

i really still cannot find how to work with results in Controller. I can work with results only in viewer.

This is my data model, this function check, if the customer with these parameters exists:
Code:
function checkcustomer($phone, $email, $name, $surname) {
      $q = $this->db->query('SELECT *
      FROM zakaznik
      WHERE Telefon = "'.$phone.'"
      AND Email = "'.$email.'"
      AND Jmeno = "'.$name.'"
      AND Prijmeni = "'.$surname.'"');
      
      return ($q->row());
    }

This is my controller getting valid entries from viewer. I want to check, if customer with these values exists and when yes, then get customer_ID, when doesn't exist, then create customer:
Code:
$data['customer'] = $this->data_model->checkcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'));

if($customer->zakaznikID == '') {
              $id_customer = $this->data_model->createcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'), $id_address);
        }
        
        else {
          $id_customer = $customer->zakaznikID;
        }

And it writes these errors, i dont know how to work with results from Model in Controller:
Message: Undefined variable: customer
Message: Trying to get property of non-object
#2

[eluser]CroNiX[/eluser]
Well, in your controller you store it in $data['customer'] but you are trying to check in $customer. It shows up as $customer only in your view and as $data['customer'] in your controller.

So, in your controller, if you did:
Code:
$customer = $this->data_model->checkcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'));
$data['customer'] = $customer;
if($customer->zakaznikID == '') {
...
}
it would probably work, or just change:
Code:
if($customer->zakaznikID == '')
to
Code:
if($data['customer']->zakaznikID == '')
(and also the other place you use $customer)
#3

[eluser]MaxEisley[/eluser]
It doesnt work:
Code:
$customer = $this->data_model->checkcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'));

if($customer->zakaznikID == '') {
              $id_customer = $this->data_model->createcustomer($this->input->post('Phone'), $this->input->post('Email'), $this->input->post('FirstN'), $this->input->post('SecondN'), $id_address);
        }
        
        else {
          $id_customer = $customer->zakaznikID;
        }

It still writes Trying to get property of non-object
on line: if($customer->zakaznikID == '') {
#4

[eluser]CroNiX[/eluser]
well, in your query, are you returning the results as an array or object? It would appear an array and you are trying to access it as an object. Do a var_dump($customer) and see what the data looks like.
#5

[eluser]MaxEisley[/eluser]
I am receiving data from this:
Code:
function checkcustomer($phone, $email, $name, $surname) {
      $q = $this->db->query('SELECT *
      FROM zakaznik
      WHERE Telefon = "'.$phone.'"
      AND Email = "'.$email.'"
      AND Jmeno = "'.$name.'"
      AND Prijmeni = "'.$surname.'"');
      
      return ($q->row());
    }
#6

[eluser]skiter[/eluser]
You have a ROW, so is not an Object, this mean that you have array in array, in array? Smile

Anyway $row[0] is ur's 1 element from 'Select * ...', if you add (row results), to array, you will have multi-dimension array, then access liek others multi-dimension arrays.
Code:
if($data['customer']['ID'] == '') {
?

To many arrays in one post, sorry my en is quite poor Tongue
#7

[eluser]MaxEisley[/eluser]
So what i should change in data_model to result it not as an array ?
#8

[eluser]TWP Marketing[/eluser]
The User Guide ( http://ellislab.com/codeigniter/user-gui...esults.htm ) says that your returned value should be an OBJECT:
Quote:...
row()

This function returns a single result row. If your query has more than one row, it returns only the first row. The result is returned as an object. Here's a usage example:
$query = $this->db->query("YOUR QUERY");

if ($query->num_rows() > 0)
{
$row = $query->row();

echo $row->title;
echo $row->name;
echo $row->body;
}
...

Does your customer file "zakaznik" contain the field zakaznikID?

Second thought, if the query finds NO record it will return FALSE, which could give you the error "Trying to get property of non-object".
You should add the code to test if rows were found (in the User Guide example above).
#9

[eluser]MaxEisley[/eluser]
Yes you are right, thanks for help man Smile
#10

[eluser]MaxEisley[/eluser]
I have still problem with that, this is my model:
Code:
function getcar($id) {
      $q = $this->db->query('SELECT *
      FROM vozidlo
      WHERE vozidloID = '.$id);
      
      return $row = $q->row();
    }

Controller:
Code:
$car = $this->data_model->getcar($this->input->post('Car'));
///input post car containt valid ID car from form
echo $car->Cost;
echo $car->Color;

all writes this is non object property.

But when i write it in viewer like this, i got good result:
Code:
CT:
$car['car'] = $this->data_model->getcar($this->input->post('Car'));
Viewer:
echo $car->Cost;
echo $car->Color;




Theme © iAndrew 2016 - Forum software by © MyBB