简体   繁体   中英

PHP Table using HTML

My goal is to use a txt file which has the data needed for a specific response from filling out a form. The form is supposed to have a username and password. Also, there is to be a price range parameters that can be set on the form. The username can be written in any case however the password is case sensitive. I have created the html form that I will be using. The data on the txt file contains addresses' for listings and the listings price. The price range parameters can be set to any values, the goal is to just return the data from the txt file as a generated response for completion of the form. The form I have created is,

<!DOCTYPE html>

<html>
<head>
<title></title>
</head>
<body>
<form action="houseprices.txt"
      method="post">
Show houses whose price is between:
<input name="lowprice" type="text" size="15" maxlength="15" /> & 
<input name="highprice" type="text" size="15" maxlength="15" /><br />
Username:   <input name="user" type="text" size="15" maxlength="15" /><br />
Password:   <input name="pw" type="password" size="15" maxlength="15" /><br />
<input type="submit" value="Show Houses" />
</form>
</body>
</html>

The only valid username is to be

Username: joe [any case(jOE)]

Password: tiger [case sensitive]

When I create the response file, should I use php syntax or use html and insert the php coding within the html document. I am using sublime, that is why I ask which syntax to use. The information must be placed in a two column table when the form returns a page. I have yet to begin writing the response pages code I was hoping to get some help with how I should structure the desired code.The text file that must be returned is this.

$houseprices["123 Elm"]=260000;
$houseprices["96 Otis"]=340000;
$houseprices["9 Windham Place"]=560000;
$houseprices["293 Wilma"]=230000;
$houseprices["6789 Beach"]=800000;
$houseprices["535 Overlook"]=750000;
$houseprices["59 South Lake"]=900000;
$houseprices["19 Ash"]=270000;
$houseprices["6820 Butternut"]=340000;
$houseprices["902 Weston"]=190000;
$houseprices["496 Ervin"]=340000;
$houseprices["316 Pond"]=280000;
$houseprices["7282 Main"]=420000;
$houseprices["561 Billings"]=350000;
$houseprices["117 Peach"]=280000;
$houseprices["2171 Bentley"]=300000;
$houseprices["365 Prescott"]=1120000;
$houseprices["319 Paley"]=310000;
$houseprices["43 Maple"]=150000;
$houseprices["291 Wilson"]=320000;
$houseprices["81 Essex"]=330000;
$houseprices["995 West Lawrence"]=480000;
$houseprices["679 Esther"]=240000;
$houseprices["6890 Patten"]=600000;
$houseprices["53 Lower Pond"]=250000;
$houseprices["4567 Washington"]=170000;
$houseprices["691 Holt"]=1900000;
$houseprices["1234 Main"]=180000;
$houseprices["6 Cherry"]=360000;
$houseprices["639 Perrywinkle"]=190000;
$houseprices["86 Foster"]=1300000;
$houseprices["341 Elm   "]=200000;
$houseprices["5122 Bern"]=350000;
$houseprices["688 Maple"]=210000;
$houseprices["64 Oak"]=640000;
$houseprices["874 Marlin"]=220000;
$houseprices["53 Lake"]=190000;
$houseprices["206 Alban"]=310000;
$houseprices["371 Martha"]=420000;
$houseprices["866 Delaware"]=230000;
$houseprices["342 West Lawrence"]=440000;
$houseprices["63 Ashley"]=450000;
$houseprices["49 Tabler"]=260000;
$houseprices["417 Deermill"]=370000;
$houseprices["38 Holt"]=490000;
$houseprices["689 Aspen"]=300000;
$houseprices["4441 Park"]=210000;
$houseprices["8394 North Lake"]=320000;
$houseprices["722 Perry"]=530000;
$houseprices["99 East Ridge"]=260000;
$houseprices["4374 Elderberry"]=270000;
$houseprices["89 Lubbock"]=370000;
$houseprices["12543 Benson"]=380000;
$houseprices["240 London"]=290000;
$houseprices["745 Park"]=390000;
$houseprices["18 Wilson"]=400000;
$houseprices["11 Westgate"]=540000;
$houseprices["4736 East River"]=250000;
$houseprices["4793 Chauncey Circle"]=370000;
$houseprices["3073 West Main"]=280000;

Notes: • The correct username is joe and the correct password is tiger. The username may be typed in any case and may have leading or trailing spaces after it. However, the password must be typed all lower case with no leading and trailing spaces after it.

• If the user does not type in the correct username and password he or she will receive a webpage with an h3 heading containing a message saying, “Invalid Username or Password.”

• If the user does type the correct username and password he or she will receive a webpage with a table listing all of the houses that are between the prices specified. (Between means greater than or equal to the lower number and less than or equal the higher number.) At the end of this webpage will be a copy of the form so that user can type in another request.

• The PHP output should be formatted as shown in the example. The words, “Here are all the houses whose prices are between $100000 and $500000” should be in an h3 heading inside the of the table (and the dollar signs should be printed before the numbers as shown). The borders of the table should be 2px solid red borders as shown. The heading “Try Again” should be an h3 heading.

<?php
echo "<!DOCTYPE html>";
echo "<html>";
echo "<head>";
echo "<title>Houses</title>";
echo "</head>";
echo "<body>";
echo '<h3> Here are all the houses whose prices are between $100000 and $500000: </h3>';


echo "</body>";
echo "</html>";
?>

I'd start by posting your form to the same page as the form (so you can resuse the form).

Then you would need to put in some PHP logic to show data if there is any, or the error for invalid login.

To show the houses, you can include the text file and then run through your array and look for anything in the range you want. You could use a foreach loop.

<!DOCTYPE html>
<html>
<head>
  <title>Your Form Page</title>
</head>
<body>
<!-- Your HTML STUFF BEFORE WHERE YOU WANT THE TABLE TO BE -->


<?php  // This tag opens up PHP
//Check to see if username & password are correct
if(strcmp($_POST['username]'], 'joe') === 0 && strcasecmp($_POST['password'], 'tiger') === 0) {

  // Include the array from your text file -- as you've structured it as PHP
  require('textfile.txt');

  // Start the table
  echo '<table><tr><th>Address</th><th>Price</th></tr>';

  foreach($houseprices as $address => $price) {
    if($price >= $_POST['lowprice'] && $price <= $_POST['highprice']){
      echo '<tr><td>' . $address . '</td><td>$' . $price . '</td></tr>';
    }
  }

  // Close out the table
  echo '</table>';

} else {
  // Display warning about password/username
  echo '<h2>Incorrect Username or Password</h2>';
}

// The tag below will close php
?>

<!-- The rest of your HTML goes down here -->
<form> ...

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