Jump to content

[SOLVED] Array Error?


kaveman50

Recommended Posts

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";]}?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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";
}

?>

 

Link to comment
Share on other sites

 

 

<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";
}

?>

Link to comment
Share on other sites

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'.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.