Welcome Guest, Not a member yet? Register   Sign In
Unable to pass variable from one function to another function
#1

[eluser]scm22ri[/eluser]
This is what I'm tying to do.

(When I refer to state and city. I literally mean US states and cities, New York, Texas etc...)

In order for me to display similar dealers on every dealership page I'm going to have to use the state and city variables. In my original function I'm passing the city variable but not the state variable. I've extracted the state variable via a function I've made (syntax below) but this is where I'm running into trouble.

When I extract the state I'm attempting to convert it as a variable but it's not working (syntax below). My question is, how would I convert what I'm extracting (the state) to a variable?

In the below function I'm getting the state from the id that I'm passing.

Code:
function get_state_from_the_id($id){

$this->db->select('state');
$this->db->from('dealers');
$this->db->where('id',$id);
$query = $this->db->get();
        return $query->result();
}

This is where I'm running into trouble

Code:
$state = ($this->Dealer_data->get_state_from_the_id($id) > 0) ? FALSE : TRUE;

In the above line of code I'm attempting to extract the state as a variable from my function but it's not working. My objective is to extract the state (IE, new york, texas, california etc...) and insert that variable into another function I made. What am I doing wrong? How can I correct my mistakes?

Thanks
#2

[eluser]InsiteFX[/eluser]
Read this:

CodeIgniter User Guide - Generating Query Results

Code:
return $query->row();
#3

[eluser]CroNiX[/eluser]
This line has several problems:
Code:
$state = ($this->Dealer_data->get_state_from_the_id($id) > 0) ? FALSE : TRUE;

First, get_state_from_the_id() is returning an OBJECT of a result set, since you return $query->result().
So you are saying, if (OBJECT > 0), which will most likely cause an error since you can't compare an INT to an OBJECT like that.

Then, you cast the result as a boolean. So $state will only be TRUE/FALSE, and not hold the state data.
#4

[eluser]scm22ri[/eluser]
Hi guys,

Thanks for both of your responses and help.

This is how I corrected the problem.

The below syntax result passes an array (result()) but I put it in a foreach statement and returned it as a single string
Code:
$this->db->select('state');
  $this->db->from('dealers');
  $this->db->where('id',$id);
  $query = $this->db->get();
  
  foreach($query->result() as $row){
   $data = $row->state;
  }
  return $data;

The syntax in my controller. The below single can be converted into a variable because I'm passing a string. I can also pass an array (which I was doing before) but that's where my problem was.

Code:
$state = $this->Dealer_data->get_state_from_the_id($id); // gets the state

On my view
Code:
foreach($simInfo as $getState){
                                      $state = $getState->state;
                                      echo $state;
    }

Thanks for the help everyone
#5

[eluser]CroNiX[/eluser]
Code:
$this->db->select('state');
  $this->db->from('dealers');
  $this->db->where('id',$id);
  $query = $this->db->get();
  
  foreach($query->result() as $row){
   $data = $row->state;
  }
  return $data;

can be rewritten as
Code:
$state = $this->db
  ->select('state')
  ->where('id', $id)
  ->get('dealers')
  ->row(); //since you are only getting one row, just get the row.  No need to iterate over an array to get the single row.
return (isset($state->state)) ? $state->state : null; //return state name, or null if not found

Code:
$state = $this->Dealer_data->get_state_from_the_id($id);
print_r($state);
if (is_null($state))
{
  //state not found, handle it
}
#6

[eluser]scm22ri[/eluser]
That's much easier to read. Thanks again, appreciate your help.




Theme © iAndrew 2016 - Forum software by © MyBB