Why doesn't this php work? [duplicate] - php

This question already has answers here:
Reference - What does this error mean in PHP?
(38 answers)
Closed 7 years ago.
So I got this code from codeacademy. It's supposed combine php, html and css to randomly output a coin-styled div in a while loop as long as the result is heads. However, It's not working, all the php is showing and I have no clue why.
.coin {
height: 50px;
width: 50px;
border-radius: 25px;
background-color: grey;
text-align: center;
font-weight: bold;
font-family: sans-serif;
color: white;
margin: 10px;
display: inline-block;
line-height: 50px;
font-size: 20px;
}
<!DOCTYPE html>
<html>
<head>
<link type='text/css' rel='stylesheet' href='style.css'>
<title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo "<div class="coin">H</div>";
}
else {
echo "<div class="coin">T</div>";
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "<p>There {$verb} {$flipCount} {$last}!</p>";
?>
</body>
</html>

<!DOCTYPE html>
<html>
<head>
<link type='text/css' rel='stylesheet' href='style.css'>
<title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo '<div class="coin">H</div>';
}
else {
echo '<div class="coin">T</div>';
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "<p>There {$verb} {$flipCount} {$last}!</p>";
?>
</body>
</html>
qoute mismatch in echo '<div class="coin">H</div>';

The problem is closing a quotation in the while loop near "div"...
The correct PHP would be:
<?php
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo "<div class='coin'>H</div>";
}
else {
echo "<div class='coin'>T</div>";
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "<p>There {$verb} {$flipCount} {$last}!</p>";
?>
Note the single quote around 'coin'.

Use this:-
<!DOCTYPE html>
<html>
<head>
<link type='text/css' rel='stylesheet' href='style.css'>
<title>More Coin Flips</title>
</head>
<body>
<p>We will keep flipping a coin as long as the result is heads!</p>
<?php
$flipCount = 0;
do {
$flip = rand(0,1);
$flipCount ++;
if ($flip){
echo "<div class='coin'>H</div>";
}
else {
echo "<div class='coin'>T</div>";
}
} while ($flip);
$verb = "were";
$last = "flips";
if ($flipCount == 1) {
$verb = "was";
$last = "flip";
}
echo "<p>There {".$verb."} {".$flipCount."} {".$last."}!</p>";
?>
</body>

Related

How to give alternating table rows different background colors in while loop and CSS (not using :nth-child() selector)

I am trying to put background colors in a PHP while loop table with CSS. I can't find out how to write a code to change the first row and first column (header) colors and also change the color diagonally below from the top left.
I know that the table header can be changed by using <th> in CSS but I'm not sure how to use <th> in PHP. I did some research and the only solution for getting this multiplication table is below.
NOTE nth-child() selector is not allowed.
This is what my code looks like:
<!DOCTYPE html>
<html>
<head>
<title>table</title>
</head>
<body>
<table border = "1">
<?php
$x = 1; //rows
echo "<table border = '1'>\n";
while ( $x <= 12 ) {
echo "\t<tr>\n";
$y = 1;//columns
while ( $y <= 12 ) {
echo "\t\t<td>$x x $y = " . $x * $y . "</td>\n";//the result of the multiplication
$y++;
}
echo "\t</tr>\n";
$x++;
}
echo "</table>";
?>
</tr>
</div>
</table>
</body>
CSS code as below.
<style>
table {
width: 100%;
height: 400px;
font-family: arial, sans-serif;
border-collapse: collapse;
}
tr{
background-color: #FF8A65;
}
</style>
</html>
And this is what it should look like:
Do you mean something like this
<!DOCTYPE html>
<html>
<head>
<title>table</title>
<style>
table {
width: 100%;
height: 400px;
font-family: arial, sans-serif;
border-collapse: collapse;
}
tr{
background-color: #82BFBF;
}
</style>
</head>
<body>
<table border = "1">
<?php
$x = 1; //rows
echo "<table border = '1'>\n";
while ( $x <= 12 ) {
if ( $x == 1)
echo "\t<tr style='background-color:green'>\n";
$y = 1;
while ( $y <= 12 ) {
$bgc = '';
if ( ($y == 1 || $x == 1) && $y != $x) {
$bgc = "style='background-color:green'";
} elseif ( $y == $x ) {
$bgc = "style='background-color:red'";
} elseif ($y > 1 && $x % 2){
$bgc = "style='background-color:#FF8A65'";
}
echo "\t\t<td $bgc>$x x $y = " . $x * $y . "</td>\n";
$y++;
}
echo "\t</tr>\n";
$x++;
}
echo "</table>";
?>
</tr>
</div>
</table>
</body>
</html>

Removing an option from dropbox after selected

I have a state guessing guess I wrote. I'm trying to remove a capital from the dropbox menu after it has been guessed. Any clues on how to go about doing that?
I have a comment on line 44 which is code that I believe may work but I havent been able to get working.
Thanks for reading and thanks in advance!
Heres my code:
<?php
require_once "include/session.php";
if (!isset($session->valid)) {
header("location: login.php");
exit();
}
require_once "include/city_info.php";
/* DO NOT MODIFY THE ABOVE LINES !!!!! */
$states = array_keys($state_capitals);
$capitals = array_values($state_capitals);
$cities = array_merge($capitals, $other_cities);
sort($cities);
$params = (object) $_REQUEST;
if (isset ($params->guess) ) {
$state = $session->statename;
$capital = $params->city;
$city = $params->city;
$session->guess ++ ;
//if they guess correctly
if ($capital == $state_capitals[$session->statename]){
$response = "You've Guessed Correct After $session->guess Guesses";
}
//if they guess incorrectly
else {
$response = " You've Guessed Wrong $session->guess Times";}
}
else {
$index = rand( 0, count($states)- 1 );
$state = $states[ $index ];
$city = $state_capitals[ $state ];
$session->statename = $state;
$guess = (0);
$session->guess = (0);
$response = "Try To Get Less Than 3 Guesses";
// create initial selections
//$session->selections = array();
// foreach($city as $value) {
// $session->selections[$value] = 1;
//}
}
?>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<meta http-equiv="X-UA-Compatible" content="IE=edge"/>
<meta http-equiv="Cache-Control"
content="no-cache, max-age=0, no-store, must-revalidate" />
<title>State Capital Guessing Game</title>
<style type="text/css">
body {
background-color:#ffff4d;
}
div {
position:center;
width: 350px;
padding: 10px;
border: 3px solid black;
border-radius: 10px;
color: black;
background-color:#3399ff;
}
}
h1{
padding:5px;
}
h2 {
text-align: center;
font-size:20px;
}
h3 {
text-align:center;
}
h4 {
text-align:center;
}
h5 {
position: absolute;
top:0px;
right:0px;
}
#logout {
position: absolute;
top: 0px;
right: 0px;
}
</style>
</head>
<body>
<a id="logout" href="logout.php">Log out</a>
<h1>
State Capital Guess Game
<form action="program.php" method="get">
<button type="submit">Reset</button>
</h1>
</form>
<div>
<form action="program.php" method="post">
<input type="hidden" name="answer" value="<?php echo $state?>" />
<h2>
Guess the capital of: <?php echo "$session->state" ?>
</h2>
<h3>
<select name="city">
<?php
$options = "";
foreach ($cities as $value){
$options .= '<option value="' . $value . '"';
if ($value == $params->city)
$options .= ' selected';
$options .= '>' . $value . '</option>';
}
echo $options;
?>
</select>
<input type="submit" name="guess" value="Guess" />
</form>
</h3>
<h4><?php echo $response ?></h4>
</div>
<h5><?php echo "The Correct Answer Is: $state_capitals[$state]" ?><h5>
</body>
</html>​

How to print PHP file contents in PHP? [closed]

It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center.
Closed 10 years ago.
Since I am new to PHP I want to know how to print a file content in PHP?
Actually my project consist of form from which data is choosed to print on a report but I want a print button on my form to print the report contents directly. Can anyone help please?
Here's my code .. I have used CSS, html and PHP and it has two buttons view and print
<?php
if ($_POST["submit"] == "View") {
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<head>
<style type="text/css" media="all">
* {
margin:0px;
padding:0;
}
body {
margin:0px;
padding:0px;
font-family:Verdana, Arial, Helvetica, sans-serif;
font-size:12px;
}
span {
font-size:12px;
}
th, td {
width: 100mm;
height: 33.5mm;
}
table {
border-collapse:collapse;
}
</style>
<title>Hotel Label - <?php echo $_SESSION["tourname"]; ?></title>
</head>
<body>
<div style="margin:0px; padding:0; width:210mm; margin-top:12mm; margin-bottom:12mm; margin-left:0.5cm; margin-right:0cm; ">
<?php
//echo $getno;
/* } // View Tag Close
else {
ini_set("memory_limit", "50M");
ob_start();*/
?>
<style type="text/css" media="all">
<!--
body {
margin:0px;
font-family:Verdana, Arial, helvetica, sans-serif;
font-size:12px;
}
span {
font-size:12px;
}
td {
width: 100mm;
max-width: 100mm;
height: 33.5mm;
max-height:33.5mm;
top:0mm;
}
table {
border-collapse:collapse;
}
-->
</style>
<page backtop="13mm" backbottom="1mm" backleft="5mm" backright="5mm" style="font-size: 10px; font-family: Verdana;">
<?php
?>
<?php
$ctr16 = 0;
$ctr16_2 = 0;
$nctr = 0;
$ctr = 0;
$mctr = 0;
$lblctr = 0;
while ($lbl = mysql_fetch_assoc($get)) {
$ctr16++;
$ctr16_2++;
$nctr++;
$ctr++;
$lblctr++;
$getno--;
if ($ctr == 1) {
echo "<table border=\"0\" align=\"left\" style =\"width: 210mm;\">";
echo "\n <tr> \n ";
echo "\n <tr> \n ";
echo "\n <td style=\"width: 100mm; max-width: 100mm; height:33.5mm; padding-left:1mm;\" valign=\"top\"> \n";
if ($lblctr > 16) {
echo "<br>";
echo "<br>";
$lblctr = 1;
}
if ($ctr16 > 64) {
echo "<br>";
echo "<br>";
$ctr16 = 1;
}
}
else {
echo "\n <td style=\"width: 100mm; max-width: 100mm; height:33.5mm; padding-left:15mm;\" valign=\"top\"> \n";
if ($nctr > 16) {
echo "<br>";
echo "<br>";
$nctr = 1;
}
if ($ctr16_2 > 64) {
echo "<br>";
echo "<br>";
$ctr16_2 = 1;
}
}
echo "<br />". "<span><font size='3px'><b>$lbl[hotelname]</b></font>";
// echo "<font size='2px'>$lbl[hotelname]";
echo "<br />"."<br />"."<font size='2px'>Haji No.-$lbl[hajino]</font>";
echo "<br />"."$lbl[suffix] $lbl[surname] $lbl[name]$lbl[midname] <br />";
echo "<br />"."<font size='2px'><b>Room No - $lbl[roomno] Bus No - $lbl[busname]</font></b>";
echo "<br />";
echo "<br />";
echo "<br />";
echo "</span> ";
// echo "<br />";
echo "</td>";
if ($ctr == 2 || $getno == 0) {
echo '<br />';
$ctr = 0;
echo "</table>";
}
}
mysql_free_result($get);
?>
</div>
</body>
</html>
<?php
} else($_POST["submit"]== "Print"){
$filecontents = file_get_contents("hotel_label_print.php");
print $filecontents;
} ?>
You can use file_get_contents()
(http://php.net/manual/en/function.file-get-contents.php)
<?php
$homepage = file_get_contents('filename.txt');
echo $homepage;
?>

SQL/PHP query works in PHPmyAdmin but not the site

SQL/PHP query works in PHPmyAdmin but not the site.
I notice that many have had this problem but admittedly I am not as advanced as some of the coders on this site...yet. =) I humbly request any experience you may have laying around :P Thank you.
<?php
// session_start();
// ob_start();
ini_set("display_errors", true);
error_reporting(-1);
// Connection to database.
mysql_connect('localhost', 'root', '') or die(mysql_error());
mysql_select_db('') or die(mysql_error());
?>
<?php
// Maintenance page.
$maintenance = 1; // 1 = Site Online || 0 = Site Offline
if ($maintenance == 0) {
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<style type="text/css">
body {
color:#ffffff;
background-color: #000000;
font-family: Arial, Helvetica, sans-serif;
}
</style>
</head>
<title></title>
</head>
<body>
<center><img src="images/p4flogo.png" /></center><br />
<?php
echo "<br/><br /><center>This site is currently under maintenance.</center>";
} else {
// Start of main website.
?>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<head>
<style type="text/css">
body {
color:#ffffff;
background-color: #000000;
font-family: Arial, Helvetica, sans-serif;
}
a:link {color: orange; text-decoration: underline; }
a:active {color: red; text-decoration: underline; }
a:visited {color: orange; text-decoration: underline; }
a:hover {color: red; text-decoration: none; }
table {
border-color: #333333;
}
th {
background-color:#ffffff;
color:#000000;
font-family: Arial, Helvetica, sans-serif;
height:30px;
}
td { font-family: Arial, Helvetica, sans-serif;
color:#ffffff;
height:35px;
}
</style>
</head>
<title></title>
</head>
<body>
<table border="0" cellspacing="1" align="center">
<tr><td>
<center><img src="images/p4flogo.png" /></center><br /><br />
<form action="" method="post">
Search for a soldier: <input type="text" name="value" size="35" /><input type="submit" value="search" /><br />
<?php
if (isset($_POST['value']) && !empty($_POST['value'])) {
$value = mysql_real_escape_string($_POST['value']);
// query to database for soldier stats
// query works in phpmyadmin but not on site.
$sql = "
SELECT
`Name`,
MAX(`Level`),
`Class`,
SUM(`Kills`),
SUM(`Deaths`),
SUM(`Points`),
SUM(`TotalTime`),
SUM(`TotalVisits`),
`CharacterID`
FROM
`Characters`
WHERE
`Name` LIKE '$value%' OR `CharacterID` LIKE '$value'
GROUP BY
`Name`,
`Class`,
`CharacterID`
ORDER BY
`Name` ASC;";
$query = mysql_query($sql);
$numrow = mysql_num_rows($query);
if ($numrow >= 1) {
echo "<br /><b>View TOP 100 Players!</b><br />";
echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\"> ";
echo "<th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";
echo "<br />";
WHILE ($row = mysql_fetch_assoc($query)) {
$SoldierName = $row['Name'];
$Level = $row['Level'];
$Class = $row['Class'];
if ($Class == NULL | empty($Class)) {
$Class = '<center>n / a</center>';
}
if ($Class == 1) {
$Class = 'Assault';
}
if ($Class == 2) {
$Class = 'Recon';
}
if ($Class == 3) {
$Class = 'Medic';
}
if ($Class == 4) {
$Class = 'Engineer';
}
echo $Kills = $row['Kills'];
if ($Kills == 0) {
$Kills = 1;
}
$Deaths = $row['Deaths'];
if ($Deaths == 0) {
$Deaths = 1;
}
$Kdr = round($Kills / $Deaths, 2);
$Points = $row['Points'];
$TimePlayed = $row['TotalTime'];
if ($TimePlayed == 0) {
$TimePlayed = 1;
} else {
$TimePlayed = round(($TimePlayed / 3600), 0);
}
$TotalVisits = $row['TotalVisits'];
$CharacterID = $row['CharacterID'];
echo "<tr>";
echo "<td><b>$SoldierName</b></td>";
echo "<td>$Level</td>";
echo "<td>$Class</td>";
if ($Kdr > 3.9) {
echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
} else if ($Kdr > 2.5 && $Kdr < 4) {
echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
} else {
echo "<td><font color=\"limegreen\">$Kdr</font></td>";
}
echo "<td>$Kills</td>";
echo "<td>$Deaths</td>";
echo "<td>$Points</td>";
echo "<td>$TimePlayed</td>";
echo "<td>$TotalVisits</td>";
echo "<td>$CharacterID</td>";
echo "</tr>";
}
echo "</table>";
} else {
echo "No player found with that name. Please try again.";
}
} else {
if (empty($_POST['value'])) {
echo "<font color=\"red\">You must enter a search value.</font>";
}
// query to p4f database for top 100 players.
$sql = "SELECT * FROM `Characters` WHERE `Points` > 1 GROUP BY `Name` ORDER BY `Points` DESC LIMIT 100;";
$query = mysql_query($sql);
echo "<h3>TOP 100 PLAYERS</h3>";
echo "<table border=\"1\" cellspacing=\"0\" cellpadding=\"5\"> ";
echo "<th></th><th>Soldier Name</th><th>Level</th><th>Class</th><th>KDR</th><th>Kills</th><th>Deaths</th><th>Points</th><th>Hours Played</th><th>Total Visits</th><th>CharacterID</th>";
// echo "Made it to loop!";
$Rank = 1;
WHILE ($row = mysql_fetch_assoc($query)) {
$SoldierName = $row['Name'];
$Level = $row['Level'];
$Class = $row['Class'];
if ($Class == NULL | empty($Class)) {
$Class = '<center>n / a</center>';
}
if ($Class == 1) {
$Class = 'Assault';
}
if ($Class == 2) {
$Class = 'Recon';
}
if ($Class == 3) {
$Class = 'Medic';
}
if ($Class == 4) {
$Class = 'Engineer';
}
$Kills = $row['Kills'];
if ($Kills == 0) {
$Kills = 1;
}
$Deaths = $row['Deaths'];
if ($Deaths == 0) {
$Deaths = 1;
}
$Kdr = round($Kills / $Deaths, 2);
$Points = $row['Points'];
$TimePlayed = $row['TotalTime'];
if ($TimePlayed == 0) {
$TimePlayed = 1;
} else {
$TimePlayed = round(($TimePlayed / 3600), 0);
}
$TotalVisits = $row['TotalVisits'];
$CharacterID = $row['CharacterID'];
echo "<tr>";
echo "<td>$Rank</td>";
echo "<td><b>$SoldierName</b></td>";
echo "<td>$Level</td>";
echo "<td>$Class</td>";
if ($Kdr > 3.9) {
echo "<td><font color=\"red\"><b>$Kdr</b></font></td>";
} else if ($Kdr > 2.5 && $Kdr < 4) {
echo "<td><font color=\"orange\"><b>$Kdr</b></font></td>";
} else {
echo "<td><font color=\"limegreen\">$Kdr</font></td>";
}
echo "<td>$Kills</td>";
echo "<td>$Deaths</td>";
echo "<td>$Points</td>";
echo "<td>$TimePlayed</td>";
echo "<td>$TotalVisits</td>";
echo "<td>$CharacterID</td>";
echo "</tr>";
$Rank++;
}
echo "</table>";
}
}
?>
</td></tr>
</table>
</body>
</html>
I'm not sure about MySQL, but I know that in SQL when you do an aggregate function, like SUM(Kills), then you can't reference the row via $row['kills']. I don't know if this is your problem, but you could try doing SUM(Kills) as 'kills' in your SELECT statement. Doing this for your aggregate SELECTs will allow you to reference them all this way.

Decision Tree - Must be a better way...?

Below is my way, albeit a basic way of creating a Decision Tree...The problem is the CSS is getting way out of control and the code is just awful...is there a better way?
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
<style>
#SwitchPicker .title{
width: 100%;
margin:auto;
text-align:center;
}
#SwitchPicker .twinanswer{
width:400px;
background:url(images/2ans.jpg) no-repeat;
margin:auto;
height:110px;
}
#SwitchPicker .answer-bottom-left {
padding-top:90px;
width:50%;
float:left;
}
#SwitchPicker .answer-bottom-right {
padding-top:90px;
width:50%;
float:left;
text-align:right;
}
#SwitchPicker .trioanswer{
width:400px;
background:url(images/3ans.jpg) no-repeat;
margin:auto;
height:110px;
}
#SwitchPicker .trio-answer-bottom-left {
padding-top:90px;
width:33%;
float:left;
}
#SwitchPicker .trio-answer-bottom-mid {
padding-top:90px;
width:33%;
float:left;
text-align:center;
}
#SwitchPicker .trio-answer-bottom-right {
padding-top:90px;
width:33%;
float:left;
text-align:right;
}
#SwitchPicker .answer{
width:50%;
float:left;
margin:auto;
text-align:center;
}
#SwitchPicker .answers{
width:40%;
padding-left:30%;
padding-right:30%;
margin:auto;
}
#SwitchPicker .detail {
display:none;
}
#SwitchPicker .answer-left {
margin-top:10px;
width:50%;
padding-left:13%;
}
#SwitchPicker .answer-right {
margin-top:10px;
width:50%;
padding-left:37%;
}
#SwitchPicker .answer-left-left {
margin-top:10px;
width:50%;
padding-left:3%;
}
#SwitchPicker .answer-left-right {
margin-top:10px;
width:50%;
padding-left:23%;
}
#SwitchPicker .answer-mid-left {
margin-top:10px;
width:50%;
padding-left:14%;
}
#SwitchPicker .answer-mid-right {
margin-top:10px;
width:50%;
padding-left:36%;
}
#SwitchPicker .answer-right-left {
margin-top:10px;
width:50%;
padding-left:27%;
}
#SwitchPicker .answer-right-right {
margin-top:10px;
width:50%;
padding-left:48%;
}
</style>
</head>
<?php
// Managed or Unmanaged
if ($_GET['st'] == "managed"){
$a1 = "answer-left";
$switch_type = $_GET['st'];
}
else if ($_GET['st'] == "unmanaged"){
$a1 = "answer-right";
$switch_type = $_GET['st'];
}
else if ($_GET['st'] == "smart"){
$a1 = "answer-mid";
$switch_type = $_GET['st'];
}
else {
echo "";
}
// Fast Ethernet or Gigabit
if (($_GET['st'] == "managed") && ($_GET['ss'] == 100)){
$a2 = "answer-left-left";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else if (($_GET['st'] == "managed") && ($_GET['ss'] == 1000)){
$a2 = "answer-left-right";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else if (($_GET['st'] == "unmanaged") && ($_GET['ss'] == 100)){
$a2 = "answer-right-left";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else if (($_GET['st'] == "unmanaged") && ($_GET['ss'] == 1000)){
$a2 = "answer-right-right";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else if (($_GET['st'] == "smart") && ($_GET['ss'] == 100)){
$a2 = "answer-mid-left";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else if (($_GET['st'] == "smart") && ($_GET['ss'] == 1000)){
$a2 = "answer-mid-right";
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
}
else {
echo "";
}
?>
<body id="SwitchPicker">
<div class="title">Small Business Decision Tree</div>
<div class="title">Managed or Unmanaged</div>
<div class="trioanswer">
<div class="trio-answer-bottom-left">Managed</div>
<div class="trio-answer-bottom-mid">Smart</div>
<div class="trio-answer-bottom-right">Unmanaged</div>
</div>
<?
if (isset($_GET['st'])) {
echo"
<div class=\"" .$a1. "\">
<div class=\"title\">Switch Speed?</div>
<div class=\"twinanswer\">
<div class=\"answer-bottom-left\">Fast Ethernet (10/100)</div>
<div class=\"answer-bottom-right\">GigabitEthernet (1000)</div>
</div>
</div>";
}
else {
echo "";
}
?>
<?
if (isset($_GET['ss'])) {
echo"
<div class=\"" .$a2. "\">
<div class=\"title\">Switch Size?</div>
<div class=\"twinanswer\">
<div class=\"answer-bottom-left\">Desktop</div>
<div class=\"answer-bottom-right\">Rack-mountable </div>
</div>
</div>";
}
else {
echo "";
}
?>
</body>
</html>
Thanks,
B.
At first you could take the lines
$switch_type = $_GET['st'];
$switch_speed = $_GET['ss'];
out of the conditions, they are the same everywhere. Just do it once before or after the if/elses.
Furthermore I see, that each sub-part of $a2 depends on a specific condition. Maybe you can construct it the following (pseudo-code) way:
...
//$a2 always starts the same
$a2 = "answer";
// Type of line
switch($_GET['st']) {
case "managed":
$a2 = $a2 + "-left"; break;
case "unmanaged":
$a2 = $a2 + "-right"; break;
case "smart":
$a2 = $a2 + "-mid"; break;
}
// speed of line
switch($_GET['ss']) {
case 100:
$a2 = $a2 + "-left"; break;
case 1000:
$a2 = $a2 + "-right"; break;
}
....
After that $a2 should hold your combined values like 'answer-left-right' and so on. A similar approach could be done with $a1 but I chose $a2 for the example as it's better suited here.
I've used associative arrays for this in the past.
In your case, you should be parsing $_GET["st"] & $_GET["ss"] at the top of the script. So you can grab those into two variables $st and $ss (and you should be doing validation on them as well!). Taken that as given, you can come up with something like:
$st_choices = array (
'managed' => array (
'1000' => array (
'key1' => 'val1',
'key2' => 'val2'
);
),
'a1' => 'answer-left',
'a2' => 'another-value'
/** ... **/
);
Then, you use a lookup function to access the array.
Adding more elements means simply adding to the array in the format above, instead of adding more if/else if/else branches.
function return_details($st, $ss) {
$vals = array();
if (isset($st_choices[$st]) && isset($st_choices[$st][$ss])) {
$vals['a1'] = $st_choices[$st]["a1"];
$vals['a2'] = $st_choices[$st]["a2"];
$vals['key1'] = $vals[$st][$ss]['key1'];
}
}
Depending on your situation, you may need to wrap those assignments in isset() also.

Categories