Creating HTML table with php output - php

I am trying to get the php output into an HTML table. The mysql code outputs the data from the table pet, but when i add the table code as shown below it stops working and gives errors. How do I get the table pet output into an HTML table? thanks
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
echo "<table border = '2'>"
<tr>
<th>id</th>
<th>name</th>
</tr>
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>;
}
echo "</table>";
?>
</body>
</html>

Your issues lies in your formatting and confusion between the use of the echo command and non PHP wrapped HTML. Code should read as follows when properly formatted.
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
?>
</table>
</body>
</html>

<html>
<head>
<title> php test script - hope this works </title>
</head>
<body>
<h1>php & mysql connection</h1>
<hr>
<?php
$db_host = "localhost";
$db_username = "vistor";
$db_pass = "visitor";
$db_name = "test";
$db = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$db->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_WARNING);
$query = $db->query('SELECT * FROM pet');
?>
<table border ="2">
<tr>
<th>id</th>
<th>name</th>
</tr>
<?php
while ($row = $query->fetch())
{
?>
<tr>
<td><?php echo $row['id'];?></td>
<td><?php echo $row['name'];?></td>
<td><?php echo $row['price']; ?></td>
</tr>
<?php } ?>
</table>
</body>
</html>

Your quoting is wrong.
echo "<table border = '2'>
<tr>
<th>id</th>
<th>name</th>
</tr>";
while ($row = $query->fetch())
{
echo "<tr>";
echo "<td>" . $row['id'] ."</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "</tr>";
}
echo "</table>";
The quote at the end of the first echo line belongs after the </tr>. And the last echo "</tr>"; line was missing a closing quote.
You should be able to see these problems from the syntax highlighting in the question.

Related

How to generate links to images using php in a table

I am trying to create a table that will display the new suggested products by users to allow a moderator to to choose which get approved. For now, all I am trying to do is show the image, as well as the user that submitted it and some other info. I know my problem is with the bottom echo statement that tries to put together a working url by combining the file location and the name of the file itself from the database, which is stored like "Roots.jpg" or like "brock.jpg"
Thanks for any help. I am pretty new to php.
<html>
<title>Forum Approval</title>
<body>
<?php
$db_host = "host";
$db_username = "user";
$db_pass = "pass";
$db_name = "name";
$pdo = new PDO('mysql:host='.$db_host.';dbname='.$db_name,$db_username,$db_pass);
$pdo->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$pdo->setAttribute(PDO::ATTR_EMULATE_PREPARES, false);
$sql = "SELECT * FROM add_new_product";
$stmt = $pdo->prepare($sql);
$stmt->execute();
?>
<table border='1' align='center'>
<caption>Products to approve</caption>
<tr>
<th>Product Id</th>
<th>User Id</th>
<th>Name</th>
<th>Company</th>
<th>Image</th>
</tr>
<?php
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo '<tr>';
echo '<td>' . $row['new_prod_ID'] . '</td>';
echo '<td>' . $row['user_ID'] . '</td>';
echo '<td>' . $row['name'] . '</td>';
echo '<td>' . $row['company'] . '</td>';
echo "<td><img src = "a link/uploads/ . $row['image']" . width='100'></td>";
echo '</tr>';
}
?>
</table>
</body>
</html>
Try changing the echo to something like...
echo "<td><img src=\"path/to/directory/".$row['image']."\" width='100'></td>
The src would be path/to/directory/Roots.jpg

when i search my database instead of getting results i get error 500

