开发者

Using Form Field to Search Php Associative Array

Code:

<?php

echo "<h1>Testing your Trivia</h1>";
$ages['1943'] = "Casablanca";
$age开发者_Go百科s['1956'] = "Around The World in 80 Days";
$ages['1970'] = "Patton";
$ages['1977'] = "Annie Hall";
$ages['1981'] = "Chariots of Fire";
$ages['1990'] = "Dances With Wolves";
$ages['2005'] = "Crash";
$ages['2006'] = "The Departed";

echo "Give the year below won academy award<br>";
echo "<Strong>Movie: </strong> <input type='text' name='' id='' readonly='readonly' /><br>";
echo "<Strong>Year it Won the Oscar: </Strong> <form method='get'><input type='text' name='year' /></form><input type='submit' /> ";

echo '<pre>';
foreach( $ages as $key => $value){
    print_r("Year: $key, Title: $value <br />");
    }
echo '</pre>';

if(isset($_GET['year']))
{
    if(array_key_exists($_GET['year'], $ages))
    {
         echo "<h2>" . $ages[$_GET['year']] . "</h2>";
    }
    else
    {
        echo 'Cannot find data';
    }
}    
?>

Basically trying to have it setup where I can get the movie input to choose a random title and display it in the input field for "movie", then a user has to guess the year it was made. If their too high, it shows a page saying that, if too low it shows an error as well.

I feel like I need to add in another If/Else for if high or too low. Anyone?

Thanks!!


Is there a reason your array keys are strings? It seems like it would make sense in this case for them to be integers.

<?php
$ages['1977'] = "Annie Hall";
$ages['1956'] = "Around The World in 80 Days";
$ages['1990'] = "Dances With Wolves";
$ages['2006'] = "The Departed";
$ages['2005'] = "Crash";
$ages['1943'] = "Casablanca";
$ages['1981'] = "Chariots of Fire";
$ages['1970'] = "Patton";

if(isset($_GET['year']))
{
    if(array_key_exists($_GET['year'], $ages))
    {
         echo $ages[$_GET['year']];
    }
    else
    {
        echo 'Cannot find data';
    }
}    
?>

<form method="GET">
    <input type="text" name="year" value="1984" />
    <input type="submit" />
</form>


If the user is submitting, say, a value from a dropdown menu/list of radio buttons, you could check to see if the $ages array has that year set, and if not, display a default message:

$year = $_GET['year'];
echo isset($ages[$year]) ? $ages[$year] : 'DOES NOT COMPUTE';

Expanded Edit

MVC men will likely put out a hit on me for saying this (if they haven't already), but I like to keep this kind of thing self-contained. That means a page (say, index.php) that looks generally like this:

<?php
// All of your PHP goes here.
?>

<!DOCTYPE html>
<!-- All of your HTML goes here. -->

I'll start with HTML. You'll want a form, like so:

<form action="" method="get">
    <div>
        <label>Movie Year: <input type="text" name="year" />
        <input type="submit" value="Look Up Movie" />
    </div>
</form>

Note that the form's action is left blank. That means that the form will be submitted to the current page. That's where the PHP at the top of your document comes into play.

You'll first want to initialize your array of years and titles:

$ages = array(
    '1977' => 'Annie Hall',
    // ...
    '1970' => 'Patton'
);

Then you check to see if the user submitted the form:

if (isset($_GET['year'])) {
    $year = $_GET['year'];
    $message = isset($ages[$year]) ? 'The film for that year is called '.$ages[$year].'.' : 'There is no film for that year.';
}

This sets the variable $message to some text that you want to display to the user.

Now we make one last jump back to the HTML part of your document, just above the form:

<?php 
if (!empty($message)) {
    echo '<p>', $message, '</p><p>Want to go again?</p>';
}
?>

And there you have it, a searchable array of movie titles, organized by year.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