Jump to content

Editing arrays


herghost

Recommended Posts

Hi everyone

 

I am using this code which is an example from a website

 

<?php
$file_handle = fopen("myfile", "rb");

while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);

print $parts[0] . $parts[1]. "<BR>";
}

fclose($file_handle);

?>

 

Which does what it is meant to, however what I am trying to do is put all the input into a form, so values can be changed.

 

How do I separate each part? Eg the 1st line of the files reads isactive = true, how would I grab this so I can put it back to the file later as isactive = false? Not all the options are true/false, most are just strings or ints.

 

Thanks

Link to comment
Share on other sites

Isn't that what you've just posted code to do?

 

 

If you mean how do you reference it:

<?php
$file_handle = fopen("myfile", "rb");
$vars = array();
while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
// either
$$parts[0] = $parts[1]; // $isactive = true;
// or
$vars[$parts[0]] = $parts[1]; // $vars['isactive'] = true; // this is the method I'd go for as some of your keys may have invalid variable names.
}

fclose($file_handle);

?>

Link to comment
Share on other sites

Hi Andy

 

Thanks for your reply

 

If I run your code (removing the extra $ sign) I get a host of errors:

 

Undefined offset: 1 in C:\wamp\www\bm\text.php on line 42 = $parts[0] = $parts[1];

Undefined offset: 1 in C:\wamp\www\bm\text.php on line 43 = $vars[$parts[0]] = $parts[1];

 

I have also noticed that the 1st couple of lines are commented with # so I have removed these lines wondering if this was causing the error, but still get them:

 

$file_handle = fopen("saves/server.properties", "rb");
while (!feof($file_handle) ) 
{
$line_of_text = fgets($file_handle);
if( !preg_match('/^#.*?\n/',$line_of_text) )
echo $line_of_text . "<br>";

$parts = explode('=', $line_of_text);
$parts[0] = $parts[1];
$vars[$parts[0]] = $parts[1];

}

 

 

 

Link to comment
Share on other sites

<?php
$file_handle = fopen("myfile", "rb");
$vars = array();
while (!feof($file_handle) ) {

$line_of_text = fgets($file_handle);
$parts = explode('=', $line_of_text);
echo '<pre> ' . print_r($parts, 1);
}

fclose($file_handle);

?>

Run that and post back the reply, I'm going out soon so can only help if you're quick.

 

 

The extra $ was intentional - PHP variable variables

Link to comment
Share on other sites


Array
(
    [0] => #Sat Jan 07 07:04:01 UTC 2012

)
Array
(
    [0] => isactive
    [1] => true

)
Array
(
    [0] => level-name
    [1] => world

)
Array
(
    [0] => enable-query
    [1] => false

)
Array
(
    [0] => allow-flight
    [1] => false

)
Array
(
    [0] => server-port
    [1] => 25565

)

Link to comment
Share on other sites

<?php
$file_handle = fopen("myfile", "rb");
$vars = array();
while (!feof($file_handle) )
{
   $line_of_text = fgets($file_handle);
   $parts = explode('=', $line_of_text);
   // if date required
   if ( !isset($parts[1]) )
   {
      $date = str_replace('#', '', $parts[0]);
      continue;
   }
   //if date not required
   if ( !isset($parts[1]) )
   {
      continue;
   }
   $vars[$parts[0]] = $parts[1];
}
fclose($file_handle);
?>

 

Link to comment
Share on other sites

Thanks Andy!

 

Enjoy your journey out! If anyone else or Andy when he gets back can help me further would be grateful!

 

I know have:

 

<form class="clean">
        <ol>
          <li>
            <fieldset>
              <legend>Server Properties</legend>
              <ol>
                <li style="">
                  <label for="nether">Allow Nether</label>
                  <select id="nether" name="nether">
                    <option value="true" <?php if ($vars['allow-nether'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['allow-nether'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="lname">Level Name</label>
                  <input type="text" id="lname" name="lname" value="<?php print $vars['level-name'];?>" />
                  <label for="query">Enable Query</label>
                  <select id="query" name="query">
                    <option value="true" <?php if ($vars['allow-query'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['allow-query'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="flight">Allow Flight</label>
                  <select id="flight" name="flight">
                    <option value="true" <?php if ($vars['allow-query'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['allow-query'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="port">Server Port</label>
                  <input type="text" id="port" name="port" value="<?php echo $vars['server-port'];?>" />
                  <label for="rcon">Enable RCON</label>
                  <select id="rcon" name="rcon">
                    <option value="true" <?php if ($vars['allow-rcon'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['allow-rcon'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="seed">Level Seed</label>
                  <input type="text" id="seed" name="seed" value="<?php echo $vars['level-seed'];?>" />
                  <label for="sip">Server IP</label>
                  <input type="text" id="sip" name="sip" value="<?php echo $vars['server-ip'];?>" />
                  <label for="wlist">Enable Whitelist</label>
                  <select id="wlist" name="wlist">
                    <option value="true" <?php if ($vars['white-list'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['white-list'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="san">Spawn Animals</label>
                  <select id="san" name="san">
                    <option value="true" <?php if ($vars['spawn-animals'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['spawn-animals'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="omode">Online Mode</label>
                  <select id="omode" name="wlist">
                    <option value="true" <?php if ($vars['white-list'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['white-list'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="pvp">Player v Player</label>
                  <select id="pvp" name="pvp">
                    <option value="true" <?php if ($vars['pvp'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['pvp'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="dif">Difficulty</label>
                  <select id="dif" name="div">
                    <option value="0" <?php if ($vars['difficulty'] == "0"){echo 'selected="selected"';}?> >Peaceful</option>
                    <option value="1" <?php if ($vars['difficulty'] == "1"){echo 'selected="selected"';}?> >Easy</option>
                    <option value="2" <?php if ($vars['difficulty'] == "2"){echo 'selected="selected"';}?> >Normal</option>
                    <option value="3" <?php if ($vars['difficulty'] == "3"){echo 'selected="selected"';}?> >Hard</option>
                  </select>
                  <label for="sname">Server Name</label>
                  <input type="text" id="sname" name="sname" value="<?php echo $vars['server-name'];?>" />
                  <label for="mode">Game Mode</label>
                  <select id="mode" name="mode">
                    <option value="0" <?php if ($vars['gamemode'] == "0"){echo 'selected="selected"';}?> >Survival</option>
                    <option value="1" <?php if ($vars['gamemode'] == "1"){echo 'selected="selected"';}?> >Creative</option>
                  </select>
                  <label for="max">Max Players</label>
                  <input type="text" id="max" name="max" value="<?php echo $vars['max-player'];?>" />
                  <label for="sm">Spawn Monsters</label>
                  <select id="sm" name="sm">
                    <option value="true" <?php if ($vars['spawn-monsters'] == "true"){echo 'selected="selected"';}?> >True</option>
                    <option value="false" <?php if ($vars['spawn-monsters'] == "false"){echo 'selected="selected"';}?>>False</option>
                  </select>
                  <label for="view">View Distance</label>
                  <input type="text" id="view" name="view" value="<?php echo $vars['view-distance'];?>" />
                  <label for="motd">Public Name</label>
                  <input type="text" id="motd" name="motdw" value="<?php echo $vars['motd'];?>" />
                  

                  
                </li>
              </ol>
            </fieldset>
          </li>
        </ol>
        <p style="text-align:right;">
          <input type="reset" value="CANCEL" />
          <input type="submit" value="OK" />
        </p>
      </form>

 

The problem is, i cant seem to get the selected function working properly, all selects default to the last possible answer (last <select> option)

Link to comment
Share on other sites

<?php
$file_handle = fopen("myfile", "rb");
$vars = array();
while (!feof($file_handle) )
{
   $line_of_text = fgets($file_handle);
   $parts = explode('=', $line_of_text);
   // if date required
   if ( !isset($parts[1]) )
   {
      $date = str_replace('#', '', $parts[0]);
      continue;
   }
   //if date not required
   if ( !isset($parts[1]) )
   {
      continue;
   }
   $vars[$parts[0]] = str_replace('false', '', $parts[1]);
}
fclose($file_handle);

if ( $vars['allow-nether'] ) echo 'selected="selected"';
if ( !$vars['allow-nether'] ) echo 'selected="selected"';?>

Link to comment
Share on other sites

Still not working Im afraid :(

 

//if date not required
   if ( !isset($parts[1]) )
   {
      continue;
   }
   $vars[$parts[0]] = str_replace('false', '', $parts[1]);


}
fclose($file_handle);


<option  <?php if ( $vars['allow-nether'] ) echo 'selected="selected"'; ?>  value="true"  >True</option>
                    <option <?php if ( !$vars['allow-nether'] ) echo 'selected="selected"';?> value="false" >False</option>

Link to comment
Share on other sites

Hi again Andy

 

Thanks for all your help on this, I fail to understand why its not doing what its meant to :P

 

I have run this with the original code and the updated code, so here goes:

 

$vars[$parts[0]] = $parts[1];
Array
(
    [allow-nether] => true

    [level-name] => This Blows!

    [enable-query] => true

    [allow-flight] => false

    [server-port] => 25565

    [enable-rcon] => false

    [level-seed] => 

    [server-ip] => 

    [white-list] => false

    [spawn-animals] => true

    [online-mode] => true

    [pvp] => true

    [difficulty] => 1

    [server-name] => Unknown Server

    [gamemode] => 0

    [max-players] => 20

    [spawn-monsters] => true

    [view-distance] => 10

    [motd] => A Minecraft Server

)

 


$vars[$parts[0]] = str_replace('false', '', $parts[1]);

Array
(
    [allow-nether] => true

    [level-name] => This Blows!

    [enable-query] => true

    [allow-flight] => 

    [server-port] => 25565

    [enable-rcon] => 

    [level-seed] => 

    [server-ip] => 

    [white-list] => 

    [spawn-animals] => true

    [online-mode] => true

    [pvp] => true

    [difficulty] => 1

    [server-name] => Unknown Server

    [gamemode] => 0

    [max-players] => 20

    [spawn-monsters] => true

    [view-distance] => 10

    [motd] => A Minecraft Server

)

Link to comment
Share on other sites

#Minecraft server properties

#Sat Jan 07 07:04:01 UTC 2012

allow-nether=true

level-name=This Blows!

enable-query=true

allow-flight=false

server-port=25565

enable-rcon=false

level-seed=

server-ip=

white-list=false

spawn-animals=true

online-mode=true

pvp=true

difficulty=1

server-name=Unknown Server

gamemode=0

max-players=20

spawn-monsters=true

view-distance=10

motd=A Minecraft Server

Link to comment
Share on other sites

<?php
$file_handle = fopen("myfile", "r"); // This is a standard text file, binary mode not needed
$vars = array();
while (!feof($file_handle) )
{
   $line_of_text = fgets($file_handle);
   if(substr($line_of_text,0,1)=='#') continue; // This is to skip comment lines
   $parts = explode('=', $line_of_text);  // Other lines in key=value pairs
   $vars[$parts[0]] = trim($parts[1]); // use trim to remove any unnecessary whitespace from key/values 
}
fclose($file_handle);
header('Content-Type: text/plain'); // we want to display a plain text file
print_r($vars);

 

 

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.