i have a website that is supposed to search the database for a specific id, but i get an error 500 when the submit button is clicked
i am running php7 and the latest mysql, ive tried removing un-needed code, ive tried changing the method i use to get information from the database, no luck
"dbh.inc.php"
<?php
$servername = "localhost";
$dBUsername = "root";
$dBPassword = "";
$dBName = "chromebook";
$conn = mysqli_connect($servername, $dBUsername, $dBPassword, $dBName);
if (!$conn) {
die("Connection Failed: ".mysqli_connect_error());
}
"search.inc.php(destination page, shows results)"
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query("SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class="table" border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
i expect the page to load with the results asked from the database, instead i get an "http error 500" message from my browser
thanks for any help in advance
First parameter of the mysqli_query should be $conn
search.inc.php
<!DOCTYPE html>
<?php
require "dbh.inc.php";
?>
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
echo "<h2>Search Results</h2>";
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>";
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'");
echo "<table class='table' border='1'>
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
?>
</body>
</html>
echo "<h2>Search Results</h2> is incomplete; add the closing ";
Unclosed if statement at if(isset($_POST['search'])) {
Unescaped double quotes on echo "<p>You Didn't Submit Data! Try Again!</p><a
href="myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit"</a>"; and echo "<table class="table" border='1'>
Also, I would highly recommend adding some error checking to your code so these 500 errors aren't a total mystery.
This works great for on page error reporting:
error_reporting(E_ALL);
ini_set('display_errors', 1);
So in total, replace your existing code with this:
<html>
<head>
<title>Searching For Data</title>
</head>
<body>
<?php
// added error reporting
error_reporting(E_ALL);
ini_set('display_errors', 1);
echo "<h2>Search Results</h2>"; //fixed
if(isset($_POST['search'])) {
$search = $_POST['search'];
if ($search == "") {
echo "<p>You Didn't Submit Data! Try Again!</p><a href=\"myurlusuallyisherebutitsforaimportantthingandidontwantusersgoingtoit\"</a>"; // escape quotes
exit;
}
$search = strtoupper($search);
$search = strip_tags($search);
$search = trim($search);
$result = mysqli_query($conn, "SELECT * FROM chrome WHERE Teacher LIKE '%search%'"); // connection param added
echo "<table class=\"table\" border='1'> // escape quotes
<tr>
<th>Room Number</th>
<th>Teacher</th>
<th>Front Barcode</th>
<th>Back Barcode</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['RoomNum'] . "</td>";
echo "<td>" . $row['Teacher'] . "</td>";
echo "<td>" . $row['FrontId'] . "</td>";
echo "<td>" . $row['Barcode'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($conn);
} // closing bracket from main 'if' statement
?>

converting php code to html

I have been having hard time trying to convert my php code to html for 5 hours and at this point, I'm really burned out :X.
Here's my Php code
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if (mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
and here's what i have done so far for HTML
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("test_db", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) {
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php mysql_close($connector); ?>
</body>
</html>
little Help here please, Thanks !!
EDIT: seems like some people are not understanding my question. My php is working fine so i want to convert the php code into html. Basically, i want my table from database to show up using HTML table.
you not close while;
so change code to :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
/////////////////////////////////// change \\\
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db($database, $connector)
or die("Unable to connect");
/////////////////////////////////// end change \\\
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM tablea ");
?>
<table border="2">
<thead>
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<td>Other_Details</td>
</tr>
</thead>
<tbody>
<?php
while ($row = mysql_fetch_array($result)) :
?>
<tr>
<td><?php echo $row['id']; ?></td>
<td><?php echo $row['Subject_Code']; ?></td>
<td><?php echo $row['date']; ?></td>
<td><?php echo $row['name']; ?></td>
<td><?php echo $row['description']; ?></td>
<td><?php echo $row['Other_Details']; ?></td>
</tr>
<?php endwhile;?>
<?php mysql_close($connector); ?>
</body>
</html>
Actually your problem is as you said like html version there is no variable like $localhost but your passing the parameter to connect mysql etc. below error code shows the variable mismatch of your code.
Error code :
<?php
$username = "dbuser";
$password = "pw";
$host = "localhost";
$database= "dbname";
$connector = mysql_connect($localhost,$dbuser,$pw,dbname);
^^^^^^^^^^ ^^^^^^^^^^^^^^^^^^
//here there is no variable like what you passing to connect mysql that's problem here
?>
solution :
<!doctype html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>database connections</title>
</head>
<body>
<?php
$con=mysqli_connect("localhost","dbuser","pw","dbname");
// Check connection
if(mysqli_connect_errno())
{
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM tablea");
echo "<table border='1' >
<tr>
<th>id</th>
<th>Subject_Code</th>
<th>date</th>
<th>name</th>
<th>description</th>
<th>Other_Details</th>
</tr>";
while($row = mysqli_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['Subject_Code'] . "</td>";
echo "<td>" . $row['date'] . "</td>";
echo "<td>" . $row['name'] . "</td>";
echo "<td width='600' class='table_width'>" . $row['description'] . "</td>";
echo "<td width='600' class='table_width' align='center'>" . $row['Other_Details'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
Here's another version that makes use of PDO - by far the superior of php connectors in terms of maintainability and versatility. I have the same code working under MySQL, SQLite and SQLServer - the only thing that changes is the connection string.
You'll note I pull all of the results at once. I also make use of the fact that we get back an array. Each element of that array is a row. Each row is an array of cells. This greatly reduces the code needed to output a result-set. We just need two forEach loops and that's it.
<?php
$host = 'localhost';
$userName = 'dbuser';
$password = 'pw';
$nameOfDb = 'dbname';
$nameOfTable = 'tablea';
$pdo = new PDO('mysql:host='.$host, $userName, $password);
$pdo->query("use " . $nameOfDb);
// desired results requested explicitly, so when we get an array for the row's data, the elements will be in the same order
// - not required if the order of the columns in the database matches the desired display order. (we could just use 'select *' then)
//$query = $pdo->prepare('select * from '.$nameOfTable);
$query = $pdo->prepare('select id, Subject_Code, date, name, description, Other_Details from '. $nameOfTable);
$query->execute();
$query->setFetchMode(PDO::FETCH_ASSOC);
$rows = $query->fetchAll();
?><!doctype html>
<html>
<head>
</head>
<body>
<table>
<thead>
<tr>
<th>id</th><th>Subject_Code</th><th>date</th><th>name</th><th>description</th><th>Other_Details</th>
</tr>
</thead>
<tbody><?php
forEach($rows as $curRow)
{
echo '<tr>';
// use this one if you want to display the columns in the same order they are returned in the query results
// AND you'd like to display all columns in the result
forEach($curRow as $curCell)
echo '<td>'.$curCell.'</td>';
// otherwise, you can request the desired elements by name
//
// echo '<td>' . $curCell['id'] . '</td>'
// echo '<td>' . $curCell['Subject_Code'] . '</td>'
echo '</tr>';
}
?></tbody>
</table>
</body>
Download HTTrack Website Copier and install and run the soft. Run your script and paste your URL in web copier software. You will get HTML output in your desire folder

i am working on contact application and i have to view contact using contact button shown in below code

My code is below:
while($row = mysqli_fetch_array($result)) {
echo "<tr><td>"."<input type='checkbox' name='checkbox[]' value=".$row['id'].">"."</td>"; echo "<td>" . $row['firstname']. "</td>";
echo "<td>" . $row['lastname']. "</td>";
echo "<td>" . $row['email']. "</td>";
echo "<td>". "</td>"; echo "<td>"."<input type='button' name='view' id='view' value='View' />"."</td>"; echo "</tr>";
echo "</tbody>"; }
echo "</table>"; } ?>
According to your question , i guess you like to fetch the detail of a certain record.I've put some of code here . The easiest way to use button instead of a , use bootstrap.
db.php
<?php
class Database
{
private static $dbName = 'Customer' ;
private static $dbHost = 'localhost' ;
private static $dbUsername = 'root';
private static $dbUserPassword = '';
private static $cont = null;
public function __construct() {
die('Init function is not allowed');
}
public static function connect()
{
// One connection through whole application
if ( null == self::$cont )
{
try
{
self::$cont = new PDO( "mysql:host=".self::$dbHost.";"."dbname=".self::$dbName, self::$dbUsername, self::$dbUserPassword);
}
catch(PDOException $e)
{
die($e->getMessage());
}
}
return self::$cont;
}
public static function disconnect()
{
self::$cont = null;
}
}
?>
dummy.php
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<link href="css/bootstrap.min.css" rel="stylesheet">
<script src="js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
<div class="row">
<h3>PHP CRUD Grid</h3>
</div>
<div class="row">
<table class="table table-striped table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email Address</th>
<th>Mobile Number</th>
</tr>
</thead>
<tbody>
<?php
include 'db.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM contacts ORDER BY id DESC';
foreach ($pdo->query($sql) as $row) {
echo '<tr>';
echo '<td>'. $row['name'] . '</td>';
echo '<td>'. $row['email'] . '</td>';
echo '<td>'. $row['mobile'] . '</td>';
echo '</tr>';
}
Database::disconnect();
?>
</tbody>
</table>
</div>
</div> <!-- /container -->
</body>
</html>
detail.php
<?php
$id = $_GET['id'];
?>
<?php
include 'db.php';
$pdo = Database::connect();
$sql = 'SELECT * FROM contacts WHERE id='.$id;
foreach ($pdo->query($sql) as $row) {
echo "<div>";
echo "<p> ".$row['name']." </p>";
echo "<p> ".$row['email']." </p>";
echo "<p> ".$row['mobile']." </p>";
echo "</div>";
}
Database::disconnect();
?>
Let's assume i've database name "Customer" and table name "contacts" . db.php is database connection class. if you look at the dummy.php , you will find that you already achieved the list of records. So to answer your question , i put a tag in name field and which redirect to detail.php with parameter of id . there you can fetch the detail of contact .. Hope it might answer your needs

Mysql table not displayed as html table in cloud 9

hi I am trying to execute the following code on cloud 9
:
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
echo "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['name'] . "</td>";
echo "<td>" . $row['price'] . "</td>";
echo "<td>" . $row['image'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
</body>
</html>
when i am executing this code, i am getting just the following php code as result instead of the table contents:
Name Price Image "; while($row = mysqli_fetch_array($result)) { echo ""; echo "" . $row['name'] . ""; echo "" . $row['price'] . ""; echo "" . $row['image'] . ""; echo ""; } echo ""; mysqli_close($con); ?>
can someone tell me whre i am going wrong?
chang this command:
$con=mysqli_connect(0.0.0.0,"ritikasahay","","trydb");
To like this:
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
Table display once instead of multiple times
<!DOCTYPE html>
<html lang="en">
<head>
<title>Test | Products</title>
</head>
<body>
<?php
$con=mysqli_connect("0.0.0.0","ritikasahay","","trydb");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM veg");
//create table
$table = "<table border='1'>
<tr>
<th>Name</th>
<th>Price</th>
<th>Image</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
//bind rows
$table .= "<tr>";
$table .= "<td>" . $row['name'] . "</td>";
$table .= "<td>" . $row['price'] . "</td>";
$table .= "<td>" . $row['image'] . "</td>";
$table .= "</tr>";
}
//close table
$table .= "</table>";
//display table
echo $table;
mysqli_close($con);
?>
</body>
</html>

Categories