assing a value to DEfine

Reply

Join Date: Sep 2007
Posts: 16
Reputation: pedramphp is an unknown quantity at this point 
Solved Threads: 0
pedramphp pedramphp is offline Offline
Newbie Poster

assing a value to DEfine

 
0
  #1
Nov 19th, 2008
Dear folk ,

consider I define a constant value


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

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
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","AnotherValue")
  4. ?>


THanks
Last edited by peter_budo; Nov 20th, 2008 at 1:10 pm. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reply With Quote Quick reply to this message  
Join Date: Jul 2006
Posts: 828
Reputation: pritaeas will become famous soon enough pritaeas will become famous soon enough 
Solved Threads: 136
Sponsor
pritaeas's Avatar
pritaeas pritaeas is offline Offline
Practically a Posting Shark

Re: assing a value to DEfine

 
0
  #2
Nov 20th, 2008
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.
"If it is NOT source, it is NOT software."
-- NASA
Reply With Quote Quick reply to this message  
Join Date: Sep 2007
Posts: 16
Reputation: pedramphp is an unknown quantity at this point 
Solved Threads: 0
pedramphp pedramphp is offline Offline
Newbie Poster

Re: assing a value to DEfine

 
0
  #3
Nov 20th, 2008
could you tell me how this code look like !!
I'm not familiar with Regular Expression
thanks
Reply With Quote Quick reply to this message  
Join Date: Jun 2007
Posts: 1,227
Reputation: kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about kkeith29 has a spectacular aura about 
Solved Threads: 167
kkeith29's Avatar
kkeith29 kkeith29 is offline Offline
Nearly a Posting Virtuoso

Re: assing a value to DEfine

 
0
  #4
Nov 20th, 2008
you could get the file contents and then use str_replace.

  1. $file = 'values.inc.php';
  2. $fh = fopen( $file,'r' );
  3. $data = fread( $file,filesize( $file ) );
  4. fclose( $fh );
  5. //or you can use file_get_contents()
  6.  
  7. $str = 'define("data","myValue");';
  8. $with = 'define("data","newValue");';
  9.  
  10. $data = str_replace( $str,$with,$data );
  11.  
  12. $fh = fopen( $file,'w' );
  13. fwrite( $fh,$data );
  14. fclose( $fh );
Last edited by kkeith29; Nov 20th, 2008 at 6:05 pm.
Reply With Quote Quick reply to this message  
Join Date: Sep 2005
Posts: 1,075
Reputation: digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice digital-ether is just really nice 
Solved Threads: 66
Moderator
digital-ether's Avatar
digital-ether digital-ether is offline Offline
Veteran Poster

Re: assing a value to DEfine

 
0
  #5
Nov 21st, 2008
Originally Posted by pedramphp View Post
Dear folk ,

consider I define a constant value


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

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
  1. <?php
  2. /*--------values.inc.php ------------*/
  3. define("Data","AnotherValue")
  4. ?>


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

  1. <?php
  2.  
  3. $config = array();
  4.  
  5. $config['name1'] = 'value1';
  6. $config['name2'] = 'value2';
  7.  
  8. ?>

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.

  1. <?php
  2.  
  3. require_once('config.php');
  4.  
  5. $config['name1'] = 'newvalue1';
  6.  
  7. $contents = "
  8.  
  9. \$config = array();
  10. \$config[name1] = '{$config[name1]}';
  11. \$config[name2] = '{$config[name2]}';
  12.  
  13. ";
  14.  
  15. file_put_contents('config.php', $contents);
  16.  
  17. ?>

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.

  1. <?php
  2.  
  3. require_once('config.php');
  4.  
  5. $config['name1'] = 'newvalue1';
  6.  
  7. $contents = "\$config = array();";
  8.  
  9. foreach($config as $name=>$value) {
  10. $contents .= "\$config[$name] = '$value';";
  11. }
  12.  
  13. file_put_contents('config.php', $contents);
  14.  
  15. ?>

Another way of doing it is to create a Class, and define properties of that class. Example:
  1. class Config {
  2.  
  3. var $name1 = 'value1';
  4. var $name2 = 'value2';
  5.  
  6. }

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.
www.fijiwebdesign.com - web design and development and fun
Cpanel Email - Let users Register email accounts on your website upon registration
Ajax Chat - Fully browser based chat!
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC