Jump to content

Novice Question.


ktstowell

Recommended Posts

Hi All,

I'm extremely new to developing but am taking an interest in it rapidly. I've written this simple app that has a user guess a number in the given array and then gives the user clues to get the rest of them. No real purpose except for learning and progression for me the author. I run into a problem however upon loading the page for the first time, my $findNumber var has not been defined yet, and prints a line of code to the browser:

 

"Notice: Undefined index: number in C:\wamp\www\sandbox\array_finder.php  on line 31"

I've experimented with empty() and isset() and just can't seem to avoid this issue, any help would be appreciated.

 

Here is the app in full:

<html>
<head>
	<title>Constants</basic>
</head>
<body>

<br />
<form   method="post">
<input  name="number" type="text" </input>
<input  name="submit" type ="submit" </input>
</form>

<?php
if (isset($_POST['Sumbit'])) {
        echo "Try another number!";
} else {
	echo "Please enter a number." . "<br />";
}
if(empty($findNumber)) {
	$findNumber = 0;
}
?>
<br />
<?php

$array1 = array(5,9,10,45,98,101);
$maxArray = max($array1);
$minArray = min($array1);
$findNumber = $_POST['number'];




if(in_array($findNumber, $array1) == true) {
	echo "Yes, that number is in the array." . "<br />";
} else {
	echo "No, that number is not in the array." . "<br />";
}
if((max($array1) == $findNumber)) {
	echo "That is the largest number in the array." . "<br />";
} else {
	echo "No, that is not the largest number in the array" . "<br />";
}
if((min($array1) == $findNumber)) {
	echo "Yes, that is the smallest number in the array." . "<br />";
} else {
	echo " No, that is not the smallest number in the array." . "<br />";
}

?>
</body>
</html>

Thank you for any help received.

 

ktstowell

Link to comment
Share on other sites

  $findNumber = $_POST['number'];

 

You're still not testing to see if $_POST['...'] exists.

 

if (isset($_POST['number'])) {
   $findNumber = $_POST['number'];
} else {
   $findNumber = 0; //Or whatever you wish to do, such as exit() or trigger_error to tell no number is defined.
}

 

Also:

  if(empty($findNumber)) {

      $findNumber = 0;

  }

 

$findNumber is not defined until later on in the script, you can't access it yet.

Link to comment
Share on other sites

Thank you Oni - Kun,

I am still getting the undefined index error, however, here is how i implemented your response:

 

<html>

<head>

<title>Array Finder</basic>

</head>

<body>

<h2>Geuss all the numbers in the array!</h2>

 

<br />

<form  method="post">

<input  name="number" type="text" </input>

<input  name="submit" type ="submit" </input>

</form>

<br />

 

<?php

 

if (isset($_POST['Sumbit'])) {

echo "Try another number!";

} else {

echo "Please enter a number." . "<br />";

}

if (isset($_POST['number'])) {

$findNumber = $_POST['number'];

        } else {

$findNumber = "0";

}

$array1 = array(5,9,10,45,98,101);

$maxArray = max($array1);

$minArray = min($array1);

$findNumber = $_POST['number'];

 

echo "<br />";

 

if(in_array($findNumber, $array1) == true) {

echo "Yes, that number is in the array." . "<br />";

} else {

echo "No, that number is not in the array." . "<br />";

}

if((max($array1) == $findNumber)) {

echo "That is the largest number in the array." . "<br />";

} else {

echo "No, that is not the largest number in the array" . "<br />";

}

if((min($array1) == $findNumber)) {

echo "Yes, that is the smallest number in the array." . "<br />";

} else {

echo " No, that is not the smallest number in the array." . "<br />";

}

 

?>

</body>

</html>

 

I must be doing something wrong here, again, all advice appreciated.

 

ktstowell

Link to comment
Share on other sites

You're still getting the error because there is another line of code where you accesa a $_POST variable but don't check if it exists first, look in your script for:

 

  $findNumber = $_POST['number'];

 

You need to do what you have done on the other $fineNumber line. Either

 

if (isset($_POST['number'])) {
   $findNumber = $_POST['number'];
} else {
   $findNumber = 0; //Or whatever you wish to do, such as exit() or trigger_error to tell no number is defined.
}

or shorten it to

$findNumber = isset($_POST['number']) ? $_POST['number'] : 0;

Link to comment
Share on other sites

Your code is full of many errors and typos, I put an action on the <form> element (as required for POST) and put your code in a proper IF statement:

 

<html>
   <head>
      <title>Array Finder</basic>
   </head>
   <body>
      <h2>Geuss all the numbers in the array!</h2>
   
   <br />
   <form action="<?php print $_SERVER['SCRIPT_NAME']; ?>" method="POST">
    <input name="number" type="input"> </input>
    <input name="submit" type="submit"> </input>
   </form>
   <br />
   
   <?php
   
   if (isset($_POST['number'])) {
    $array1 = array(5,9,10,45,98,101);
    $maxArray = max($array1);
    $minArray = min($array1);
    $findNumber = $_POST['number'];
   
    echo "<br />";
   
    if(in_array($findNumber, $array1) == true) {
        echo "Yes, that number is in the array." . "<br />";
    } else {
        echo "No, that number is not in the array." . "<br />";
    }
    if((max($array1) == $findNumber)) {
        echo "That is the largest number in the array." . "<br />";
    } else {
        echo "No, that is not the largest number in the array" . "<br />";
    }
    if((min($array1) == $findNumber)) {
        echo "Yes, that is the smallest number in the array." . "<br />";
    } else {
        echo " No, that is not the smallest number in the array." . "<br />";
    }
   
  } else {
      echo "Please enter a number." . "<br />";
   }

   ?>
   </body>
   </html>

 

That should fix your problem, it works.

Link to comment
Share on other sites

Thank you!

I finally realized that  with the:

 

if (isset($_POST['number'])) {

$findNumber = $_POST['number'];

        } else {

$findNumber = "0";

}

...bit you gave me, I'm still creating $findNumber via this isset() control structure, so having $findNumber = $_POST['number'] written a second time was the error line. Thank you for all of your help, taking all criticism and advice, as I am a brand new student of PHP and programming/scripting in general.

 

ktstowell

Link to comment
Share on other sites

When I looked at the code I didn't see you did it twice. It's always good to check the variables at the beginning (in my example, the isset on $_POST['number']) as you can avoid running ALL that code unless they submit the form.

 

If you look, it nicely goes to the ELSE statement and asks for the number, not running any of the code that doesn't need to be ran.

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.