Page 1 of 1

Ultra simple...what on earth am I missing? [Solved]

Posted: Sun Mar 18, 2007 2:56 pm
by supergrover1981
Hi gang,

I've been up all night trying to finish a little project. Part of it involves a login script - nothing big or important, but I can't use pear modules due to client's paranoid sysadmin.

Anyhow, I'm having trouble with one ridiculously simple snippet of code. Any help deeply appreciated.

Code: Select all

foreach ($passwordarray['username'] as $key=>$value) {
	echo $value;
        $debugvar = 'supergrover';
	if ($value == 'supergrover') {
		echo "You're in!";
	}
}
Now, for the dear life of me, I cannot get the conditional ("if") statement to trigger. 'supergrover' is one of the values echoed on the first line of the foreach loop, but the conditional just won't run.

If I replace $value in the conditional with $debugvar, it works fine. If I change the 'supergrover' value in the conditional to any other value in the $passwordarray['username'] array, the problem still occurs.

A var_dump of $passwordarray['username']:
array(2) { [0]=> string(17) "supergrover
" [2]=> string(14) "testuser
" }

Any suggestions really appreciated - I'm sure I'm missing something ridiculously simple, but I'm completely stumped. :-)

Cheers,
- JB

Posted: Sun Mar 18, 2007 3:43 pm
by feyd

Posted: Sun Mar 18, 2007 3:56 pm
by supergrover1981
*grin*

Many thanks for the reply, Feyd. Good idea, but unfortunately it didn't solve the problem. :-/

This is...bizarre.

Posted: Sun Mar 18, 2007 4:13 pm
by feyd
How did you use it?

Posted: Sun Mar 18, 2007 4:56 pm
by supergrover1981
---Hi Feyd - if you do come back to read this message, please ignore it after all. I just realised I was appending a line break to my array values. *cowers in shame*

Anyway, many thanks
- Supergrover. :-)


----------------
*grin*

Hi Feyd, many thanks for your interest - I do appreciate it. :-)

The code involves a whole bunch of object-oriented classes sprawled all over the place. For the sake of convenience & readabilty, I've cut it down into a very simple procedural script (attached below). I've checked and the problem does still exist with this script.

Here is the relevant page in its entirety:

Code: Select all

<?php

//A simple connection procedure. This runs fine. "(my password)" is replaced with, you guess it, my password.
$connection = mysql_connect('localhost', 'root', '(my password)') or die("Could not make a MySQL Connection");
$drb = mysql_select_db("shop", $connection) or die("Could not connect to the databse");
$query = "SELECT username, password FROM user";
$result = mysql_query($query, $connection) or die("Could not run the MySQL query");


//Makes an array with column headers as keys and values as rows.
$i = 0;
while($row = mysql_fetch_assoc($result)) {
	foreach ($row as $key=>$value) {
		$fetcher[$key][$i] = $value."<br />";
		$i++;
	}	
}


//In theory, it returns "you're in" if 'supergrover' is one of the values from the username column. ...In practice, it doesn't. Please note that echo $value DOES return supergrover as one of its values.
foreach ($fetcher['username'] as $key=>$value) {
	echo $value;
	$debugvar = 'supergrover';
	if (trim($value) == 'supergrover') {
		echo "You're in!";
	}

}

?>

Posted: Sun Mar 18, 2007 5:00 pm
by supergrover1981
*bangs head against wall*

I just realised - it's the ".<br />" in the array. Feel free to hurl tomatoes, eggs and/or hammers. I was initially just going to echo out the values, before I needed to evaluate them.

As expected, a simple, stupid error. Many thanks for the interest, I do appreciate it. Apologies for my stupidity.

...Now, I think it's time for some sleep. :-)

Cheers,
- SuperGrover