RE: [PHP] looping variables from a file
| From: | Ford, Mike [LSS] | Date: | Wed, 24 Mar 2004 18:12:27 +0000 |
| Subject: | RE: [PHP] looping variables from a file | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-181385@lists.php.net to get a copy of this message | ||
> -----Original Message-----
> From: Ryan A [mailto:ryan@coinpass.com]
> Sent: 24 March 2004 17:30
>
> Hi,
> I have a config_inc.php file which has around 60 parameters set in it.
> eg:
> $db_name="something";
> $db_user="root";
> $db_pass="blah";
> $x_installed_path="/home/blah/";
> etc
>
> I have a requirment of echoing out these statements to the
> client to show him how his setup is:
>
> eg:
> echo "DB_name=<font color green>".$db_name."</font>";
>
> if I was getting variables via a POST or a GET I would use
> something like
> this:
>
> foreach ($_POST as $key => $val)
> { echo "$key=<font color green> $value </font>";}
>
> as you can imagine the above loop will save a crapload of
> time instead of "partially hard codeing" each key and value
> but how do I do this while reading from a file? and the other
> big problem is the file contains one or two arrays...
One possibility, if you're able to change the format of the config_inc file
without causing too much havoc, would be to write it something like this:
$CONFIG = array(
'db_name' => 'something',
'db_user' => 'root',
'db_pass' => 'blah',
'x_installed_path => '/home/blah/',
...
);
Then you have your array all neatly set up for you. (And you can always
extract($CONFIG) whilst you convert all your other usages to $CONFIG[]...!!)
I do like John Holmes's solution, though!
As far as the arrays-within-the-array goes, this sounds like a perfect setup
for a recursive procedure. If you write it to emit, say, an unordered list,
then each recursive call will produce a nested list and, hey presto!, you've
got nicely structured output too.
Cheers!
Mike
---------------------------------------------------------------------
Mike Ford, Electronic Information Services Adviser,
Learning Support Services, Learning & Information Services, JG125, James
Graham Building, Leeds Metropolitan University, Beckett Park, LEEDS, LS6
3QS, United Kingdom
Email: m.ford@leedsmet.ac.uk
Tel: +44 113 283 2600 extn 4730 Fax: +44 113 283 3211