Jump to content

[SOLVED] Editing info in an array


cheechm

Recommended Posts

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

Link to comment
Share on other sites

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'];

?>

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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);
}
}
?> 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.