HTML table error in php - php

i am new to php and i try to solve this many times but i couldn't.
this is my php code
Can someone help me with this it should be easy for a php expert.
<?php
require_once 'connector.php';
$result = mysql_query('SELECT * FROM highscores ORDER BY score DESC');
$username = mysql_query('SELECT username FROM users WHERE id in(SELECT user_id FROM highscores)');
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while ($name = mysql_fetch_array($username) )
{
echo "<tr>
<td>" . $name ['username'] . "</td>";
}
while( $row = mysql_fetch_array($result))
{
echo"
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}
echo"
</table>
</body>
</html>
";
the table i want to take

mysql_* is deprecated! use mysqli_*
<?php
require_once 'connector.php';
$SQL = "SELECT u.username, h.score, h.date
FROM users AS u
JOIN highscores AS h ON (h.user_id = u.users_id)";
$result = mysql_query($SQL) or die( mysql_error() );
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>";
if( mysql_num_rows($result) > 0 )
{
echo "<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>";
while ( $row = mysql_fetch_array($result) )
{
echo "<tr>";
printf("<td>%s</td>", $row['username']);
printf("<td>%s</td>", $row['score']);
printf("<td>%s</td>", $row['date']);
echo "</tr>";
}
echo "</table>
</body>
</html>";
}
mysql_free_result($result);

Can you try this,
$result = mysql_query('SELECT hs.score, hs.date, u.username FROM highscores as hs, users as u where hs.user_id=u.id ORDER BY score DESC');
echo "<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
while( $row = mysql_fetch_array($result))
{
echo"<tr>
<td>" . $row ['username'] . "</td>
<td>" . $row ['score'] . "</td>
<td>" . $row ['date'] . "</td>
</tr>";
}

Instead of putting everything in a double quote, you can use string concatenation with (.) operator. For example, Instead of writing like this
echo"<html>
<head>
<title>Highscores</title>
</head>
<body>
<table border='1'>
<tr>
<th>user</th>
<th>score</th>
<th>Date</th>
</tr>
";
You can write like this:
echo "<html>" .
"<head>" .
"<title>Highscores</title>"
"</head>" .
"<body>" .
"<table border='1'>" .
"<tr>" .
"<th>user</th>" .
"<th>score</th>" .
"<th>Date</th>" .
"</tr>" ;
The following code block
echo "<tr>
<td>" . $name ['username'] . "</td>";
Should be written as
echo "<tr>" .
"<td>" . $name ['username'] . "</td>";
In this way the code looks more readable as well.

The second loop needs to be inside the first loop, and the closing </tr> shouldn't be inside the second loop.
<?
while ($name = mysql_fetch_array($username)) {
echo "<tr>";
echo "<td>" . $name["username"] . "</td>";
while ($row = mysql_fetch_array($result)) {
echo "<td>" . $row["score"] . "</td>";
echo "<td>" . $row["date"] . "</td>";
}
echo "</tr>";
}
?>

Related

table class not working after inserting sql database inside

I firstly did the table layout and made sure everything is working, after connecting the table to a database and trying to put the records inside it, everything worked perfectly, but the class didnt, i have now the boring table without the layout made.
<body>
<h1>Employees</h1>
<table class="responstable">
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>
the table class is "responstable"
table tag is defined in 2 places.try removing this.
echo "<table border='1'>
or remove the table tag at the top after h1 tag and add the class to the table tag defined in the echo as,
complete code
<body>
<h1>Employees</h1>
<?php
require 'connection.php';
$conn = Connect();
$result = mysqli_query($conn,"SELECT * FROM employee");
echo "<table border='1' class='responstable'>
<tr>
<th>Id</th>
<th>First name</th>
<th>Last name</th>
<th>Salary</th>
<th>Start Date</th>
<th>Department</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['salary'] . "</td>";
echo "<td>" . $row['startdate'] . "</td>";
echo "<td>" . $row['department'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
<script src='http://cdnjs.cloudflare.com/ajax/libs/respond.js/1.4.2/respond.js'></script>
</body>
</html>

PHP if statement in html-table outputs error

I am new to HTML5 and PHP, I am trying to output a specific value in table data, If the database-retrieved-value is per condition.
My code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
. "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Error output
Parse error: syntax error, unexpected T_IF in
/test/composition/login/portal/portal.php
on line 204
What is the right way to solve this?
EDIT
my current code:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>Naam Client</th>
<th>Laatste Update</th>
<th style="margin-left: 40%; padding-left: 0%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable" style="font-size: 11pt;">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, naam, datum FROM clients";
$result = mysql_query($query);
$query2 = "SELECT status FROM clients";
$result2 = mysql_query($query2);
if (!empty ($result2)) {
while ($row2 = mysql_fetch_assoc($result2)) {
echo $row2['status'] . "<br />";
}
}
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['naam'] . "</td>
<td>" . $row['datum'] . "</td>
<td style='padding-left: 30%;'>";
if ($results2 > 60 && $results2 < 70) {
echo "red";
} elseif ($results2 > 50 && $results2 < 60) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
Output the right data. but partly outside and partly inside the table.
You will have to remove the if statement out of the echo to get rid of the error Try this:
<table class="scroll">
<thead style="background-color: #99E1D9; color: #705D56;">
<tr>
<th>ID</th>
<th>Name Client</th>
<th>Last Update</th>
<th style="padding-left: 30%;">Status</th>
</tr>
</thead>
<tbody id="hoverTable">
<?php
$connection = mysql_connect('localhost', 'root', '');
mysql_select_db('patientdb');
$query = "SELECT id, name, date FROM clients";
$result = mysql_query($query);
//status waarden uit
$status = "SELECT status FROM clients";
$status_ = mysql_query($status);
while($row = mysql_fetch_array($result)){ //Loop through results
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60) {
echo "red";
} elseif ($status_ > 50) {
echo "yellow";
} else {
echo "green";
}
echo "</td>
</tr>";
}
mysql_close();
?>
</tbody>
</table>
You can't have an if statement (or any other statement, for that matter) in the middle of another statement like echo. If you want to concatenate different strings depending on a variable, you can use the conditional (AKA "ternary") operator.
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .
$status_ > 60 ? "red" : ($status_ > 50 ? "yellow" : "green" )
. "</td>
</tr>";
Try:
$status = "green";
if ($status > 50)
{
$status="yellow";
}
elseif($status>60)
{
$status="red";
}
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>" .$status. "</td>
</tr>";
You can't append to a string a conditional statement, assign to a variable first for example (like I posted)
This part isn't at the right place:
if ($status_ > 60){echo "red";
} elseif ($status_ > 50){echo "yellow";
} else{echo "green";}
should be:
echo "<tr>
<td>" . $row['id'] . "</td>
<td>" . $row['name'] . "</td>
<td>" . $row['date'] . "</td>
<td style='padding-left: 30%;'>";
if ($status_ > 60){
echo "red";
} elseif ($status_ > 50){
echo "yellow";
} else{
echo "green";
}
echo "</td></tr>";
Surely status_ would not come back with a number, but an array.
$status_ = mysql_query($status);
Without knowing what data is coming back, it is difficult to help.
mysql_query

MySQL query working in workbench, but does not work via PHP interface

I have a MySQL query that I know to be working in Workbench/Sequel Pro that appears to return empty in my PHP code:
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
if($db_field = mysql_fetch_assoc($result)) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field = mysql_fetch_assoc($result)) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}
The result of this code is the first table row (Exam ID, Module ID etc.) is displayed but the table content is not.
I cannot work out why this is happening. Other than the SQL query, the code is identical to code used elsewhere (where it is working fine).
You are doing mysql_fetch_assoc twice. The first time it reads the record and prints the table header. The second time there are no more records so it doesn't print a row.
Change
if($db_field = mysql_fetch_assoc($result)) {
to
if(mysql_num_rows($result) > 0) {
Try this. You are using mysql_fetch_assoc($result); two times, First time it get result and second time there is no result.
$SQL = "SELECT * FROM staff, invigilation, exam, occupation, room, module
WHERE staffID='$inputID'
AND invigilation.examID=exam.exam_ID
AND exam.exam_ID=occupation.examID
AND room.room_ID=occupation.roomID
AND exam.module_ID=module.module_ID
AND staff.password='$inputPassword'";
$result = mysql_query($SQL);
$db_field = mysql_fetch_assoc($result)
if($db_field != "" ) {
echo "
<table border='1'>
<tr>
<th>Exam ID</th>
<th>Module ID</th>
<th>Module name</th>
<th>Duration</th>
<th>Start time</th>
<th>Room</th>
</tr>";
while ($db_field) {
echo "
<tr>
";
echo "<td>" . $db_field['exam_ID'] . "</td>";
echo "<td>" . $db_field['module_ID'] . "</td>";
echo "<td>" . $db_field['module_name'] . "</td>";
echo "<td>" . $db_field['duration'] . "</td>";
echo "<td>" . $db_field['start_datetime'] . "</td>";
echo "<td>" . $db_field['room_name'] . "</td>";
echo "
</tr>";
}
} else {
echo "Incorrect login details. Please try again";
die();
}

Output not displaying all rows

Having an issue with the output of records from a query run to display records.... It only shows the first row as the code specifies and then the next results all in.. paragraphs? I don't know if it has something to do
<?php
include 'core/init.php';
include 'includes/overall/header.php';
?>
<div class="article" style="width:900px !important">
<?php
$result = $sql = mysql_query("SELECT * FROM ref_employees WHERE employerid={$user_data['user_id']} ")
or die('Error in query : $sql. ' .mysql_error());
echo "<table border='0' class='table'>
<tr>
<th>ID Number</th>
<th>Employee Number</th>
<th>FirstName</th>
<th>LastName</th>
<th>MiddleName</th>
<th>Job Title</th>
<th>Employement Status</th>
<th>Contact</th>
<th>Email</th>
<th>Edit</th>
</tr>";
if (mysql_num_rows($sql) > 0)
{
while ($row = mysql_fetch_array($sql)){
if ($row['employed'] == '1'){
echo "<tr>";
echo "<td>" . $row['idnumber'] . "</td>";
echo "<td>" . $row['empnumber'] . "</td>";
echo "<td>" . $row['firstname'] . "</td>";
echo "<td>" . $row['lastname'] . "</td>";
echo "<td>" . $row['middlename'] . "</td>";
echo "<td>" . $row['jobtitle'] . "</td>";
echo "<td>" . $row['employed'] . "</td>";
echo "<td>" . $row['contactnum'] . "</td>";
echo "<td>" . $row['contactemail'] . "</td>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "</tr>";
echo "</tr>";
echo "</table>";
}
}
}
?>
</div>
<?php include 'includes/overall/footer.php';
?>
You are using closing table tag into loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
echo "</table>";
}
use table closing tag out of loop as
while ($row = mysql_fetch_array($sql)){
....
....
...
}
echo "</table>";

Hiding a table if a search result does not exist

Exactly what the title says. I want the table containing all of the search queries to be hidden, but I've tried a lot of things and none of them work. For example, if($myData!=null) {proceed with showing the table}, but that didn't work. isset() also didn't work. Any ideas?
<style>
ul
{
list-style-type: none;
}
</style>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Search Chemicals</title>
</head>
<p>
<body>
<h3>Chemical Information</h3>
<p>You may search by Catalog number, CASRN, or the chemical name.</p>
<form method="post" action="search.php?go" id="searchform">
<input type="text" name="name">
<input type="submit" name="submit" value="Search">
</form>
<?php
error_reporting(0);
if (isset($_POST['submit'])) {
if (isset($_GET['go'])) {
if (preg_match("/^[a-zA-Z0-9]+/", $_POST['name'])) {
$name = $_POST['name'];
$conn = mysql_connect("localhost", "Blimeo", "password");
$db = mysql_connect("localhost", "-", "-") or die('I cannot connect to the database because: ' . mysql_error());
//-select the database to use
$mydb = mysql_select_db("chemicals");
//-query the database table
$sql = "SELECT * FROM products WHERE Catalog LIKE '%" . $name . "%' OR CASRN LIKE '%" . $name . "%' OR Chemical_Name LIKE '%" . $name . "%'";
$myData = mysql_query($sql, $conn);
echo "<table border=1>
<tr>
<th>Catalog</th>
<th>Image</th>
<th>CASRN</th>
<th>Chemical Name</th>
<th>Quantity 1</th>
<th>Price 1</th>
<th>Quantity 2</th>
<th>Price 2</th>
<th>Quantity 3</th>
<th>Price 3</th>
<th>Quantity 4</th>
<th>Price 4</th>
</tr>";
while ($record = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>" . $record['Catalog'] . "</td>";
echo "<td><img src=\"./img/" . $record['Image'] . "\" alt=\"Chemical\"/></td>";
echo "<td>" . $record['CASRN'] . "</td>";
echo "<td>" . $record['Chemical_Name'] . "</td>";
echo "<td>" . $record['Quantity1'] . "</td>";
echo "<td>" . $record['Price1'] . "</td>";
echo "<td>" . $record['Quantity2'] . "</td>";
echo "<td>" . $record['Price2'] . "</td>";
echo "<td>" . $record['Quantity3'] . "</td>";
echo "<td>" . $record['Price3'] . "</td>";
echo "<td>" . $record['Quantity4'] . "</td>";
echo "<td>" . $record['Price4'] . "</td>";
echo "</tr>";
echo "</form>";
echo "<ul>\n";
echo "<li>" . "" . $Catalog . " " . $CASRN . " " . $Chemical_Name . "</li>\n";
echo "</ul>";
}
}
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
}
?>
</body>
</html>
</p>
You should add mysql_num_rows();
$myData = mysql_query($sql, $conn);
$exists = mysql_num_rows($myData);
if($exists) {
echo "<table border=1>";
//..................
echo "</table>";
} else {
echo "<p>Product not found! Please rephrase your search criteria.</p>";
}
Well, it seems you are echoing out your table regardless of the results of the search query. You should probably check the number of rows returned in teh result set and only echo out the table if the count is > 0.
Use <div visibility="hidden"> to hide the table and use Javascript to change the visibility based on the search queries or some other condition.

Categories