Larry Pisani wrote:
Hi,

I need to do some text file manipulation of a text file from an html
interface.  I have a file with multiple lines where each line has 2 fields
each (a mac address and an associated vlan).  Here's an example of rht text
file:

/tmp/TEST:
====================
a1b2.c3d4.e5cc VLAN3
a1b2.c3d4.e5dd VLAN4
a1b2.c3d4.e5ee IS
a1b2.c3d4.e5ff VLAN6
a1b2.c3d4.e5a1 VLAN7
a1b2.c3d4.e5b2 VLAN8
a1b2.c3d4.e5c3 Printer
a1b2.c3d4.e5d4 Guest
a1b2.c3d4.e5e5 IS
a1b2.c3d4.e5f6 VLAN6
a1b2.c3d4.e5a2 VLAN2
a1b2.c3d4.e5a3 VLAN4
====================

Being an admin and not a developer of any kind, I'm having trouble reading
in the file and being able to output specific lines and fields of a php
array.

[ snip ]


Try this

<?php

  $infile   = "/tmp/TEST";
  $contents = file($infile);

if (!$contents) { die("Could not read ".$infile); }

  $tmp    = array();
  $lookup = array();

  while(list($num,$line) = each($contents))
  {
     $tmp = explode(" ",$line);
     $lookup[$tmp[0]] = $tmp[1];
  }

?>

Now you have an array with the "key" being the mac address, and the value being your vlan, something like this (actual output of the snippet above):

Array
(
    [a1b2.c3d4.e5cc] => VLAN3
    [a1b2.c3d4.e5dd] => VLAN4
)

You can search this array for the mac address, and then output the associated lan, or you be a bit more on-topic, create a form with the associated data, like so :

<?php

  echo '<table border="0">';
  echo '<tr><td colspan="2">VLAN Editor</td></tr>';
  while(list($mac,$vlan) = each($lookup))
  {
     echo '<tr><td>';
     echo $mac;
     echo '</td><td>';
     echo '<input type="text" name="'.$mac.'" value="'.$vlan."' />';
     echo '</td></tr>';
  }
  echo '</table>';

?>

You can add the form bits however you like :)

What I want to eventually do is read in the "records" and fields of this
file, present it as a form, allow admins to modify the second field and
write that back to the text file (while backing up the original file).

In order to preserve your original data, I would suggest you make a backup of it first (copy the file if you have to).


If you will be doing a lot of read/writes and updates, consider using a database to store the information.

[ trimmed rest ]

Hope this helps,
Burhan

--
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php



Reply via email to