Is it possible to pass a variable from MODEL to CONTROLLER?! - Printable Version +- CodeIgniter Forums (https://forum.codeigniter.com) +-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20) +--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23) +--- Thread: Is it possible to pass a variable from MODEL to CONTROLLER?! (/showthread.php?tid=25462) |
Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009 [eluser]developer10[/eluser] First of all, my query runs twice (once for counting, once for limiting and displaying data): Code: 0.0687 SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE d_seo = 'informatika-elektronika-racunari-i-oprema' AND _f_grad = 'Sarajevo' AND f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC This is my model method: Code: function fetch_all($getDjelatnost, $getGrad, $getSlovo, $getStart, $per_page) But, what i want more than preventing the query run twice (i know many people here have the same problem), is to move those pagination configs to my controller. My question is: Is it possible to declare a variable like this: Code: $varToPass = $noLimit->num_rows(); and then pass it from the model to my controller (i want to use it there as total_rows config item for pagination)? That way i think i'd be able to move everything related to pagination to the controller. THANKS! Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009 [eluser]Unknown[/eluser] You could create a function that returned the value you want to pass such as: Code: function varToPass() { Code: $this->your_model->varToPass(); Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-12-2009 [eluser]developer10[/eluser] [quote author="beckyftw" date="1260689784"]You could create a function that returned the value you want to pass such as: Code: function varToPass() { Code: $this->your_model->varToPass(); that variable should contain the value mentioned above (from the previous model function). if i send it from a new function, it won't hold the value i need (num_rows BEFORE applying the limit) unless i run a similar query in that new function as well. so, i need the variable sent from the same function. thanks anyway! Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-13-2009 [eluser]bhbutter123[/eluser] the way that i have been doing it is by returning an array in my model so that essential here is what my model will setup and return to my controller: $database_information['data'] = the data from the table; $database_information['counts'] = various counts from the table you want to use; $database_information['other'] = any other information you would need; In your situation I would put that variable you want to pass inside of $database_information['other']; Is it possible to pass a variable from MODEL to CONTROLLER?! - El Forum - 12-13-2009 [eluser]jedd[/eluser] [quote author="cold_fusion" date="1260683921"] First of all, my query runs twice (once for counting, once for limiting and displaying data): Code: SELECT * FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE d_seo = 'informatika-elektronika-racunari-i-oprema' AND _f_grad = 'Sarajevo' AND f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC I think that for a count call you put less stress on your DB if you do this: Code: SELECT COUNT(id) AS count FROM _tbl_firme LEFT JOIN tbl_djelatnosti ON _tbl_firme.d_id = tbl_djelatnosti.d_id WHERE d_seo = 'informatika-elektronika-racunari-i-oprema' AND _f_grad = 'Sarajevo' AND f_vidljivo = 1 ORDER BY f_istaknuto DESC, _f_ime ASC Your model code is a bit scary. Rather than this: Quote: - you can just do this: Code: if($getDjelatnost && $getDjelatnost != 'Sve') With a ternary operator you could even get that down to a single line per conditional. Quote:But, what i want more than preventing the query run twice (i know many people here have the same problem), I don't think there's a way around this - you need to do a select that includes a count() against every qualifying record, and a separate select to pull in the specific records you want. Keep in mind that the DB will cache the first query, and if the relevant table doesn't change, then subsequent (identical) queries will be blindingly fast. Enjoy the thrill of the database engine's performance! |