cheechm Posted October 6, 2007 Share Posted October 6, 2007 Hello, I was just wondering: Say I have an array like so (in lang.php) $lang = array ( [welcome] => Welcome to the site ); How would I be able to insert that into a text field in a another file say admin.php and then be able edit the "Welcome to the Site" bit and save it? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/ Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 $lang['welcome']= "New data"; mis-read your need to write the whole PHP file again.. kinda bad idea.. a database would be better.. why would you want to do this ? Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363325 Share on other sites More sharing options...
Wuhtzu Posted October 6, 2007 Share Posted October 6, 2007 Sorry I misread the question lang.php <?php $lang = array('welcome' => 'Welcome to site', 'other_message' => 'Some other message' ); ?> admin.php <?php include('lang.php'); echo $lang['welcome']; echo $lang['other_message']; ?> Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363327 Share on other sites More sharing options...
Wuhtzu Posted October 6, 2007 Share Posted October 6, 2007 MadTechie isn't it a quite common way to deal with multiple languages in php applications? All he needs is a lang.php with language for the site and lang_admin.php with language for the admin part. Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363328 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 the 2 ways i use are #1 a database (easy) #2 many files (one for each language) on option 2, your only need to write the array contents <?php $lang = array('welcome' => 'Welcome to site', 'other_message' => 'Some other message' ); ?> but still its the whole file.. if its a configuration file, then its a bad idea, if its just the language file, then thats fine.. unless its going to happen many times (multiple people opening the file at ones will cause major problems) thats way i asked why they are doing it Personally when i language files, i write one in English and send that to be translated to XYZ languages, if the customer whats to change it themselfs i nomally go for a database.. of course if its a short amount of text then one file is fine, i just put all the arrays into thats file, but the idea is still the same. Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363334 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 It is just gonna be in English. It is a custom CMS. It will only ever be short amount of text. However how would I pull it from the array and post it in a text field where the user can edit it then save it back to the array? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363464 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 your probably have tp do something like this <?php $filename = 'lang.php'; include $filename; $handle = fopen($filename, "r"); $contents = fread($handle, filesize($filename)); fclose($handle); $NewLang = $lang; //update NewLang //ie $NewLang['Extra'] = "New Data"; $NewContents = ""; $NewArray = ""; foreach($NewLang as $K => $V) { $NewArray .= "\"$K\" = \"$V\", "; } $NewArray = trim($NewArray, ","); $NewContents = '<?php'; $NewContents = '$lang = array('; $NewContents .= $NewArray; $NewContents .= ')'; $NewContents .= '?>'; //write back to file if (is_writable($filename)) { if (!$handle = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($handle, $NewContents ) === FALSE) { echo "Cannot write to file ($filename)"; exit; } fclose($handle); } } ?> Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363485 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 Thanks very much for that. That looks like it is just creating new ones, how would I update them? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363517 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 thats for the update just read the array into form elements and to update run a script like the above example to add elements your need to update this part //update NewLang //ie $NewLang['Extra'] = "New Data"; Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363520 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 Sorry. How would I put it in form elements and what IDs would I need to give the form elements? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363527 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 you sure you want to do it this way.. a database it easier to manage.. i don't have time to explain it at the moment if you can write a form and post it, i'll try to get some time in the morning (i'm in the UK) and write a basic versioin Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363537 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 I suppose it could be easier in a database however then each time I want to call it would I have to use $row[Data]? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363539 Share on other sites More sharing options...
MadTechie Posted October 6, 2007 Share Posted October 6, 2007 true, but you could have the lang.php file open the database and create the array and then refer to that. so it would still seam exactly the same as having the array you suggested Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363541 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 Here is a form: <form name='Language' action='lang.php' method='POST' id="lang"> <input type='text' name='data' id='data' size='45' value=''> </form> How would I be able to insert into that? Also how could I write the array name before the input type so for instance if it is the welcome message it would display "Welcome"? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363544 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 Anyone else? Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363575 Share on other sites More sharing options...
darkfreaks Posted October 6, 2007 Share Posted October 6, 2007 <?php $lang = array('welcome' => 'Welcome to site', 'other_message' => 'Some other message' ); ?> <?php echo $lang; ?><form name='Language' action='lang.php' method='POST' id="lang"> <input type='text' name='data' id='data' size='45' value=''> </form> Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363583 Share on other sites More sharing options...
cheechm Posted October 6, 2007 Author Share Posted October 6, 2007 Does that insert the welcome message into the text input? Thanks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363587 Share on other sites More sharing options...
darkfreaks Posted October 6, 2007 Share Posted October 6, 2007 you want it in the input?and no it will display it before the input <input type='text' name='data' id='data' size='45' value='<?php echo $lang; ?>'> Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363590 Share on other sites More sharing options...
MadTechie Posted October 7, 2007 Share Posted October 7, 2007 erm.. no it would be <input type='text' name='data' id='data' size='45' value='<?php echo $lang['welcome']; ?>'> Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363894 Share on other sites More sharing options...
cheechm Posted October 7, 2007 Author Share Posted October 7, 2007 Ok. Thanks MadTechie and darkfreaks Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-363959 Share on other sites More sharing options...
darkfreaks Posted October 7, 2007 Share Posted October 7, 2007 thanks techie i didnt know how to display aspecific message the one i did would array everything and notspecifically one message Quote Link to comment https://forums.phpfreaks.com/topic/72094-solved-editing-info-in-an-array/#findComment-364050 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.