I am having trouble with this and do not understand or know PHP. If you could ex
ID: 3816302 • Letter: I
Question
I am having trouble with this and do not understand or know PHP. If you could explain your solution I would appreciate it and will upvote. Thanks
You need to practive how to use PHP to implement a dynamic webpage.
Your webpage should display a random joke and a random quote. Furthermore, each time the page is visited, your script should randomly pick up a different joke and a different quote, which should vary based on the current date or the date provided through query string.
If the current date happens to be a national holiday, you should display a joke related to that holiday in red color, and the quote in green color, also relevant to that holiday. The theme of the joke and quote has to be relevant to the holiday. Choose at least FIVE national holidays. As you know some holidays have dates varing from year to year. For example, Christmas day is a holiday with fixed date, but easter day is NOT. To simplify your life, let us choose only those holidays with fixed dates. Which 5? You pick! If the current date is nothing special, you should display a joke randomly and a quote randomly. Certainly you should prepare pools of data to pick.
For now, you can hard code 10 jokes and 10 quotes inside your script, using array data structures for regular days, hard code 2 jokes and 2 quotes for each holiday.
For each joke, you need to provide a link for where you get the joke. For each quote, you need to provide the people to whom the quote should be attributed.
test your program for different dates, you have to either wait until those special days coming or reset server’s system date to those different special holiday dates in order to test the result, but usually we cannot wait for each special day to come. So that is not a good way to test your solution. Therefore you should also page should also be able to extract date information ( year, month, day) provided through query string, so that, user can test your page by manually providing different dates through query string without resetting server’s time.
Here are some hints for doing this.
Use randomize to initialize the seed of the pseudo random generator, before you use rnd function, use randomize in a single line.
Use if else and array data structures
In the case of regular days, use 4 arrays of the same size, 1 array for jokes, 1 array for the urls of the jokes, 1 array for quotes, 1 array for the authors of quotes
In the case for special holidays, you can consider to fix data or use a set of separate data structures. the same index, randomly picked, controls the joke, url of the joke, quote, the author of the quote
Explanation / Answer
<?php
/*
Any php script outputs HTML and the parts with starting from "<?php" and ending at "?>" is the PHP tag which executes the code inside such a block as PHP. This part is a comment and this will not be executed.
*/
// we choose random holidays
const Christmas = 0;
const Newyear = 1;
const Halloween = 2;
const StPatrick = 3;
const IndependenceDay = 4;
// declare an array of dates which are holidays in mm-dd format
$holidays = array('25-12', '01-01', '31-10', '17-03', '04-07');
# Dummy holiday jokes.. please fill these up
$jokes = array(
array(
array('joke'=>'joke about christmas-1','url'=>'urlchristmas-1', 'quote'=>'quote-christmas-1', 'quote-src'=>'quote-src-christmas-1'),
array('joke'=>'joke about christmas-2','url'=>'urlchristmas-2', 'quote'=>'quote-christmas-2', 'quote-src'=>'quote-src-christmas-2'),
),
array(
array('joke'=>'joke about new-year-1','url'=>'urlnew-year-1', 'quote'=>'quote-new-year-2', 'quote-src'=>'quote-src-new-year-1'),
array('joke'=>'joke about new-year-2','url'=>'urlnew-year-2', 'quote'=>'quote-new-year-2', 'quote-src'=>'quote-src-new-year-2'),
),
array(
array('joke'=>'joke about halloween-1','url'=>'urlhalloween-1', 'quote'=>'quote-halloween-2', 'quote-src'=>'quote-src-halloween-1'),
array('joke'=>'joke about halloween-2','url'=>'urlhalloween-2', 'quote'=>'quote-halloween-2', 'quote-src'=>'quote-src-halloween-2'),
),
array(
array('joke'=>'joke about patrick-1','url'=>'urlpatrick-1', 'quote'=>'quote-patrick-2', 'quote-src'=>'quote-src-patrick-1'),
array('joke'=>'joke about patrick-2','url'=>'urlpatrick-2', 'quote'=>'quote-pattrick-2', 'quote-src'=>'quote-src-patrick-2'),
),
array(
array('joke'=>'joke about freedom-1','url'=>'urlfreedom-1', 'quote'=>'quote-freedom-2', 'quote-src'=>'quote-src-freedom-1'),
array('joke'=>'joke about freedom-2','url'=>'urlfreedom-2', 'quote'=>'quote-freedom-2', 'quote-src'=>'quote-src-freedom-2'),
),
);
# thse are jokes for non-HOLIDAYS
$unspecified_jokes = array(
'Some unspecified joke - 1',
'Some unspecified joke - 2',
);
$unspecified_jokes_url = array(
'url - 1',
'url - 2',
);
$unspecified_quotes = array(
'Some unspecified quote - 1',
'Some unspecified quote - 2',
);
$unspecified_quotes_url = array(
'Some unspecified quote url - 1',
'Some unspecified quote url - 2',
);
// get current time
$time_now = time();
// get today's date in mm-dd format
$today = date("m-d");
// if the user has supplied a date, then use that.
if(isset($_GET['date'])) {
$today = $_GET['date'];
}
// seed the random number generator.. so that we get dfferent random numbers each time
srand($time_now);
// check if current date is a holiday...
if(in_array($today, $holidays)) {
// if the current date is holiday then display appropriate joke
$position = array_search($today, $holidays);
// get a random number between 0 and 1 (inclusive)
$randomNumber = rand(0, 1);
// get the joke data
$data = $jokes[$position][$randomNumber];
?>
<div color='red'>
<?php echo $data['joke']; ?> <br/>
<?php echo $data['url']; ?>
</div>
<div color='green'>
<?php echo $data['quote']; ?> <br/>
<?php echo $data['quote-url']; ?>
</div>
<?php
}
else {
// if current date is not a holiday, just display some random joke
$randomNumber = rand(0,1);
?>
<?php echo $unspecified_jokes[$randomNumber]; ?> <br/>
<?php echo $unspecified_jokes_url[$randomNumber]; ?> <br/>
<?php echo $unspecified_quotes[$randomNumber]; ?> <br/>
<?php echo $unspecified_quotes_url[$randomNumber]; ?>
<?php
}
?>
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.