| | |
Error comparing strings from arrays
Please support our PHP advertiser: PostgreSQL or MySQL? Compare and contrast the two most popular open source databases
![]() |
•
•
Join Date: Apr 2004
Posts: 27
Reputation:
Solved Threads: 0
I have the following code, which tries to see if someone selected a drop down menu item that is already part of the database. If it is, then the error message should show up.
[PHP]$size_of_user_array = count($user_selects);
$size_of_db_array = count($already_in_db);
for($y=0;$y<$size_of_user_array;$y++)
{
$input = $user_selects[$y];
for($r=0;$r<$size_of_db_array;$r++)
{
//echo "Already in $r = " .$already_in_db[$r];
//echo " ";
//echo "user_selects[ $y ] = " . $user_selects[$y];
$db_output = $already_in_db[$r];
echo " $db_output = $input ";
if($input == $db_output)
{
echo "Table ". $already_in_db[$r] . " is already a part of database " . $_SESSION['category'] . "Please make your selections again";
exit();
}
}
if($input == "none")
{
// do something else
}
else{
$database_name = $_SESSION['category'] . "." . $input;
//$sql=create_table($database_name);
//echo "database name = " . $database_name; // for debugging
//echo "Session abbr = " . $_SESSION['Abbr']; // ok ok, so perhaps I'm a little overkill with the debugging
//echo "Input = " . $input; // but isn't it nice to just uncomment and see the stuff?
//mysql_query($sql) or die("error making table! Please contact system admin for more help." . mysql_error());
//echo "Table Created!<br>";
//update_control($_SESSION['category'],$_SESSION['Abbr'],$input);
}
}
}
?>[/PHP]
The output for the code is as follows
But it doesn't.
Can someone point out my mistake?
Thanks!
Kris
[PHP]$size_of_user_array = count($user_selects);
$size_of_db_array = count($already_in_db);
for($y=0;$y<$size_of_user_array;$y++)
{
$input = $user_selects[$y];
for($r=0;$r<$size_of_db_array;$r++)
{
//echo "Already in $r = " .$already_in_db[$r];
//echo " ";
//echo "user_selects[ $y ] = " . $user_selects[$y];
$db_output = $already_in_db[$r];
echo " $db_output = $input ";
if($input == $db_output)
{
echo "Table ". $already_in_db[$r] . " is already a part of database " . $_SESSION['category'] . "Please make your selections again";
exit();
}
}
if($input == "none")
{
// do something else
}
else{
$database_name = $_SESSION['category'] . "." . $input;
//$sql=create_table($database_name);
//echo "database name = " . $database_name; // for debugging
//echo "Session abbr = " . $_SESSION['Abbr']; // ok ok, so perhaps I'm a little overkill with the debugging
//echo "Input = " . $input; // but isn't it nice to just uncomment and see the stuff?
//mysql_query($sql) or die("error making table! Please contact system admin for more help." . mysql_error());
//echo "Table Created!<br>";
//update_control($_SESSION['category'],$_SESSION['Abbr'],$input);
}
}
}
?>[/PHP]
The output for the code is as follows
PHP Syntax (Toggle Plain Text)
array(5) { [0]=> string(8) "boxsets " [1]=> string(4) "none" [2]=> string(4) "none" [3]=> string(4) "none" [4]=> string(4) "none" } array(4) { [0]=> string(11) "accessories" [1]=> string(5) "track" [2]=> string(4) "sets" [3]=> string(7) "boxsets" } accessories = boxsets track = boxsets sets = boxsets boxsets = boxsets <- should trigger accessories = none track = none sets = none boxsets = none accessories = none track = none sets = none boxsets = none accessories = none track = none sets = none boxsets = none accessories = none t rack = none sets = none boxsets = none
But it doesn't.
Can someone point out my mistake?
Thanks!
Kris
Tip: Somebody will probably reply to help you on this, but your chances of getting a reply are MUCH higher if you will narrow your problem down to the one or 2 steps where the problem actually occurs, then post only enough code to demonstrate the problem.
Many times, in this process, you'll discover the problem yourself. I know that many people like myself--when we see a long posting of code--give a quick read, then move on rather than analyze your 100 lines of code.
"Baby steps" is my motto in troubleshooting code or anything else. Use echo() statements liberally after each step to see if your variables contain what you think they should. Check every step of the way until you reach a step where something is not as you think it should be. Do not move to the next step until you resolve the problem. If you are unable--after searching the Internet, reading the documentation, etc--to fix your problem, post a simple code example to reproduce your problem, and state your question clearly.
Many times, in this process, you'll discover the problem yourself. I know that many people like myself--when we see a long posting of code--give a quick read, then move on rather than analyze your 100 lines of code.
"Baby steps" is my motto in troubleshooting code or anything else. Use echo() statements liberally after each step to see if your variables contain what you think they should. Check every step of the way until you reach a step where something is not as you think it should be. Do not move to the next step until you resolve the problem. If you are unable--after searching the Internet, reading the documentation, etc--to fix your problem, post a simple code example to reproduce your problem, and state your question clearly.
•
•
Join Date: Apr 2004
Posts: 27
Reputation:
Solved Threads: 0
all the comments in orange are debugging methods (echo()'s) which do what you suggest to make sure they are outputing the correct material.
I coded the entire page, and worked out all the kinks, then found I had a logic error, thus I had to interject in the middle of the page.
Thanks though, perhaps I'll cut down the code a little.
I think it's a syntaxitcal(sp) thing, a specific about php that I am missing...boy do I miss C++...
I coded the entire page, and worked out all the kinks, then found I had a logic error, thus I had to interject in the middle of the page.
Thanks though, perhaps I'll cut down the code a little.
I think it's a syntaxitcal(sp) thing, a specific about php that I am missing...boy do I miss C++...
The line from your output:
Should be triggering the warning message as you say. So, what would make those two apparently identical strings NOT be equal? Spaces? Other white characters such as line endings, tabs, carriage returns? Try changing your debugging echo statement to this:
[PHP]
echo "|".$db_output."|=|".$input."|";
[/PHP]
In your output, you should not have any space between the strings and the pipes. If you do, then walla, there is your problem. You can use trim() on both values to remove beginning and trailing white-space.
PHP Syntax (Toggle Plain Text)
boxsets = boxsets
[PHP]
echo "|".$db_output."|=|".$input."|";
[/PHP]
In your output, you should not have any space between the strings and the pipes. If you do, then walla, there is your problem. You can use trim() on both values to remove beginning and trailing white-space.
![]() |
Similar Threads
- I need help again (C++)
- Comparing Strings (Assembly)
- Zend PHP Certification (PHP)
- comparing two strings with linear search.. (Java)
Other Threads in the PHP Forum
- Previous Thread: php.ini file not being read
- Next Thread: Help to create a unique forum
| Thread Tools | Search this Thread |
# .htaccess 5.2.10 access apache api array autocomplete broken cakephp checkbox class clean clients cms code convert cron curl database date directory display dissertation dropdown dynamic echo$_get[x]changingitintovariable... email encode error explodefunction fairness file folder form function functions hack href htaccess html htmlspecialchars image include indentedsubcategory ip javascript joomla legislation limit link local login mail memberships menu methods multiple multipletables mysql mysqlquery network newsletters object oop open passwords paypal pdf persist php provider query radio random redirect remote script search secure securephp server sessions simple sockets source space spam sql subscription system table tutorial upload url user variable voteup web youtube





