update functie in php?

Status
Niet open voor verdere reacties.

ginojo

Gebruiker
Lid geworden
7 sep 2010
Berichten
912
hallo allemaal,

Ik heb dit eigenlijk no nooit eerder nodig gehad. Nu wil ik net als in sql een php bestand bewerken. Heb je in php een soort update functie om slecht 1 waarde in een bastand te herschrijven?? dus als je bijv dit hebt:
PHP:
$waarde1 = "hoi";
$waarde2 = "hello";
$waarde3 = "hai";
en dat ik dit dan vanuit en andere file kan veranderen in,
PHP:
$waarde1 = "hoi";
$waarde2 = "welcome";
$waarde3 = "hai";
dus slecht 1 waarde wijzigen?

Alvast bedankt,
Ginojo
 
dus je hebt een php script, en in dat script moet een variabele eenmalig veranderd worden?
Ik zou het proberen met get_file_content() en dat met str_replace('$waarde2="hello"', '$waarde2="welcome"');
en dat het weer wegschrijven als een php file.

Maar vanwaar zou je online een php-file willen laten veranderen?
 
Neeeeeeee dit moet je niet doen. Dat is een zeer slecht idee. Als het voor je programma nodig is om tijdens de uitvoering een .php file te wijzigen is er iets mis met je programma.

Als je ons uitlegt wat je wil doen, dan kunnen we je helpen een betere oplossing te vinden.
 
@Frats:
Vandaar ook mijn vraag aan hem waarom hij variabelen wilt wijzigen.
Kan me ook eerlijk gezegd geen rede bedenken waarom je dat zou moeten doen.
Maar als hij het perse zo wilt of denkt te moeten doen, zou dit een manier kunnen zijn.

Ben wel heel benieuwd wat zijn wens is om te verwezelijken!
 
Oke, ik heb jullie in erge spanning laten zitten.. ;)

Mijn reden is:
Ik wil dus een website maken die volledig zonder enige html ervaring aan te passen is. Ik zat dus te denken aan de text.php file te includen en dan op de plekken waar de tekst moet komen die waarde te echo'en. En als je het wilt aanpassen dat je de waarde een soort van kunt updaten... :)

Is het een beetje duidelijk?
Of is dit een heel omslachtige manier?

Alvast bedankt voor jullie reacties,
Ginojo
 
je wilt dus een soort template system maken.
Op you tube staan tuts!
 
Wat is precies de reden dat je dit niet met een database wil doen? Die zijn daarvoor, zegmaar.

En inderdaad, je bent op zoek naar een template of CMS systeem, daar zijn er een hoop van die je als inspiratie kan gebruiken.
 
