简体   繁体   中英

PHP scripting with loops, elseif statements

If someone could just talk to me and maybe point me in a direction. I work better if I have someone who understands it guiding me that I can talk to. Im not asking for it to be done for me. Thank you

The array below represents the item number of the items a small company has in stock.

$inventory = array ("ABC123X","TWX325R","NPR779N","PUY343S","MIS394A","RSE874W","WER343P");

Form:
Write an HTML form that accepts a single text input of an inventory number. (ALREADY DONE: Will be using $item = $_POST["item"]; in the script

PHP Script: Write a PHP script that searches the array.

If the item is in stock (is in the array), print the item in stock message.

If the item is not in stock (not in the array), print the item not in stock message.

Hints:

  1. Use a foreach loop to search your array for the data sent to you by the user. Use a sequential search. (eg: Compare the first item, then the next, then the next, until you find the item you are seeking.)

  2. You will need to nest an if-statement in your loop since not every item will be a match.

  3. Create a flag variable that tracks whether or not an item is found. Set $flag = false at the beginning of your program. Set $flag to true ONLY if the item is found. At the end of your program, test $flag to determine whether or not you need to display the item not in stock message.

I have the following which is bits and peices but I am trying to figure out how to put it all together. I am basically doing this on my own with little information and have tried googling it

<?php

$item = $_POST["item"];
$inventory = array("ABC123X","TWX325R","NPR779N","PUY343S","MIS394A","RSE874W","WER343P")
$flag = false

foreach ($item as $inventory)

if ($flag == true)
 print "<font size=+3><b>ACME Hardware Store</b></font><br><br>";
 print "We have item $item in stock! Feel free to contact us for more info.";
else
if ($flag == false)
 print "<font size=+3><b>ACME Hardware Store</b></font><br><br>";
 print "That item not in inventory! Please contact us if you would like to special     order it.";

?>

If what you need to check is if the item is there in inventory array, just try. Or if you should use foreach check the php manual for the syntax

if(in_array($item,$inventory)){
print "<font size=+3><b>ACME Hardware Store</b></font><br><br>";
 print "We have item $item in stock! Feel free to contact us for more info.";
}
else
{
 print "<font size=+3><b>ACME Hardware Store</b></font><br><br>";
 print "That item not in inventory! Please contact us if you would like to special     order it.";


}

This sounds too simple, but I believe you are just looking for the in_array function.

http://us3.php.net/manual/en/function.in-array.php

But if you really want to do it your way, I believe this would be the correct way:

foreach( $inventory as $inv_item ){
   if( $inv_item == $item ){
      $flag = true;
      break;
   }
}

if( $flag == true ) {
   ...
} else {
   ...
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM