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

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.

Related

Why wont the chosen option reflect in the PHP code I wrote?

Basically it's supposed to get the user submitted value of the array size, and the method they want to calculate with the array from the drop down. the php successful gets the code from the array size but always sees the field with the dropdown as "mean" no matter which I select.
forms5.html:
`<html>
<head>
</head>
<body style="background-color: rgb(192, 192, 192);">
<div class="container" style="background-color: rgb(165, 165, 165); width: 575px; height: 250px; border: ridge; border-color: rgb(209, 209, 209);">
<form name = "form1" method = "get" action = "actions_5.php">
<div class = "form-group" style=" padding-left: 10px; padding-right: 10px; padding-top: 10px;">
<label><h2>Enter size of an array</h2></label> <br>
<input type = "number" name = "array_size" placeholder="Type in an integer">
</div>
<br>
<div style="padding-left: 10px; padding-right: 10px;">
<label><b>Select an option</b></label>
<select name = "option" class="form-control">
<option value="mean">Calculate Mean</option>
<option value="median">Calculate Median</option>
<option value="mode">Calculate Mode</option>
</select>
</div>
<div style="padding: 10px;">
<input type="submit" class="btn btn-primary">
</div>
</form>
</div>
</body>
</html>'
actions_5.php:
'<?php
session_start();
echo "<html>";
echo "<head>";
echo '<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.1.3/dist/css/bootstrap.min.css" rel="stylesheet"
integrity="sha384-1BmE4kWBq78iYhFldvKuhfTAU6auU8tT94WrHftjDbrCEXSU1oBoqyl2QvZ6jIW3" crossorigin="anonymous">
';
echo "</head>";
echo "<body style='background-color: lightgray'>";
//store variables
$choice = $_GET['option'];
$size = $_GET['array_size'];
//create array
for($i = 0; $i < $size; $i++) {
$array[$i] = mt_rand(1,10);
}
echo "<div style = 'color: green; padding-top: 160px'><center><h3>Initialized array --><h3></center></div>";
echo "<br>";
echo"<center><div style = 'color: red; font-size: 35px; padding-top: 10px'>";
for($i = 0; $i < $size; $i++) {
echo"<b>$array[$i] </b>";
}
echo"</div></center>";
echo"<br>";
//if statements
echo"<center><div style = padding=top: 10px; front-size: 35px>";
if($choice = "mean") {
$mean = array_sum($array) / count($array);
echo"<p style = 'color:crimson'><b>Mean = $mean</b></p>";
} else if($choice = "mode") {
sort($array);
sort($array, $size);
$max = 1;
$mode = $array[0];
$current = 1;
for($i = 0; $i < $size; $i++){
if($array[$i] == $array[$i] - 1){
$current++;
}
else {
if($current > $max) {
$max = $current;
$mode = $array[$i - 1];
}
$current = 1;
}
}
if($current > $max) {
$max = $current;
$mode = $array[$size - 1];
}
if($max = 1) {
echo"<p style = 'color:teal'><b>There is no mode in the set.</b></p>";
} else{
echo"<p style = 'color:teal'><b>Mode = $mode</b></p>";
}
} else {
rsort($array);
$n = count($array) / 2;
$median = $array($n);
echo"<p style = 'color:blue'><b>Median = $median</b></p>";
}
echo"</div></center>";
echo "</body>";
echo "</html>";
?>'

run php code if $players is between 5 and 9 in third span

Ok i edit my question i think i understand more now how to build a question.
The website: bit DOT ly/1MHItEH
I want to run a php code when span 3 is between 5 & 9.
The code which outputs this is $players and in the bottom there is a html output code
<td><span style='text-shadow: 0 0 10px #ffca00; color: #ffca00; font-size: 20pt;'>". $players ."</span></td>
This php code outputs a html table with different values for every added server.
For example if i add a new server to servers.php it will create a new span for that server with new values. So basicly it reads and outputs different values for every server.
So how can i just run extra php code if the span 3 is between 5 & 9?
<html>
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script>
<style>
tr td, tr th { text-align: center !important; }
tr td.motd, tr th.motd { text-align: left !important; }
</style>
</head>
<body style="width: 100%; margin: 0 auto; height: 20px; ">
<table class='table table-bordered table-striped'>
</br>
<?php
error_reporting(0);
function openserveriai() {
}
function closeserveriai() {
}
function getnextstring(&$data) {
$temp="";
$counter=0;
while (ord($data[$counter++])!=0) $temp.=$data[$counter-1];
$data=substr($data,strlen($temp)+1);
return $temp;
}
function getnextbytevalue(&$data) {
$temp=ord($data[0]);
$data=substr($data,1);
return $temp;
}
function addServer($ip) {
$map = '';
$players = '';
$maxplayers = '';
$servername = '';
$output = '';
$live_server = '0';
$packet = '0';
$packet = "\xFF\xFF\xFF\xFFTSource Engine Query\x00";
$live_server = fsockopen("udp://".$ip);
if(!$live_server)
{
$output = "Off";
}
else
{
fwrite($live_server, $packet);
socket_set_timeout($live_server,1,0);
$junk = fread($live_server,5);
$status = socket_get_status($live_server);
$do = 1;
$server_info= "";
while($do)
{
$str_1 = fread($live_server,1);
$server_info .= $str_1;
$status = socket_get_status($live_server);
if($status["unread_bytes"] == 0) {$do = 0;}
}
fclose($live_server);
if (strlen($server_info) > 0)
{
$success = 1;
$junk = getnextstring($server_info);
$servername = getnextstring($server_info);
$map = getnextstring($server_info);
$junk = getnextstring($server_info);
$junk = getnextstring($server_info);
$players = getnextbytevalue($server_info);
}
if ($players != '') {
$players = $players;
} else {
$players = "0";
}
if ($maxplayers != '')
{
$maxplayers = $maxplayers;
}
else
{
$maxplayers = "0";
}
if ($output != "Full" and $players != "0" or $maxplayers != "0")
{
$output = $output;
}
else
{
$output = "<font color='#FF0000'>Offline</font>";
}
if ($map != '')
{
$map = $map;
}
else
{
$map = "--";
$maxplayers = "--";
$players = "--";
}
if ($servername != '') {
$servername = $servername;
}
else
{
$servername = "--";
}
}
if($players == $maxplayers && $players != '--')
{
$players = "" . $players . "";
}
else if($players > $maxplayers-3 && $players != '--')
{
$players = "" . $players . "";
}
else
{
$players = "" . $players . "";
}
if ( strlen($map) > 19 )
{
$map = substr($map, 0, 19) . '...';
}
echo "
<tbody>
<tr style='background: #10130d;'>
";
if ($map == '--')
{
echo "<td><span style='background-color: #b85c5c; border-radius: .25em; padding: .2em .6em .3em; font-weight: bold; color: #fff; font-size: 10pt;'>Offline</i></span></td>";
}
else
{
echo "<td><span style='background-color: #5cb85c; border-radius: .25em; padding: .2em .6em .3em; font-weight: bold; color: #fff; font-size: 10pt; '>Online</i></span></td>";
}
echo "
<td><span style='color: #fff; font-size: 10pt;'>". $ip ."</span></td>
<td><span style='text-shadow: 0 0 10px #ffca00; color: #ffca00; font-size: 20pt;'>". $players ."</span></td>
</tr>
</tbody>
";
}
openserveriai();
include ('servers.php');
closeserveriai();
?>
</table>
</body>
</html>
You can do something like this
<?php
$total = 5;
if ($total >= 5 && $total <= 9) {
//Between 5 and 9
} else {
// Not between 5 and 9
}
?>
<table class='table table-bordered table-striped'>
<tbody>
<tr>
<td><span>First</span></td>
<td><span>Second</span></td>
<td><span><?php echo $total; ?></span></td>
</tr>
</tbody>
</table>
Then you can call that php page with
require('yourpage.php');

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>​

Why doesn't this php work? [duplicate]

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>

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.

Categories