![]() |
Unable to pass variable from one function to another function - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived General Discussion (https://forum.codeigniter.com/forumdisplay.php?fid=21) +--- Thread: Unable to pass variable from one function to another function (/showthread.php?tid=60145) |
Unable to pass variable from one function to another function - El Forum - 01-25-2014 [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 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 Unable to pass variable from one function to another function - El Forum - 01-26-2014 [eluser]InsiteFX[/eluser] Read this: CodeIgniter User Guide - Generating Query Results Code: return $query->row(); Unable to pass variable from one function to another function - El Forum - 01-26-2014 [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. Unable to pass variable from one function to another function - El Forum - 01-26-2014 [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'); 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){ Thanks for the help everyone Unable to pass variable from one function to another function - El Forum - 01-26-2014 [eluser]CroNiX[/eluser] Code: $this->db->select('state'); can be rewritten as Code: $state = $this->db Code: $state = $this->Dealer_data->get_state_from_the_id($id); Unable to pass variable from one function to another function - El Forum - 01-26-2014 [eluser]scm22ri[/eluser] That's much easier to read. Thanks again, appreciate your help. |