Error comparing strings from arrays

Reply

Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Error comparing strings from arrays

 
0
  #1
Jun 29th, 2005
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 "&nbsp;";
//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
  1. 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" }
  2. accessories = boxsets
  3. track = boxsets
  4. sets = boxsets
  5. boxsets = boxsets <- should trigger
  6. accessories = none
  7. track = none
  8. sets = none
  9. boxsets = none
  10. accessories = none
  11. track = none
  12. sets = none
  13. boxsets = none
  14. accessories = none
  15. track = none
  16. sets = none
  17. boxsets = none
  18. accessories = none t
  19. rack = none
  20. sets = none
  21. boxsets = none

But it doesn't.

Can someone point out my mistake?

Thanks!

Kris
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Error comparing strings from arrays

 
0
  #2
Jun 29th, 2005
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.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: Error comparing strings from arrays

 
0
  #3
Jun 29th, 2005
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++...
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 354
Reputation: Troy is an unknown quantity at this point 
Solved Threads: 5
Troy's Avatar
Troy Troy is offline Offline
Posting Whiz

Re: Error comparing strings from arrays

 
0
  #4
Jun 29th, 2005
The line from your output:
  1. boxsets = boxsets
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.
Troy Wolf is the author of SnippetEdit. "Website editing as easy as it gets." IX Web Hosting
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: Error comparing strings from arrays

 
0
  #5
Jun 29th, 2005
Cool! Thanks for the help, I'll try those out.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 27
Reputation: Gnome_101 is an unknown quantity at this point 
Solved Threads: 0
Gnome_101 Gnome_101 is offline Offline
Light Poster

Re: Error comparing strings from arrays

 
0
  #6
Jun 29th, 2005
You were right.

The pipes show a trailing space on the side of the user selects.

I used a text file to upload those values, and after checking them, found a space after each entry.

:o Thanks for your help. I would have been stuck for quite some time without your assistance.

Kris
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:


Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC