kaveman50 Posted October 27, 2009 Share Posted October 27, 2009 Does anyone know what's wrong with this array?It says that there is an error on line 19.Basically, I'm trying to make it so that a user enters a name, and it spits out if their name is in the list. If it's not, it'll say "Not found." <form method="post" action=""> Name: <input type="text" name="num1" size="10"/><br /><br /><input type="Submit" name="Submit" value="Look up name" /></form> <? $name=$_POST['num1'];$array=array('Al', 'David' ,'Sarah' ,'Don' ,'Ed' ,'Bob' ,'Frank', 'Hank', 'Jim' ,'Ben'); for($i=0; $i<=9; $i++){ if ($array[$i]==$name){echo "Found";}else[echo "Not found";]}?> Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 Oh yeah, and how do I make it so that the php code doesn't show up all black on my post? Quote Link to comment Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 You're using square brackets where you should be using curly ones in the else statement. There's also no need for a loop, just use in_array() Ex: if(in_array($name, $array)) { echo "Found"; } else { echo "Not Found"; } This can also be shortened to (using the Ternary Operator): echo (in_array($name, $array)) ? 'Found' : 'Not Found'; To post code use [ code ] or [ php ] tags (without the spaces). If you don't know what I'm talking about press quote on my post to see what it looks like. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 Now it's saying line 35? <form method="post" action=""> Name: <input type="text" name="num1" size="10"/> <br /> <br /> <input type="Submit" name="Submit" value="Look up name" /> </form> <? $name=$_POST['num1']; $array=array('Al', 'Bob', 'Carl','Don' ,'Eric' ,'Fred' ,'Greg', 'Hank', 'Irene' ,'Judy'); for($i=0; $i<=9; $i++){ if(in_array($name, $array)) { echo "Found"; } else { echo "Not Found"; } ?> Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 you're not closing your for() loop. and if your code was any larger than it is now, i wouldn't have even looked at it since 'Now it's saying line 35?' doesn't really mean anything to me other than 'Now it's saying line 35?'. Quote Link to comment Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 And as I said before you can completely remove that loop, it's unnecessary. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 I see your point mrMarcus. I completely forgot that the lines weren't numbered when i posted my code. Alright, so I removed the loop and changed my if statement, but now it is saying that i have an error on this line. if(in_array($name, $array)) Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 what does the error say Quote Link to comment Share on other sites More sharing options...
Alex Posted October 27, 2009 Share Posted October 27, 2009 There's nothing wrong with that line assuming those are the correct variables. You really need to post the entire section in question. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 I don't think it's going to help but it just says Parse error: syntax error, unexpected T_IF in /Applications/XAMPP/xamppfiles/htdocs/webalizer/search_array.php on line 17 Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 <form method="post" action=""> Name: <input type="text" name="num1" size="10"/> <br /> <br /> <input type="Submit" name="Submit" value="Look up name" /> </form> <? $name=$_POST['num1']; $array=array('Al', 'Bob', 'Carl','Don' ,'Eric' ,'Fred' ,'Greg', 'Hank', 'Irene' ,'Judy'); c if(in_array($name, $array)) { echo "Found"; } else { echo "Not Found"; } ?> Quote Link to comment Share on other sites More sharing options...
akitchin Posted October 27, 2009 Share Posted October 27, 2009 ... what is that lonely "c" doing right in the middle of that section? that's why the T_IF is unexpected - it is tripping up when you just tell it to do "c" especially without a semicolon. Quote Link to comment Share on other sites More sharing options...
mrMarcus Posted October 27, 2009 Share Posted October 27, 2009 I don't think it's going to help but it just says Parse error: syntax error, unexpected T_IF in /Applications/XAMPP/xamppfiles/htdocs/webalizer/search_array.php on line 17 of course it helps .. without it, it becomes a guessing game. the error states the exact issue at hand, 'unexpected T_IF'. make sure to always post error messages exactly as seen on screen to get more direct assistance. it's like going into the doctors, saying 'fix me doc', and when asked what the error is, you say, 'it won't help'. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks akitchin! After I deleted that, it worked. I don't know why I had that in there. Quote Link to comment Share on other sites More sharing options...
kaveman50 Posted October 27, 2009 Author Share Posted October 27, 2009 Thanks everyone Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.