I've a php page with embedded HTML and I'm displaying data from a MySQL database. PHP is echoing the html inside the php page. All of the data is being returned; however, the data table is being displayed with an extra column and the data that should be in the last column is displayed in the extra column (e.g. my last name is 'Last Name,' but there is an extra column after 'Last Name' with the 'last name' data).
What am I doing wrong here?
Thanks.
get_records.php
//make connection
$conn = mysql_connect('localhost', 'root', '');
//select db
mysql_select_db('kis');
if (!$conn) {
die("Can not connect: " . mysql_error());
}
//select db and run query
mysql_select_db('kis');
$sql = "SELECT * FROM users";
$records = mysql_query($sql);
?>
<!DOCTYPE html>
<html lang="en">
<head>
<link rel="stylesheet" type="text/css" href="css/TableCSSCode.css" media="all"/>
<title>Volunteer Data</title>
</head>
<body>
<div class="CSSTableGenerator">
<h1>Volunteer Records</h1>
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
echo "</tr>";
}//end while
?>
</tr>
</table>
</div>
<!--end #dr_container-->
</body>
</html>
You need to close the td's
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
You have not properly closed your td tags:
echo "<td>" . $users['middlename'] . "<td>";
echo "<td>" . $users['lastname'] . "<td>";
It should be </td> at the end.
You're echoing the row tags (<tr>). So, don't add additional ones in the plain html (just around your PHP code).
Close "td" tag.
Remove "tr" tags before and after where php code start because there are already tr tags inside the php code.
Following is the updated HTML of table:
<table>
<tr>
<th>First Name</th>
<th>Middle Name</th>
<th>Last Name</th>
</tr>
<?php
//loop through the records and display in page
while ($users = mysql_fetch_assoc($records)) {
echo "<tr>";
echo "<td>" . $users['firstname'] . "</td>";
echo "<td>" . $users['middlename'] . "</td>";
echo "<td>" . $users['lastname'] . "</td>";
echo "</tr>";
}//end while
?>
Related
I am attempting to add sortable columns to my html table and I thought I would give the jquery tablesorter a try. This is my syntax sans the actual DB call, and I think I have it set-up properly, however my table is not allowing me to sort. Why am I not able to sort?
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
EDIT --->
I edited my syntax to read like this, but still have the issue
</thead>
<tbody>
<?php
foreach ($query as $res)
{
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
}
?>
</tbody>
</table>
</html>
EDIT 2
Below is update to show how $query get's it's value
<head>
<script type="text/javascript" src="/path/to/jquery-latest.js"></script>
<script type="text/javascript" src="/path/to/jquery.tablesorter.js"></script>
<script>
$(document).ready(function()
{
$("#SaleDistro").tablesorter();
}
);
</script>
</head>
<?php
$option = array();
$option['driver'] = 'mssql';
$option['host'] = 'IP Address';
$option['user'] = 'username';
$option['password'] = 'password';
$option['database'] = 'database';
$option['prefix'] = '';
$db = JDatabase::getInstance($option);
$query = $db->getQuery(true);
$query = "Select SalesName, SalesRegion, SalesCount, SalesSupervisor from salesdata;";
$db->setQuery($query);
$query = $db->loadObjectList();
if ($query)
{
?>
<table id="SaleDistro" class="tablesorter" border="1">
<thead>
<tr>
<th>Sales Name </th>
<th>Sales Region </th>
<th>Sales Count </th>
<th>Sales Supervisor </th>
</tr>
</thead>
<?php
foreach ($query as $res)
{
print "<tbody>";
print "<tr>";
print "<td>" . $res->sn . "</td>";
print "<td>" . $res->sr . "</td>";
print "<td>" . $res->sc . "</td>";
print "<td>" . $res->ss . "</td>";
print "</tr>";
print "</tbody>";
}
?>
</table>
</html>
You only want one <tbody> opening and closing tag each, so you need to move them out of the foreach loop.
I am pulling stuff from a DB and populating it into a drop-down select. When the user selects the query, I want it to be displayed in a table (which it is now). But the problem is with the format. I want the headers to be displayed just on the top of the table rather than for every single row. What is wrong with my code? Any ideas/suggestions? Thanks in advance.
Resulting page of code:
Desired format (table headings just on top):
<?php
session_start();
if(!isset($_SESSION['EmployeeID'])){
$URL="Logon.php";
header ("Location: $URL");
}
?>
<!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>Orders Page</title>
</head>
<body>
<h1>Orders Information Page</h1>
<form action="Orders.php" method="post">
<?php
require "Information.php";
try{
$database="Northwind";
$conn = new PDO("mysql:host=$hostname;dbname=$database", $username, $password);
} catch(PDOException $e){
$conn->setArribute(PDO::ATT_ERRMODE, PDO::ERRMODE_EXCEPTION);
echo "Could not open database.";
}
$lname= $_POST['txtFirstName'];
$extension = $_POST['txtLastName'];
if($_SERVER['REQUEST_METHOD'] == 'POST'){
$_SESSION['OrderID']=$_POST['selOrder'];
} //end if is a post
echo "<select name='selOrder'>";
$sql = $conn->prepare("SELECT * FROM orders WHERE EmployeeID=? ORDER BY CustomerID ASC");
if ($sql->execute(array($_SESSION['EmployeeID']))) {
while ($row = $sql->fetch()) {
echo "\n\t<option value='" . $row['OrderID'] . "'";
if(isset($_SESSION['OrderID']) && $_SESSION['OrderID']== $row['OrderID']){
echo " selected='selected' ";
}
echo ">";
echo "Cust:" . $row['CustomerID'] . " ordered on " . $row['OrderDate'];
echo "</option>";
}
}
echo "</select>";
?>
<input name="btnSubmit" type="submit" value="Submit" />
<?php
if(isset($_SESSION['OrderID'])){
echo "<h3>Order Details</h3><p>";
$sql = $conn->prepare("SELECT * FROM orders, orderdetails, products WHERE orders.OrderID=orderdetails.OrderID AND products.ProductID=orderdetails.ProductID AND orders.OrderID=? ");
if ($sql->execute(array($_SESSION['OrderID']))) {
while ($row = $sql->fetch()) {
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
{
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
}
echo "</p>";
}
$conn =null;
?>
</form>
</body>
</html>
Maybe I understand the problem very poorly, or do not understand at all, but is
...
echo "<table border = '1'>
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
...
not what you are looking for? Eg displaying the <th>'s only once?
This is not very difficult, you just made a little mistake.
Get the
<tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
part out of the while loop, just before the while loop, and it will work like how you want it to.
Hope this helps.
move
"<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
(and while you're at it the echo "</table>";)
out of the while loop:
echo "<table border = '1'><tr>
<th>Order ID</th>
<th>Order Date</th>
<th>Product Name</th>
</tr>";
while ($row = $sql->fetch()) {
echo "<tr>";
echo "<td>" . $row['OrderID'] . "</td>";
echo "<td>" . $row['OrderDate'] . "</td>";
echo "<td>" . $row['ProductName'] . "</td>";
echo "</tr>";
}
echo "</table>";
note that you had two extra curly braces that weren't doing anything.
I'm trying to display members from my database using PHP, however the last name goes in with the first name and I'm not quite sure why... Could anyone point me in the right direction?
<?php
$mysql_db_hostname = "localhost";
$mysql_db_user = "alex";
$mysql_db_password="";
$mysql_db_database="gym";
$con = mysql_connect($mysql_db_hostname, $mysql_db_user, $mysql_db_password) or die("Could not connect database");
mysql_select_db($mysql_db_database, $con) or die("Could not select database");
$query = mysql_query("select * from users WHERE Category='Member'");
echo "<table border=1>
<tr>
<th>Users ID</th>
<th>First Name</th>
<th>Last Name</th>
</tr>";
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td";
echo "<td>" . $row['Last_Name']."<br>" . "</td";
echo "</tr>";
}
echo "</table";
?>
You aren't closing the tags. Syntax highlighter would show the error.
while($row =mysql_fetch_assoc($query))
{
echo "<tr>";
echo "<td>" . $row['user_id']."<br>" . "</td>";
echo "<td>" . $row['First_Name']."<br>" . "</td"; <-----------
echo "<td>" . $row['Last_Name']."<br>" . "</td"; <------------
echo "</tr>";
}
You are not closing your <td> tags properly
echo "<td>" . $row['First_Name']."<br>" . "</td>";
//^here
echo "<td>" . $row['Last_Name']."<br>" . "</td>";
//^and here
So the output is all one cell with both variables printed inside
You forgot to close the td on your first name field.
Just change
</td
to
</td>
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.
I have looked extensively through the site and have come to the conclusion that I am encountering a problem that many people do, yet none of the answers I have seen seem to work!
Basically, I am trying to populate an HTML table from the data stored in a mySQL table. The data is in a table called "categories". When I load the page, the table headers appear but no table data.
I have written and rewritten my code no less than 4 times - it works fine as an "ul" or when rendered as just plain text, but as soon as I put it into a "table" to seems to stop working!
Here's my code:
<?php
include("includes/dbconnect.php"); //This works fine as tested on the page
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
$product = mysql_fetch_array($myData);
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php do {
echo $product['title']." <br />";
} while ($product = mysql_fetch_array($myData));
//-----------------------------------------
//Table - doesnt work
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
while ($product = mysql_fetch_array($myData)) {
echo "<tr>";
echo "<td>".$product['title']."</td>";
echo "<td>" . $product['category'] . "</td>";
echo "<td>" . $product['sport'] . "</td>";
echo "<td>" . $product['team'] . "</td>";
echo "<td>" . $product['price'] . "</td>";
echo "<td>" . $product['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
</body>
</html>
I really am at my wits end, Ive worked my way through countless YouTube tutorials and still for some reason the code wont work. Can anyone help?
You've already run mysql_fetch_array in a do while loop at the top of your code. You aren't able to pull a result row more than once. Instead, push all of the the returned rows on to an array:
$products = array();
while ($product = mysql_fetch_array($myData)) {
$products[] = $product;
}
You can then loop through $products as many times as you'd like to build out your page. So for your table, this would look like:
echo "<table border=1>
<tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th>
</tr>";
foreach($products as $item) {
echo "<tr>";
echo "<td>" . $item['title'] . "</td>";
echo "<td>" . $item['category'] . "</td>";
echo "<td>" . $item['sport'] . "</td>";
echo "<td>" . $item['team'] . "</td>";
echo "<td>" . $item['price'] . "</td>";
echo "<td>" . $item['shipping'] . "</td>";
echo "</tr>";
}
echo "</table>";
if that code really looks like this remove the do-while loop and everything where you iterate over your $mydata and it will work since you waste the internal mysql-counter
meaning in the second loop your $result is already at the end, either you query again OR you push all the results in an array for multiple use
You have already pulled your data once with this.....$product = mysql_fetch_array($myData);
so just use this array with foreach loop....
foreach($product as $sProduct){
// User $sProduct to access that single product.
}
Your code contains many bugs and implementation
try this if it works for you :
<?php
include("includes/dbconnect.php");
$sql = "SELECT * FROM products";
$myData = mysql_query($sql) or die (mysql_error());
?>
<html>
<head>
<title></title>
</head>
<body>
//This plain text works
<?php
if(mysql_num_rows ($myData) > 0)
{
$html = "<table border=1><tr>
<th>Title</th>
<th>Category</th>
<th>Sport</th>
<th>Team</th>
<th>Price</th>
<th>Shipping</th></tr>";
while ($product = mysql_fetch_array($myData))
{
$html .= "<tr>";
$html .= "<td>".$product['title']."</td>";
$html .= "<td>".$product['category']."</td>";
$html .= "<td>".$product['sport']."</td>";
$html .= "<td>".$product['team']."</td>";
$html .= "<td>" . $product['shipping'] . "</td>";
$html .= "</tr>";
}
$html .= "</table>";
echo $html;
}
else
{
echo "No Product Found";
}
?>
</body>
</html>
If this works for you, you can figure out the problems or I will explain :)