...

View Full Version : Flat file array, replace 3rd key value based on itself.



Peuplarchie
07-21-2008, 04:42 AM
Good day to all of you,
Him working of a login script and him stopped at the display of the member.
The script is easy and it's text(flat) file based.
My problem is at the 3rd array level, when I try to display it, I need to change the value based on what it is.


here's the code :





<?PHP
$file = file("pass.txt');
$data= array();


$director = "Director/index.php";
$pretty = "Pretty/index.php";
$users = "User/index.php";



foreach($file as $value){
list($user, $password, $url) = explode('=>', $value);
$data[$user] = array("url"=>$url, "password"=>$password);



if ($url==$director):
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>Director<br/><br/>";

else if ($url==$pretty) :
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>Pretty<br/><br/>";

else:
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>User<br/><br/>";

endif;


}

?>




Thanks !
Take care !

_Aerospace_Eng_
07-21-2008, 04:50 AM
Is that your exact code?

Are you getting any errors?

You should be getting a parse error on the first line. You are missing a quote.

Peuplarchie
07-21-2008, 04:52 AM
Yes it is, my exact code, The quote is corrected, and it is not the problem here, I'm getting an :

Parse error: syntax error, unexpected T_IF, expecting ':' in

_Aerospace_Eng_
07-21-2008, 08:41 AM
You aren't using the correct if else syntax. I'm not sure why you thought a : represented an opening if statement or an endif; ended it.

<?php
$file = file("pass.txt")
$data= array();


$director = "Director/index.php";
$pretty = "Pretty/index.php";
$users = "User/index.php";



foreach($file as $value){
list($user, $password, $url) = explode('=>', $value);
$data[$user] = array("url"=>$url, "password"=>$password);



if ($url==$director) {
echo "<b>Member : </b>$user<br/>";
echo "<b>Code : </b>$password<br/>";
echo "<b>Status : </b>Director<br/><br/>";
}
else if ($url==$pretty) {
echo "<b>Member : </b>$user<br/>";
echo "<b>Code : </b>$password<br/>";
echo "<b>Status : </b>Pretty<br/><br/>";
}
else {
echo "<b>Member : </b>$user<br/>";
echo "<b>Code : </b>$password <br/>";
echo "<b>Status : </b>User<br/><br/>";
}
}
?>

dumpfi
07-21-2008, 01:59 PM
Colons are valid syntax, see here (http://www.php.net/manual/en/control-structures.alternative-syntax.php).

However, Peuplarchie missed this paragraph:

Note: Note that elseif and else if will only be considered exactly the same when using curly brackets as in the above example. When using a colon to define your if/elseif conditions, you must not separate else if into two words, or PHP will fail with a parse error.dumpfi

Peuplarchie
07-21-2008, 02:11 PM
it is not giving me error now, but it change the value of the url for all and put user like so :



Member : Jack
Code : Daniel
Status : User

Member : Angolina
Code : Joli
Status : User

Member : Mike
Code : Lee
Status : User

Peuplarchie
07-21-2008, 02:14 PM
<?PHP
$file = file("../../info.txt");
$data= array();


$director = "Director/index.php";
$pretty = "Pretty/index.php";
$users = "User/index.php";



foreach($file as $value){
list($user, $password, $url) = explode('=>', $value);
$data[$user] = array("url"=>$url, "password"=>$password);

if ($url==$director):
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>Director<br/><br/>";


elseif ($url==$pretty):
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>Pretty<br/><br/>";

else:
echo "<b>Member : </b>". $user ."<br/>";
echo "<b>Code : </b>". $password ."<br/>";
echo "<b>Status : </b>User<br/><br/>";

endif;
}
print_r ($data)

?>

p4plus2
07-21-2008, 02:56 PM
I would recommend you use { } instead of if endif. but thats just my opinion...But in either case make sure you have elseif as one word :).

_Aerospace_Eng_
07-21-2008, 05:55 PM
Colons are valid syntax, see here (http://www.php.net/manual/en/control-structures.alternative-syntax.php).

However, Peuplarchie missed this paragraph:
dumpfi
My mistake. I've never used that syntax so didn't know it was valid. Apologies to the OP.

Start echoing out the $url value to see what you are getting. It seems like its getting to the last else statement.

Peuplarchie
07-21-2008, 09:07 PM
it's the same, I just can't figure why it don't work.
Exact, it jum,ps the 2 first and go to the last one.

scoop_987
07-21-2008, 09:38 PM
erm... this may sound stupid, but have you ever tried checking your paths?

$director = "Director/index.php";
$pretty = "Pretty/index.php";
$users = "User/index.php";

if the paths are actually all in lower, then its gonna fail, thats if its a linux box.

It is usually overlooked alot by people.

oh and another thing, that last lot of code you posted will fail with missing ";"

print_r($data)
should be:
print_r(data);

Peuplarchie
07-22-2008, 01:38 AM
RESOLVED



foreach($file as $value){
$value = trim($value);
list($user, $password, $url) = explode('=>', $value);
$data[$user] = array("url"=>$url, "password"=>$password);



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum