DaniWeb IT Discussion Community

DaniWeb IT Discussion Community (http://www.daniweb.com/forums/index.php)
-   PHP (http://www.daniweb.com/forums/forum17.html)
-   -   assing a value to DEfine (http://www.daniweb.com/forums/thread158416.html)

pedramphp Nov 19th, 2008 7:17 pm
assing a value to DEfine
 
Dear folk ,

consider I define a constant value


<?php
  /*--------values.inc.php ------------*/
  define("Data","MyValue")
?>

so right now I want to change the Value of Data from another page and I wold like that to be saved on Values.inc.php file , you know I'm thinking some kind of data Store ,instead of using data bases.I have seen smarty template ENgine has done this before , could some on help me !!
let me clear , next time when I open the values.. file I would like to see
<?php
  /*--------values.inc.php ------------*/
  define("Data","AnotherValue")
?>


THanks

pritaeas Nov 20th, 2008 8:11 am
Re: assing a value to DEfine
 
You can change the file itself, if you have write permission on it. Open the file and use a regex to replace the old value with the new.

pedramphp Nov 20th, 2008 11:32 am
Re: assing a value to DEfine
 
could you tell me how this code look like !!
I'm not familiar with Regular Expression
thanks

kkeith29 Nov 20th, 2008 6:04 pm
Re: assing a value to DEfine
 
you could get the file contents and then use str_replace.

$file = 'values.inc.php';
$fh = fopen( $file,'r' );
$data = fread( $file,filesize( $file ) );
fclose( $fh );
//or you can use file_get_contents()

$str = 'define("data","myValue");';
$with = 'define("data","newValue");';

$data = str_replace( $str,$with,$data );

$fh = fopen( $file,'w' );
fwrite( $fh,$data );
fclose( $fh );

digital-ether Nov 21st, 2008 12:14 pm
Re: assing a value to DEfine
 
Quote:

Originally Posted by pedramphp (Post 740016)
Dear folk ,

consider I define a constant value


<?php
  /*--------values.inc.php ------------*/
  define("Data","MyValue")
?>

so right now I want to change the Value of Data from another page and I wold like that to be saved on Values.inc.php file , you know I'm thinking some kind of data Store ,instead of using data bases.I have seen smarty template ENgine has done this before , could some on help me !!
let me clear , next time when I open the values.. file I would like to see
<?php
  /*--------values.inc.php ------------*/
  define("Data","AnotherValue")
?>


THanks

I thought I'd add:

What you mention is common in PHP configuration files, or language files.

Usually, you know the names of the constants that you have saved in the file in advance. But if you don't, you could use an array, as it allows you to discover all the variables saved in it.

eg: file config.php

<?php

$config = array();

$config['name1'] = 'value1';
$config['name2'] = 'value2';

?>

Then when you want to add or remove something from your storage file, you can just include() the file, and get the array, modify it and write it back to file.

<?php

require_once('config.php');

$config['name1'] = 'newvalue1';

$contents = "

\$config = array();
\$config[name1] = '{$config[name1]}';
\$config[name2] = '{$config[name2]}';

";

file_put_contents('config.php', $contents);

?>

Note the forward slash before the $ signs lets PHP know that the $ sign is a literal instead of being the start of a variable.

It you don't know the names of the indexes in the array, then you can iterate through the array in a loop.
With constants, its harder to do this.

<?php

require_once('config.php');

$config['name1'] = 'newvalue1';

$contents = "\$config = array();";

foreach($config as $name=>$value) {
  $contents .= "\$config[$name] = '$value';";
}

file_put_contents('config.php', $contents);

?>

Another way of doing it is to create a Class, and define properties of that class. Example:
class Config {

  var $name1 = 'value1';
  var $name2 = 'value2';

}

Like an array, the properties of the class instance can be iterated over. Some consider this better because a class is automatically global while an array isn't.


All times are GMT -4. The time now is 7:54 pm.

Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2009 DaniWeb® LLC