Below is what I'm using to create flashcards. It works. However, if I want to add more categories to select from I have to add more "elseif" and "or" 's. Is there a better way to consolidate the code so whether a person chooses one or all of the categories they will get the same result?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<link rel="stylesheet" type="text/css" href="/css/standard.css" />
<title>My Site</title>
<center>
<font size="5">
<? {
mysql_connect;
mysql_select_db;
if (isset($_POST['question'])) {
if ($_POST['selection'] == 'All') {
$query = "Select id, question from master ORDER BY RAND() LIMIT 0,1";
$all= $_POST['selection'];
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$question = $row["question"];
$id = $row["id"];
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
echo '<p>Q: ' . $question;
?>
<div id="miniwrap">
<form action="index.php" method="POST"> <input type="hidden" name="answer" value="<? echo $id; ?>" /> <input type="hidden" name="all" value="<? echo $all; ?>" />
<input type="submit" value="Get Answer" /> </form>
</div>
<?
}
}
elseif ($_POST['selection'] == 'General')
{
$options = $_POST['selection'];
$query = "Select id, question from master WHERE category1 = '$options' OR category2 = '$options' OR category3 = '$options' ORDER BY RAND() LIMIT 0,1";
$general = $_POST['selection'];
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$question = $row["question"];
$id = $row["id"];
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
echo '<p>Q: ' . $question;
?>
<div id="miniwrap">
<form action="index.php" method="POST"> <input type="hidden" name="answer" value="<? echo $id; ?>" /> <input type="hidden" name="general" value="<? echo $general; ?>" /> <input type="hidden" name="iv" value="<? echo $iv; ?>" /> <input type="hidden" name="ekg" value="<? echo $ekg; ?>" />
<input type="submit" value="Get Answer" /> </form>
</div>
<?
}
}
elseif ($_POST['selection'] == 'IV')
{
$options = $_POST['selection'];
$query = "Select id, question from master WHERE category1 = '$options' OR category2 = '$options' OR category3 = '$options' ORDER BY RAND() LIMIT 0,1";
$iv = $_POST['selection'];
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$question = $row["question"];
$id = $row["id"];
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
echo '<p>Q: ' . $question;
?>
<div id="miniwrap">
<form action="index.php" method="POST"> <input type="hidden" name="answer" value="<? echo $id; ?>" /> <input type="hidden" name="general" value="<? echo $general; ?>" /> <input type="hidden" name="iv" value="<? echo $iv; ?>" /> <input type="hidden" name="ekg" value="<? echo $ekg; ?>" />
<input type="submit" value="Get Answer" /> </form>
</div>
<?
}
}
elseif ($_POST['selection'] == 'EKG')
{
$options = $_POST['selection'];
$query = "Select id, question from master WHERE category1 = '$options' OR category2 = '$options' OR category3 = '$options' ORDER BY RAND() LIMIT 0,1";
$ekg = $_POST['selection'];
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$question = $row["question"];
$id = $row["id"];
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
echo '<p>Q: ' . $question;
?>
<div id="miniwrap">
<form action="index.php" method="POST"> <input type="hidden" name="answer" value="<? echo $id; ?>" /> <input type="hidden" name="general" value="<? echo $general; ?>" /> <input type="hidden" name="iv" value="<? echo $iv; ?>" /> <input type="hidden" name="ekg" value="<? echo $ekg; ?>" />
<input type="submit" value="Get Answer" /> </form>
</div>
<?
}
}
else {
echo 'I skipped to the bottom';
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
?>
Make a PROPER selection!
<form action="index.php" method="POST"> <input type="hidden" name="question" value="set" /> <input type="radio" name="selection" value="All" checked> All <input type="radio" name="selection" value="General"> General <input type="radio" name="selection" value="IV"> IV <input type="radio" name="selection" value="EKG"> Basic EKG
<br> <input type="submit" value="Start!" /> </form>
<?
}}
elseif (isset($_POST['answer'])) {
$id = $_POST['answer'];
$all = $_POST['all'];
if (!empty($all)) {$selection = $all;}
$general= $_POST['general'];
if (!empty($general)) {$selection = $general;}
$iv = $_POST['iv'];
if (!empty($iv)) {$selection = $iv;}
$ekg = $_POST['ekg'];
if (!empty($ekg)) {$selection = $ekg;}
$query = "Select answer from master WHERE id = '$id'";
$result = mysql_query($query);
while ($row = mysql_fetch_assoc($result))
{
$answer= $row["answer"];
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
echo '<p>A: ' . $answer;
?>
<div id="miniwrap">
<form action="index.php" method="POST"> <input type="hidden" name="question" value="set" /> <input type="hidden" name="selection" value="<? echo $selection; ?>" />
<input type="submit" value="Next Question!" /> </form>
</div>
<? }}
else {
echo '</head>';
echo '<body>';
echo '<div id="wrap">';
?>
What questions would you like to study?
<form action="index.php" method="POST"> <input type="hidden" name="question" value="set" /> <input type="radio" name="selection" value="All" checked> All <input type="radio" name="selection" value="General"> General <input type="radio" name="selection" value="IV"> IV <input type="radio" name="selection" value="EKG"> Basic EKG
<br> <input type="submit" value="Start!" /> </form>
<?
}
}
?>
</div>
</font>
</body>
</html>