converting php code to html - php

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

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

How do I fill my combobox in PHP

I want to fill my combobox with names from my sql database. I saw some code on W3schools, but I really can't get it to work in my code. I have the combobox filled with select but that's not what I want it to be like. Here is the code:
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
$sql="SELECT * FROM user WHERE id = '".$q."'";
$result = mysqli_query($con,$sql);
echo "<table>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Age</th>
<th>Hometown</th>
<th>Job</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['LastName'] . "</td>";
echo "<td>" . $row['Age'] . "</td>";
echo "<td>" . $row['Hometown'] . "</td>";
echo "<td>" . $row['Job'] . "</td>";
echo "</tr>";
}
echo "</table>";
mysqli_close($con);
?>
Here is my code where I want it to apply to.
<!DOCTYPE html>
<html>
<head>
<link rel="stylesheet" href="javascript.js">
<link rel="stylesheet" href="layout.css">
<title>Bestuur wijzigen</title>
<link rel="icon" href="images/favicon.png">
</head>
<body>
<ul class="horizontal gray">
<li><a class="active" href="index.php">Bestuur</a></li>
<li>Bestuur wijzigen</li>
<li>Bestuur toevoegen</li>
</ul>
<form action="index.php">
<table class="table" border="1" frame="void" rules="rows">
<tr>
<td><label for="naam">Kies een bestuurslid</label></td>
<td>
<select>
<option value="volvo">Luca Fraser</option>
<option value="saab">Pieter Schreurs</option>
<option value="opel">Wessel Oblink</option>
<option value="audi">Andre Lammers</option>
</select>
</td>
</tr>
<tr>
<td><label for="functie">Functie</label></td>
<td>
<select>
<option value="#" selected="">Voorzitter</option>
<option value="#">Secretaris</option>
<option value="#">Penningmeester</option>
</select>
</td>
</tr>
<tr>
<td><button type="submit" class="button">Opslaan</button</td>
</tr>
</tbody></table>
</form>
</body>
</html>
Here is sample code for one combobox try with this code
<?php
$q = intval($_GET['q']);
$con = mysqli_connect('localhost','peter','abc123','my_db');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
mysqli_select_db($con,"ajax_demo");
//$sql="SELECT * FROM user WHERE id = '".$q."'"; //your old code
$sql = "SELECT * FROM user WHERE id LIKE = '%$q%'";
$result = mysqli_query($con,$sql);
$name = [];
while($row = mysqli_fetch_array($result)) {
$names[] = $row['FirstName'];
}
mysqli_close($con);
?>
//if your query not working uncomment following line and check then it is working the problem is your query result
// $names = ['jhon','patel','Ameer','Stepan'];
<select>
<?php foreach($names as $name){ ?>
<option value="#"><?php echo $name; ?></option>
<?php } ?>
</select>
Try this simple code. It will give you an idea on how you should create something with MySQL and PHP.
<?php
//your database connection code ...
echo "<select id='functie'>";
while($row = mysqli_fetch_array($result)) {
echo "<option value='".$row['id']."'>".$row['column']."</option>";
}
echo "</select>";
//your additional following codes ...
?>
Modify it to suit your needs.

Creating HTML table with php output

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.

Table html/CSS output error: unexpected T_STRING

I am new to web dev, PHP, CSS and html. I want to put a CSS in my table displaying the data on my database, but it doesn't work.
Here is my code:
My CSS file is named "table.css" ...
<html>
<head>
<title>WEW</title>
<head>
<link href="table.css" rel="stylesheet" type="text/css" />
</head>
<body>
<?php
$con = mysql_connect("localhost","abc123","abc123");
if (!$con)
{
die('Could not connect: ' . mysql_error());
}
mysql_select_db("database_ME", $con);
$result = mysql_query("SELECT * FROM id");
$data->set_css_class("table");
echo "<table class="table">
<tr>
<th>id</th>
<th>password</th>
</tr>";
while($row = mysql_fetch_array($result))
{
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
echo "<td>" . $row['id'] . "</td>";
echo "<td>" . $row['password'] . "</td>";
echo "</tr>";
}
echo "</table>";
echo "</div>";
mysql_close($con);
?>
</body>
</html>
I'm assuming the CSS file is well written, and that it uses the .table selector.
There's several syntax errors in there, all because you need to escape the inner " like so:
echo "A 'string with several \"nesting\" levels' needs escaping.";
echo "<table class="table">
change to
echo "<table class='table'>
AND
echo "<tr onmouseover="this.style.backgroundColor='#ffff66';" onmouseout="this.style.backgroundColor='#d4e3e5';">";
change to
echo "<tr onmouseover=\"this.style.backgroundColor='#ffff66';\" onmouseout=\"this.style.backgroundColor='#d4e3e5';\">";

Categories