own CONSTANT? |
[eluser]Josssi[/eluser]
Hi people, i need to create functionality for admin of the site and it is ability for changing value of customers bonus. I thing it is no very good to store it into database, so i want to store it into some CONSTAT, which i could call everywhere in code. Is it possible in CI to create my own constant?....Could you somebody give me an advice please?
[eluser]Martenvanurk[/eluser]
You can use the define function of PHP for it : http://php.net/define
[eluser]Josssi[/eluser]
where will be stored value of this new constant? It will be stored till I will change its value again?
[eluser]Josssi[/eluser]
or an other question, can in define it in cofig file if i want to have it static stored?
[eluser]Unknown[/eluser]
Hi, A constant stays constant. You can change it by changing your script. Next invocation it will be changed. It is simply read every time you run the script. For example: If you put this in your index.php: define("JOSSISCONSTANT", 25); It will be avialable as JOSSISCONSTANT. Problem: You have to watch the scope. Easier is this maybe: $GLOBALS['JOSSISCONSTANT'] = 25; And then refer to this everywhere you need it via $GLOBALS['JOSSISCONSTANT']. That should also work from within functions/methods/whereever you need it.
[eluser]Josssi[/eluser]
Thank you very much for reaction ![]() this reasont are just for one cycle so i have to use an other reason. You now, my system it is like in shop. If owner has good day, so as an admin can set -10% for every customer. This value 10 i wanted to store in config file without creating a database. I thought that it is possible to change value of CONSTANT. I thought that i can to set static value. Because if owner will be in the next day angry, so bonus -10% will be deleted, an I need in this contant have again value 0 ![]()
[eluser]CroNiX[/eluser]
If you don't want to or can't use a database, you could always manually include() a "temp_constant.php" file at the bottom of /application/config/constants.php. Then in the admin, have a field where the admin can change the value. If the value changes, recreate the define() in the temp_constant.php file. The temp_constant.php file would just be: Code: <?php Code: file_put_contents(path_to_temp_constant.php, $string_containing_the_above_code); |
Welcome Guest, Not a member yet? Register Sign In |