CodeIgniter Forums
own CONSTANT? - 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: own CONSTANT? (/showthread.php?tid=50319)



own CONSTANT? - El Forum - 03-22-2012

[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?


own CONSTANT? - El Forum - 03-22-2012

[eluser]Martenvanurk[/eluser]
You can use the define function of PHP for it : http://php.net/define


own CONSTANT? - El Forum - 03-22-2012

[eluser]Josssi[/eluser]
where will be stored value of this new constant? It will be stored till I will change its value again?


own CONSTANT? - El Forum - 03-22-2012

[eluser]Josssi[/eluser]
or an other question, can in define it in cofig file if i want to have it static stored?


own CONSTANT? - El Forum - 03-22-2012

[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.


own CONSTANT? - El Forum - 03-22-2012

[eluser]Josssi[/eluser]
Thank you very much for reaction Smile,
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 Smile


own CONSTANT? - El Forum - 03-22-2012

[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
//important so people can't directly access the variable!
if ( ! defined('BASEPATH')) exit('No direct script access allowed');
define('NAME_OF_CONSTANT', value);
which should be fairly simple to create and write the file with php.

Code:
file_put_contents(path_to_temp_constant.php, $string_containing_the_above_code);