/    Sign up×
Community /Pin to ProfileBookmark

associative array (movies)

Can some help me start out doing this problem I have. Thanks

Write a PHP script that creates an associative array to contain movies and the year they won the Academy Award. Use the data in the table below, or add to it as you like!

Year Academy Award Winner
1977 Annie Hall
1956 Around The World in 80 Days
2008 Curious Case of Benjamin Button
1990 Dances With Wolves
2006 The Departed
2005 Crash
1943 Casablanca
1981 Chariots of Fire
1970 Patton

Create the array so that you can look up the Academy Award winner by supplying the year.

Display your array using the print_r() function. Be sure the output is readable (not a single line of text!)

Sort the array in ascending order by year and display the results using a foreach loop

Sort the array in descending order by movie title and display the results using a foreach loop.

Create an Academy Award trivia test using your array as a database. The project contains 2 PHP files: index.php, and AcademyAward.php. The index.php file displays a movie title selected randomly from your array and provides a text field for the user to enter a year.

Figure 1. Opening Screen

The text field displaying the movie title needs to be unchangeable by the user. Use the readonly attribute to prevent users from messing with the title.

When the user presses the “Get Result” button, the movie title and the user’s guess are sent to the AcademyAward.php scrip. That script must verify that the user entered something in the year text field and that the entry is numeric. This script will look up the year the movie won the award and compare it to the user’s guess. If the user is wrong, a page similar to Figure 2 is displayed.

Figure 2. Incorrect Guess
For a correct guess, a page similar to Figure 3 should be displayed.

Figure 3. Correct Guess

to post a comment
PHP

1 Comments(s)

Copy linkTweet thisAlerts:
@SyCoApr 22.2009 — Dude you're not asking for help you're asking for someone to write your application for you. Here's enough info you you to be able to do it all yourself.

Create and array

[code=php]$ozkars['1977']='Annie Hall';
$ozkars['1956']='Around The World in 80 Days';
//etc[/code]


select a particular year
[code=php]$year='1977';
foreach ($ozkars as $key => $value){
if($key==$year) echo $value;
}[/code]


print_r() is a debuging function not a display function but to answer your quesion you can use the <pre> html tag.

[code=php]echo '<pre>'.print_r($ozkars).'</pre>';[/code]

Sort using krsort() or ksort
[code=php]krsort($ozkars);
[/code]

natsort() to sort on movie names. foreach() in the normal way to display the ordered values
[code=php]
foreach ($ozkars as $key => $value){
echo $key . ' - '. $value . '<br />';
}
[/code]


select a random movie
[code=php]$rand_key=array_rand($ozkars,1);
echo $ozkars[$rand_key];[/code]



The text field displaying the movie title needs to be unchangeable by the user. Use the read only attribute to prevent users from messing with the title.[/QUOTE]

Why not just just display it and put the value in a hidden input.

To create your triva quiz store the year and title to a session variable same as the $ozkars array was made. Compare after the guess using the method shown to display a year.
×

Success!

Help @ajpenn215 spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.20,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...