using database library calls within a helper - 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: using database library calls within a helper (/showthread.php?tid=22143) |
using database library calls within a helper - El Forum - 08-30-2009 [eluser]AF[/eluser] I am just starting to use CI, and I am getting stuck with this same issue. It may be that I am thinking incorrectly in how to arrange my code. I have a set of helper functions that deal with adding some functionality to the calendar library. Within those functions, I need to get a difference between two date. This is very easy in mysql, select date_diff. But I have been completely unable to make database calls unless the function is in a model. And if the function is in a model, I cannot use the function in a helper. I can't use $this within a helper function... I have to be thinking of this incorrectly, any assistance? using database library calls within a helper - El Forum - 08-30-2009 [eluser]InsiteFX[/eluser] Hi AF, you can do something like this in helpers. Code: function your_function Enjoy InsiteFX using database library calls within a helper - El Forum - 08-30-2009 [eluser]AF[/eluser] Could you give me a little more detail? Here is the code: function datediff($sdate, $anchor) { $query="SELECT DATEDIFF('$sdate','$anchor')"; $query =$this->db->query($query); $row=$query->result_array(); return($row); } So, maybe this is a basic question, but what is the & in your case? And, thank you too. using database library calls within a helper - El Forum - 08-30-2009 [eluser]jegbagus[/eluser] [quote author="AF" date="1251710269"]Could you give me a little more detail? Here is the code: function datediff($sdate, $anchor) { $query="SELECT DATEDIFF('$sdate','$anchor')"; $query =$this->db->query($query); $row=$query->result_array(); return($row); } So, maybe this is a basic question, but what is the & in your case? And, thank you too.[/quote] hello af, & get_instance suppose to get instance that CI create when you fire a request. your library and everything that you call from load function will be at this instance. the code will be Code: function datediff($sdate, $anchor) { any other opinion? using database library calls within a helper - El Forum - 08-31-2009 [eluser]Boris Strahija[/eluser] [quote author="AF" date="1251699598"]I am just starting to use CI, and I am getting stuck with this same issue. It may be that I am thinking incorrectly in how to arrange my code. I have a set of helper functions that deal with adding some functionality to the calendar library. Within those functions, I need to get a difference between two date. This is very easy in mysql, select date_diff. But I have been completely unable to make database calls unless the function is in a model. And if the function is in a model, I cannot use the function in a helper. I can't use $this within a helper function... I have to be thinking of this incorrectly, any assistance?[/quote] I think you're doing it the wrong way. Why don't you just make a pure PHP function, without the db call. using database library calls within a helper - El Forum - 08-31-2009 [eluser]AF[/eluser] Ok, That helps. I had thought I would put it in a library earlier, but the functions don't fit nicely into a class, they are more just a variety of different functions. A pure PHP function call instead of a db call is an option, but it means I rebuild a bunch of wheels in php when I have the functions working and tested in mysql... I do take your point, is there an easy date_diff function in php? I don't know it off hand. Thanks for your help, I appreciate the knowledge and experience. using database library calls within a helper - El Forum - 08-31-2009 [eluser]Boris Strahija[/eluser] [quote author="AF" date="1251743015"]Ok, That helps. I had thought I would put it in a library earlier, but the functions don't fit nicely into a class, they are more just a variety of different functions. A pure PHP function call instead of a db call is an option, but it means I rebuild a bunch of wheels in php when I have the functions working and tested in mysql... I do take your point, is there an easy date_diff function in php? I don't know it off hand. Thanks for your help, I appreciate the knowledge and experience.[/quote] Just take a look at the CI date helper, and the function timespan(). |