dit zou je kunnen helpen
PHP:
<?php
	/**
	* @author Lieven Roegiers
	* @copyright 2009
 	* @CMS autosite
 	* opensource http://code.google.com/p/autosite/ 
	*/

	/**
	 * getpropertiefile()
	 * 
	 * @param mixed $path the path where the translation file is
	 * @param mixed $lang lang = locale like(NL, FR) and is like form_FR.properties 
	 * @param mixed $name name of the file
	 * @return new properties()
	 */
	function getpropertiefile($path,$lang,$name){
		//print $path.$autosite['translations']. "forms_".$lang.".properties exist:".isexist_propertiefile($path,$lang,$name);
		if(isexist_propertiefile($path,$lang,$name)){
				print"<!-- @LANGlocale:".$lang." IS USED !!!properties!!! -->";
				return new properties($path,"_".$lang,$name);
		}else{
				print"<!-- @LANGlocale:".$lang." IS NOT EXIST !!!properties!!! -->";
				return new properties("./layout2/translations/","",$name);
		}
	}
	/**
	 * isexist_propertiefile()
	 * @param mixed $filelocation the path where the translation file is
	 * @param mixed $locale lang = locale like(NL, FR) and is like form_FR.properties 
	 * @param mixed $name name of the file
	 * @return bool
	 */
	function isexist_propertiefile($filelocation,$locale,$name){
		return is_file($filelocation.$name."_".$locale.".properties");
	}	
	class properties{
		private $filelocation;	
	    private	$file;
	    private	$toget=array();//!important!speed you if you not check of it is a array
	    private $delemiter = " ";
		/**
		 * properties::__construct()
		 * 
    	 * @param mixed $filelocation the path where the translation file is
    	 * @param mixed $locale lang = locale like(NL, FR) and is like form_FR.properties 
    	 * @param mixed $name name of the file
		 * @param integer $lifetime not used at this time
		 * @return
		 */
		function __construct($filelocation,$locale,$name,$lifetime = 0){
			$this->filelocation= $filelocation.$name.$locale.".properties";
			//$this->test();
		}
		/**
		 * properties::is_filelocation()
		 * 
		 * @return bool
		 */
		private function is_filelocation(){
			return(isset($this->filelocation)&& is_file($this->filelocation));
		}
		/**
		 * properties::open()
		 * 
		 * @param string $mode
		 * @return bool
		 */
		private function open($mode='r'){
			return $this->is_filelocation() && $this->file=fopen($this->filelocation,'r');
		}
		/**
		 * properties::open_2write()
		 * 
		 * @param string $mode
		 * @return bool
		 */
		private function open_2write($mode='a+'){
			return $this->is_filelocation() && $this->file=fopen($this->filelocation,'a+');
		}
		/**
		 * properties::getproperties()
		 * 
		 * @param mixed $tofind_keys
		 * @param string $prefix
		 * @return array(key=>value)
		 */
		public function getproperties($tofind_keys,$prefix=""){
			$asoarr=array();
            
			if(!is_array($tofind_keys)){
				print("[error]varkeys translations not found (properties.inc)");
			}else{
				if ($this->open()){
					while (!feof($this->file)){    			
			    			$fline = fgets($this->file, 4096);
			    			$pos = strpos($fline,$this->delemiter);
			    			$key = substr($fline,0,$pos);
                            $propertie = $prefix.$key;
			    			if($pos>1 && array_key_exists($propertie,$tofind_keys) ){
								$value = substr($fline,$pos+1);
								$asoarr[$key]=str_replace(array("\r\n", "\n", "\r"), "",$value);
							}
					}
				}
			//$this->test2($tofind_keys,$asoarr);
			}
			return $asoarr;	
		}
		/**
		 * properties::isexist()
		 * //TODO underconstruction
		 * @param mixed $label
		 * @return bool not used at this time
		 */
		private function isexist($label){//
			return true;
		}
		/**
		 * properties::find_value()
		 *  //TODO underconstruction
		 * @param mixed $str_tofind
		 * @param mixed $bufferarr
		 * @return return if exist in list else
		 */
		public function find_value($str_tofind,&$bufferarr){
			if (array_key_exists($bufferarr,$str_tofind)){
				return $bufferarr[$str_tofind];
			}else{
				if ($this->open()){
					while (!feof($this->file)){    			
		    			$fline = fgets($handle, 4096);
		    			$pos = strpos($fline,$this->delemiter);
						if($pos>1 && substr($fline,0,$pos)===$str_tofind){
							$bufferarr[$str_tofind]= substr($fline,$pos+1,-0);//last pieces		
						}else{
							print substr($fline,0,$pos);
						}
					}
				}	
			}	
		}
		/**
		 * properties::test()
		 * 
		 * @return 
		 */
		private function test(){
			print("debug<br />");
			print($this->filelocation ."is".$this->is_filelocation().'<br />');
		}
		/**
		 * properties::test2()
		 * 
		 * @param mixed $keyarr
		 * @param mixed $foundarr
		 * @return
		 */
		private function test2($keyarr,$foundarr){
			print("debug<br />");
			print_r($keyarr);
			print("<br />");
			print_r($foundarr);
			print("<br />");
		}
		/**
		 * properties::__destruct()
		 * 
		 * @return
		 */
		function __destruct(){
			    $this->file=null ;	
		}	
	}
?>

aanroep dan ook

PHP:
$autosite['lang'] =(isset($_GET['lang']))? $_GET['lang'] : 'NL';
$formsdata['login']['items']=array("username"=>"","password"=>"","login!2"=>"");
if(isexist_propertiefile($autosite['translations'],$autosite['lang'],"forms")){
				print"<!-- @LANGlocale:".$autosite['lang']." IS used -->";
				$properties = new properties($autosite['translations'],"_".$autosite['lang'],"forms");//lang keys and locales
			}else{
				print"<!-- @LANGlocale:".$autosite['lang']." IS NOT EXIST -->";
				$properties = new properties($autosite['translations'],"","forms");//default keys and translations
			}
//mijn template systeem bekijk je dan maar op de repos
$template->translate($properties->getproperties($formsdata['login']['items']));
 
Laatst bewerkt:
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan