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.
Related
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');
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>
I need some help on how to echo out my mysql data into a html table. I'm trying to put in the relevant table tags where necessary but I must be doing it wrong as its not looking how I want it.
Here's what I am getting:
ID Name Number Note Alert
1
2
Nick Nick insurance insurance alert alert
Here is how I want it to look:
ID Name Number Note Alert
1 Nick Insurance Alert
2 Nick Insurance Alert
Here is my code, can someone please show me where I need to put my table tags to get the desired result:
<?php
include 'config.php';
$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());
echo "<table class=\"table\" style=\"width:900px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px; color:#96969;\" >";
while ($info = mysql_fetch_array($data))
{
echo "<tr><td><p>" . $info['id'] . "</p></td></tr>";
}
?>
<?php
include 'config.php';
$data = mysql_query("SELECT * FROM supplier_stats") or die(mysql_error());
while ($info = mysql_fetch_array($data))
{
echo "<td><p>" . $info['company_name'] . "</p></td>";
}
?>
<?php
include 'config.php';
$data = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error()); ?>
<?php
include 'config.php';
$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$days = $row['expire_date'] - 1;
if ($days > 0)
{
echo "<td><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>";
}
else
{
$when = $days * -1;
echo "<td><p>Insurance expires";
if ($when > 1)
{
echo " in {$when} days</p></td>";
}
if ($when >= 8)
{
echo " <div class=\"green_light\"></div>";
}
if ($when <= 7)
{
echo " <div class=\"red_light\"></div>";
}
elseif ($when === 1)
{
echo " tomorrow</p></td>";
}
elseif ($when = 0)
{
echo " today</p></td>";
}
}
}
?>
<?php
include 'config.php';
$result = mysql_query("SELECT TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date
FROM supplier_stats") or die(mysql_error());
while ($row = mysql_fetch_array($result))
{
$days = $row['expire_date'] - 1;
if ($days > 8)
{
echo "a is bigger than b";
}
}
echo "</table>"; //Close the table in HTML
?>
You need more or less somethin like this:
<?php include 'config.php';
$data = mysql_query("SELECT *, TIMESTAMPDIFF(DAY, insurance_date, NOW()) AS expire_date FROM supplier_stats")
or die(mysql_error());
echo "<table class=\"table\" style=\"width:900px; font-family: 'Lucida Grande', Tahoma, Verdana, Arial, sans-serif;
font-size:11px; color:#96969;\" >";
while($row = mysql_fetch_array( $data )) {
$days = $row['expire_date'] -1;
echo "<tr><td><p>".$row['id'] . "</p></td>";
echo "<td><p>".$row['company_name'] . "</p></td>";
if ($days > 0) {
echo "<td><p>Insurance expires in <font color=\"red\">{$row['expire_date']} day(s)!</font></p></td>";
} else {
$when = $days*-1;
echo "<td><p>Insurance expires";
if ($when > 1){
echo " in {$when} days</p></td>";
}
if ($when >= 8){
echo " <div class=\"green_light\"></div>";
}
if ($when <= 7){
echo " <div class=\"red_light\"></div>";
} elseif ($when ===1) {
echo " tomorrow</p></td>";
} elseif ($when == 0) {
echo " today</p></td>";
}
}
echo "<tr>";
}
echo "</table>"; //Close the table in HTML
?>
you've closed after first that is why you are getting output like this..remove from:
echo "<tr><td><p>".$info['id'] . "</p></td></tr>";
above line...and close after while loop ends..
<table>
<tr>
<td>Id</td>
<td>Name</td>
<td>Number</td>
<td>Note</td>
<td>Alert</td>
</tr>
<?php
$sql = mysql_query("your query");
$i = 0;
while ($info = mysql_fetch_array($data))
{
$i++
?>
<td><?php echo $i; ?></td>
<td><?php echo $info['name']; ?></td>
<td><?php echo $info['number']; ?></td>
<td><?php echo $info['note']; ?></td>
<td><?php echo $info['alert']; ?></td>
</tr>
<?php
}
?>
</table>
These are the working codes that i have done. the program is about accessing information in an xml file such as student's name,id and marks based on group and team number submitted from a form. there are 2 submit buttons if one button is clicked, it will display with an additional element (pic) and the other one without it. so far so good.
but looking at the php codes, i know that i will be facing problems if there are numerous groups and team numbers. there would be a lot of if-else statements and the codes would be very long. i have tried using foreach but i'm not getting the result that i want. are there anyways to simplify this? i have been searching on the web but it seems that i just don't know how to implement them. quite new to this.
<html><style>
.datagrid table { border-collapse: collapse; text-align: center; width: 100%; }
.datagrid {font: normal 12px/150% Arial, Helvetica, sans-serif;
background: #fff;
overflow: hidden;
border: 4px solid #006699;
-webkit-border-radius: 20px;
-moz-border-radius: 20px;
border-radius: 20px; }
.datagrid table td, .datagrid table th { padding: 7px 20px; }
.datagrid table thead th {background:-webkit-gradient( linear, left top, left bottom, color-stop(0.05, #006699), color-stop(1, #00557F) );
background:-moz-linear-gradient( center top, #006699 5%, #00557F 100% ); filter:progid:DXImageTransform.Microsoft.gradient(startColorstr='#006699', endColorstr='#00557F');
background-color:#006699; color:#FFFFFF; font-size:15px; font-weight:bold;
border-left: 1px solid #0070A8; }
.datagrid table thead th:first-child { border: none; }
.datagrid table tbody td { color: #00496B; border-left: 1px solid #E1EEF4;font-size: 13px;font-weight: normal; }
.datagrid table tbody .alt td { background: #E1EEF4; color: #00496B; }
.datagrid table tbody td:first-child { border-left: none; }
.datagrid table tbody tr:last-child td { border-bottom: none; }
</style>
<center>
<form action="" method="post" >
<select name="group">
<option value="csa">CSA</option>
<option value="csb">CSB</option>
</select>
Enter team number:<input type="text" name="teamnum" value="">
<input type="submit" name="submit1" value="With Photo">
<input type="submit" name="submit2" value="Without Photo">
</form>
<form action="index.php">
<input type="submit" value="main">
</form>
</center>
</html>
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
//$photo =$xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/pic');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>1</td>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if (isset($_POST['submit2']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
if ($group == 'csa' && $teamnum == '1') {
$name = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"1")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2) = each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
else if ($group == 'csa' && $teamnum == '2') {
$name = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/name');
$id = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/id');
$total = $xml->xpath('//student[teamNum[contains(text(),"2")] and group[contains(text(),"CSA")]]/total');
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
while ((list(, $node) = each($name)) && (list(, $node1) = each($id)) && (list(, $node2)= each($total))) {
echo "<tbody><tr class='alt'>";
echo "<td>$node</td>";
echo "<td>$node1</td>";
echo "<td>$node2</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}
?>
i have found the way to solve this by using foreach
<?php
if (isset($_POST['submit1']) != '') {
$file = "student.xml";
$xml = simplexml_load_file($file) or die("Unable to load XML file!");
if (isset($_POST['group']) && isset($_POST['teamnum']) != '') {
$teamnum = $_POST['teamnum'];
$group = $_POST['group'];
echo "<div class='datagrid'><table border='1' cellpadding='1'>";
echo "<thead><tr><th>Photo</th><th>Name</th><th>ID</th><th>carry marks</th></tr></thead>";
echo "<tbody>";
foreach ($xml->xpath("//student[contains(teamNum, $teamnum) and contains(group, $group)]") as $student) {
echo "<tr class='alt'>";
echo "<td>".$student->picture."</td>";
echo "<td>".$student->name."</td>";
echo "<td>".$student->id."</td>";
echo "<td>".$student->total."</td>";
echo "</tr>";
}
echo "</tbody></table></div>";
}else echo 'no data';
} else {
echo "enter a team's number";
}
?>
the trick is in the xpath. it selects all students that contains submitted values. after that it will be as $student and i will be able to output the element values from there.
I have two rank in my forum Admin = 1 and User = 0 Then this code is doing so the admin get red name.
if($info['rank'] == 1) { $x= "<a class='admin'>".$last_user."</a>";
} else { $x= "<a class='user'>".$last_user."</a>"; }
But everyone is getting red name..
use the $x where you want to wish.
<?PHP
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
if($info[rank] == 1) { $x= "<div class='admin'>".$last_user."</div>"; } else { $x="<div>".$last_user."</div>"; }
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .="
<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user."
$x
<i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
?>
I have make the variable for you in the top.
Hope it work
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res)) {
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "") { $last_user = getusername($row['topic_creator']); } else { $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "") { $last2_user = 'Started by'; } else { $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'><div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'><i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)." Comments <i class='icon-user icon-1'>
</i>".$last2_user." ";
Syntax error is there at last line i.e ".$last2_user."
Just close the quote at " ";
This will solve your problem.
It looks like you are trying to insert an if statement inside the string you are creating. You can't do that as far as I know. However, the solution is easy. You end the string, insert your if statement, then append the rest of what you wanted. Like this:
if (mysql_num_rows($res) > 0) {
while ($row = mysql_fetch_assoc($res))
{
$tid = $row['id'];
$cid = $row['category_id'];
$title = $row['topic_title'];
$views = $row['topic_views'];
$date = $row['topic_date'];
$creator = $row['topic_creator'];
if ($row['topic_last_user']== "")
{ $last_user = getusername($row['topic_creator']); }
else
{ $last_user = getusername($row['topic_last_user']); }
if ($row['topic_last_user']== "")
{ $last2_user = 'Started by'; }
else
{ $last2_user = 'Most recent by'; }
$topics .= "<li class='category'><div class='left_cat'>
<a class='underline' href='view_topic.php?cid=".$cid."&tid=".$tid."'>
<div class='title'><i class='icon-comment'></i> ".$title."</div></a>
<div class='info'>
<i class='icon-comments icon-1'></i> ".topicreplies($cid, $tid)."
Comments <i class='icon-user icon-1'>
</i>".$last2_user." ";
// The string above is ended - and you insert your if statement.
// I changed the echo to an append to the string btw.
if ($info['rank'] == 1) { $topics .= "<div class='admin'>".$last_user."</div>"; } else { $topics .= "<div>".$last_user."</div>"; }
// Now you get to continue appending to the string.
$topics .= " <i class='icon-calendar'> </i> ".convertdate($date)."</div>
</div>";
$topics .= "<div class='right_cat'>
<div class='face'>
<img class='img_face' src='https://minotar.net/avatar/".getusername($creator)."/40.png'></div>
<div class='comments_cat'>
<div class='comments_top'>Comments</div>
<div class='comments'>".topicreplies($cid, $tid)."</div></div>
<div class='views_cat'>
<div class='views_top'>Views</div>
<div class='views'>".$views."</div></div>
</div></li>";
}
echo $topics;
} else {
echo "<div class='alert alert-danger text5'>There are no topics available yet.</div>";
}
The following code is what I have in the example provided above, and Here
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<form action="" method="POST">
Input number and press enter:
</br>User = 0 Admin = 1</br>
<input name="rank" id="rank" type="text" />
</form>
<?php
if (isset($_POST['rank'])) {
$info['rank'] = $_POST['rank'];
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
}
?>
As I mentioned in my comment above your code works fine, unless there is something going on elsewhere that we cant see with what you have given us.
And without a form.
<style type="text/css">
.user { font-weight:900; color: #000; text-decoration: none !important; }
.admin { font-weight:900; color: #F00; text-decoration: none !important; }
</style>
<?php
$info['rank'] = 1;
if($info['rank'] == 1) { $x= "<a class='admin'>Admin</a>"; }
else { $x= "<a class='user'>User</a>"; }
echo $x;
?>