Forum Moderators: coopster

Message Too Old, No Replies

Recieve data from textfile using PHP

         

thijsnetwork

2:47 pm on Mar 13, 2004 (gmt 0)

10+ Year Member



Hello,

While scripting PHP I encountered the following problem.

I'm using a flat text file to store the style preferences of each user. So they can choose their own style, it's stored in a text file (./data/customize.dat) and it looks like this for example:

username¦FF00FF¦FFFF00¦00FF00
anotheruser¦FF00FF¦FFFFFF¦00FF00
yetanother¦CC0000¦FF0000¦0000F0

As you can see, the 3 last items are parts of hexadecimal colors. Well, I've got the username in a cookie and I want to use the colors in my script like this:

$color[1], $color[2], ...

I know how to open a file and make loop, but I don't know how to check which row I have to use, according the username. This is what I've got so far (it's part of a function).


$results = file ($_SERVER["DOCUMENT_ROOT"] . "/tn_v32/lib/data/customize.dat");
if (!$results) {
return '/* ERROR OCCURED WHILE OPENING USERFILE */';
}

for($i = 0; $i < count($results); $i++) {

// what to do in here? :S

return $color;
}

Does anybody know how I can reach this using PHP?

Birdman

3:59 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This ought to do it for you. Note that array pointers usually start at zero, not one. I forced the color array to start at 1 by putting the index in brackets($color[1]).

for($i = 0; $i < count($results); $i++) {

$user_array = explode("¦",$results[$i]);

if ($user_array[0] == $_SESSION["username"]){
$color[1] = $user_array[1];
$color[2] = $user_array[2];
$color[3] = $user_array[3];
}

return $color;
}

thijsnetwork

4:07 pm on Mar 13, 2004 (gmt 0)

10+ Year Member



Thanks for your help, but it won't work as expected. It only works for the first user in the text-file, the first line. If for example "anotheruser" or "yetanother" logs in, it won't work.

coopster

4:27 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Move the return statement so that it is inside the if condition.

Birdman

4:30 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You have to put the actual name of the cookie in the $_SESSION var. Not an actual username but the name of the cookie. I used "username" because it is a sensible name for the cookie but we're confusing it with your example list which shows the first user's name as "username", when I really doubt someone would actually have that username.

Confused yet? I just confused myself ;)

if ($user_array[0] == $_SESSION["cookiename"]){

[edited by: Birdman at 4:31 pm (utc) on Mar. 13, 2004]

thijsnetwork

4:30 pm on Mar 13, 2004 (gmt 0)

10+ Year Member



works! :)

Birdman

4:34 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Oops, sorry. I didn't even realise it was in a function. I'm goin' blind.

thijsnetwork

4:34 pm on Mar 13, 2004 (gmt 0)

10+ Year Member



is it possible to simplify the array if it becomes longer. Because now there are just 3 colors, but it can be 10 in the future. With the current script i'll have to put 10 lines of code for each color :S like:

$color[x] = $user_array[x];
...

Solution?

Birdman

4:41 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You could put another for loop in there. I suggest you just call your colors starting at zero for simplicity.

if ($user_array[0] == $_SESSION["username"]){
for ($e=0; $e<count($user_array)-1; $e++){
$color[] = $user_array[$e];
}
return $color;
}

Then access your colors starting at $color[0]. If you really want to start at one, change the line to:

$color[$e+1] = $user_array[$e];

thijsnetwork

4:45 pm on Mar 13, 2004 (gmt 0)

10+ Year Member



Thanks for the code, it works.

Then access your colors starting at $color[0]
No, because that's the username ;)

Birdman

4:46 pm on Mar 13, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You are correct