Displaying results based on user selection
Hello,
I am working with a form whereby results will be displayed based on the day that a user has selected from a dropdown list.
For example, if a user selects Monday or Sunday, the group that will do activities on that day will be displayed. I created arrays for days '$dayOfWeek' and group '$team'. I have been able to make the days appear in a dropdown list.
My problem now is how to make the team that will play on each day appear below it when a particular day is selected. Does anyone know how to fix this or of a better way to do this please?
You can see my code below.
Code:
<!doctype html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>Switching values and record display</title>
</head>
<body>
<?php
//this is the arrays for days of the week
$dayOfWeek = array();
$dayOfWeek['day1'] = "Monday";
$dayOfWeek['day2'] = "Tuesday";
$dayOfWeek['day3'] = "Wednesday";
$dayOfWeek['day4'] = "Thursday";
$dayOfWeek['day5'] = "Friday";
$dayOfWeek['day6'] = "Saturday";
$dayOfWeek['day7'] = "Sunday";
?>
<?php
//the arrays for team
$team = array();
$team ['one'] = "year one, year two, year three";
$team ['two'] = "year four, year five,year six ";
$team ['three'] = "year three, year two, year one";
$team ['four'] = "year one, year four, year six";
$team ['five'] = "year five, year three, year five";
$team ['six'] = "year one to year six";
$team ['seven'] = "year three, year five, year two";
?>
<!--here is the form-->
<form action="" method="post">
<label for="weekDay">Week Day: <br/></label>
<select name="Wday">
<option value="select">Select a week day</option>
<option value="d1"><? echo $dayOfWeek['day1']?></option>
<option value="d2"><? echo $dayOfWeek['day2']?></option>
<option value="d3"><? echo $dayOfWeek['day3']?></option>
<option value="d4"><? echo $dayOfWeek['day4']?></option>
<option value="d5"><? echo $dayOfWeek['day5']?></option>
<option value="d6"><? echo $dayOfWeek['day6']?></option>
<option value="d7"><? echo $dayOfWeek['day7']?></option>
</select>
<label for="Group"> <br/><br/>
Group:</label>
<div>
<?php
//if a user selects a day of the week above, show the teams for that day
if($dayOfWeek['day1']){
echo $team ['one'];
}
?>
</div>
</form>
</body>
</html>
I will appreciate your help please.
Thanks in advance.
Re: Displaying results based on user selection
why dont you study about sql and php. there are lots of site that teach you php and sql. the site where you can study is as follows:
http://www.w3schools.com
Re: Displaying results based on user selection
Like czns said, you could have made use of a db(mysql) for storing the info and then query it when needed. Otherwise, use XML as storage medium.
Your present approach doesn't show a relation between the dayOfWeek and the Team !
Anyway, here's the modified code:
PHP Code:
<!doctype html>
<html lang="en">
<head>
<meta charset=utf-8>
<title>Switching values and record display</title>
</head>
<body>
<?php
//this is the arrays for days of the week
$dayOfWeek = array(); //we are using same "keys" for both the arrays
$dayOfWeek['d1'] = "Monday";
$dayOfWeek['d2'] = "Tuesday";
$dayOfWeek['d3'] = "Wednesday";
$dayOfWeek['d4'] = "Thursday";
$dayOfWeek['d5'] = "Friday";
$dayOfWeek['d6'] = "Saturday";
$dayOfWeek['d7'] = "Sunday";
?>
<?php
//the arrays for team
$team = array(); //we are using same "keys" for both the arrays
$team ['d1'] = "year one, year two, year three";
$team ['d2'] = "year four, year five,year six ";
$team ['d3'] = "year three, year two, year one";
$team ['d4'] = "year one, year four, year six";
$team ['d5'] = "year five, year three, year five";
$team ['d6'] = "year one to year six";
$team ['d7'] = "year three, year five, year two";
?>
<!--here is the form-->
<form action="" method="post">
<label for="weekDay">Week Day: <br/></label>
<select name="Wday">
<option value="select">Select a week day</option>
<?php
//loop through the days and echo it by enclosing it in "<option>" tag
foreach($dayOfWeek as $k=>$v)
{
echo '<option value="'.$k.'">'.$v.'</option>';
} ?>
</select>
<label for="Group"> <br/><br/>
Group:</label>
<div>
<?php
//if a user selects a day of the week above, show the teams for that day
if(isset($_POST['Wday'])) //if user has submitted the form..
{
echo $team[$_POST['Wday']]; //echo the correspnding array element from the $team array, using the key passed
}
?>
</div>
<input type="submit" value="Submit" /> <!-- A submit button to submit the form -->
</form>
</body>
</html>
Note that, I have use the same keys for both the arrays. In that way, it would be easy to display the requested team info for that particular day.
And also, for a form to submit, you need to have a "Submit" button or you could make use of some ajax to dynamically send it when user changes the item from the combobox.
I won't recommend this if you are developing an app. Make use of some storage medium like database or XML file, and query them.
Hope it well help :wave:
Re: Displaying results based on user selection
Thank you very much everyone for your help and suggestions. The code you corrected worked very well. I will now consider PHP and MySQL for further and related tasks.
Once again, thanks.:check:
Re: Displaying results based on user selection
Glad to know that it helped.
:wave: