Forum Moderators: coopster

Message Too Old, No Replies

how can i load if/else variables from text file?

         

ahmed24

9:57 am on Jun 25, 2009 (gmt 0)

10+ Year Member



hey everyone,

i've currently got the following code that basically provides different values for the following variables: $start,$lunch & $end.
so if username1 equals user1 then the values are specified else if the username1 equals user2 then another set of values etc...

because these users will be ammended from time to time, i dont want to edit the code everytime and want to know how i can make the code below work by fetching data from a text file that has each user in 1 line with the variables set in the text file?

if ($username1=="user1") {
$start = "900";
$lunch = "1200";
$end = $normal;
}

elseif ($username1=="user2") {
$start = "800";
$lunch = "1200";
$end = "400";
}

else {
$start = "900";
$lunch = "1200";
$end = $normal;
}

Frank_Rizzo

11:23 am on Jun 25, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



A quick and easy way would be to do something like:

-----

$include_file = $username1 . '.txt';

include($include_file);

----

and then have separate files for each user

**** user1.txt
<?php
$start = "900";
$lunch = "1200";
$end = $normal;
?>
****

**** user2.txt
<?php
$start = "800";
$lunch = "1200";
$end = "400";
?>
****

That would be fine for not too many files but if there are many you may want to incorporate into one larger file. Bigger than that then a database would be a better option.

[edited by: Frank_Rizzo at 11:24 am (utc) on June 25, 2009]

ahmed24

11:39 am on Jun 25, 2009 (gmt 0)

10+ Year Member



thanks for your reply. sounds great.. but how would i do an if/elseif/else statement if i use the above method?

ahmed24

11:41 am on Jun 25, 2009 (gmt 0)

10+ Year Member



oh i just realised. if the text file exists then its an if anyway otherwise it is an else. but can i say if $include_file exists then include it otherwise set the variable values as default?

ahmed24

12:55 pm on Jun 25, 2009 (gmt 0)

10+ Year Member



its ok i've done it. i used the following:

if (is_file($include_file)) {
include($include_file);
}

else {
$start = "800";
$lunch = "1200";
$end = "400";
}