I want to show mysql data in a html table... php echo shows me the content, but how can I transfer this into the html-table ? I've already added some dummy data manually into the html-table ..
<?php
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
}
} else {
echo "00 results";
}
$conn->close();
?>
<h1>Bestellungen</h1>
<div class="countdown" data-role="countdown" data-days="2"></div>
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<tr><td>2</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Filet</td><td>50</td></tr>
<tr><td>3</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Frisch</td><td>50</td></tr>
<tr><td>4</td><td>Sonne</td><td>Forellen</td><td>50</td><td>Lebend</td><td>50</td></tr>
</tbody>
</table>
</div>
You should iterate inside the table and add the html come in echo
<div class="container" style="padding: 25px 0px;">
<table class="table striped hovered border" data-role="datatable" data-searching="true">
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
echo "<tr>
<td>".$row["id"]. "</td>
<td>". $row["Datum"]. "</td>
<td>". $row["Kunde"] . "</td>
<td>". $row["Produkt"] . "</td>
<td>". $row["Produktversion"] . "</td>
<td>". $row["Menge"] ."</td>
</tr>";
}
?>
</tbody>
</table>
</div>
Remeber you have to loop each to create rows for the table.
$sql = "SELECT id, Datum, Kunde, Menge, Produkt, Produktversion FROM Aufträge";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
?>
<table>
<thead>
<tr>
<th>id.</th>
<th>Datum</th>
<th>Kunde</th>
<th>Produkt</th>
<th>Produktversion</th>
<th>Menge</th>
</tr>
</thead>
<tbody>
<?php
while($row = $result->fetch_assoc()) {
//echo "<br> ".$row["id"]. " ". $row["Datum"]. " " . $row["Kunde"] . $row["Produkt"] . $row["Produktversion"] . $row["Menge"] ."<br>";
?>
<td><?php echo $row["id"]; ?></td>
<td><?php echo $row["Datum"] ?></td>
<td><?php echo $row["Kunde"]; ?></td>
<td>---</td>
<td>---</td>
<td>---</td>
<?php
}
?>
</tbody>
</table>
<?php
} else {
echo "00 results";
}
$conn->close();
You need to create each and every row inside the while loop.
Related
So, I have this piece of HTML code which is just a navigation bar.
<div class="views">
<b>Views</b>
<hr style="background-color:#f0efef;">
<table id="views_table">
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_pending_cases">My Pending & On Hold Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_solved_cases">My Solved Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_csat_cases">My CSat Cases</td>
</tr>
<tr class="item">
<td class="view" id="my_open_cases">My Open Cases</td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><div>Escalation Review</div></td>
</tr>
<tr class="item">
<td class="view" id="WW Support Tier 1"><div>WW Support Tier 1</div></td>
</tr>
</table>
</div>
And then I have this php file which makes a query in my MySQL database and displays all the information in a table.
<?php
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM escalationreview";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
Now, what I want to achieve is: I want to call this php functiom everytime I click on an item in the navigation bar, then send the ID of the item clicked to the php function and query based on that ID, something like this:
HTML
<tr class="item">
<td class="view" id="WW Support Tier 1" onclick="fill(this.id)"><div>Escalation Review</div></td>
</tr>
PHP
<?php
function fill($id){
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
I know the above code does not work, it is just an idea of how I imagine this could work.
I believe this can be achieved using AJAX but I'm not completely sure. Any guidance here would be highly appreciated! Thanks beforehand.
If your PHP file renders the whole html, you might not need AJAX at all and just fill the link of each navigation item with the id as query parameter:
<td class="view" id="WW Support Tier 1">
<div>Escalation Review</div></td>
</tr>
Then in your PHP file, just get the ID as parameter
<?php
$id = $_REQUEST["id"] ?? null;
if($id) {
fill($id);
}
function fill($id) {
if ( !$conn = new mysqli("localhost", "root", "", "zendesk_data") ){
$output="Connection failed: " . $conn->connect_error;
} else {
$sql = "SELECT status, id, subject, requester, requested, requested_updated, service, next_sla_breach FROM $id";
if ( $result = $conn->query($sql) ){
if ($result->num_rows > 0) {
$output="<table class='queue_table'>
<tr align='left'>
<th>Status</th>
<th>ID</th>
<th>Subject</th>
<th>Requester</th>
<th>Requested</th>
<th>Requested Updated</th>
<th>Service</th>
<th>Next SLA Breach</th></tr>";
while($row = $result->fetch_assoc()) {
$output.= "<tr><td>". $row["status"]. "</td><td><a href='../tickets/new.php?tid=" . $row["id"] . "'>" . $row["id"]. "</a></td><td>" . $row["subject"]. "</td><td>" . $row["requester"]. "</td><td>" . $row["requested"]. "</td><td>". $row["requested_updated"]. "</td><td>".
$row["service"]. "</td><td>". $row["next_sla_breach"]. "</td></tr>";
}
$output.="</table>";
} else {
$output= "0 results";
}
} else {
$output="Error en la consulta: ".$conn->error;
}
$conn->close();
}
echo $output;
}
?>
let formData = new FormData();
formData.append('id', this.id);
$.ajax({
url: "./yourPage.php",
type: "POST",
data:formData,
processData: false,
contentType: false,
complete: (response) => {
console.log(response.responseText)
}
})
in yourPage.php:
function fill($_REQUEST["id"]){ .... your code that you wrote above}
This question already has answers here:
Creating dynamic tables in HTML using MySQL and PHP
(2 answers)
Closed 11 months ago.
I want to show the list of an array in a table, I want to insert each ID in the ID column, each name in the "name" column, etc...
But it's showing all the content in single column, How I can fix it?
It shows like that right now:
[![enter image description here][1]][1]
PHP:
<table class="blueTable">
<thead>
<tr>
<th>ID</th>
<th>USER</th>
<th>MAIL</th>
</tr>
</thead>
<tfoot>
<tr>
<td colspan="3">
<div class="links">« <a class="active" href="#">1</a> 2 3 4 »</div>
</td>
</tr>
</tfoot>
<tbody>
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
echo "<tr>";
while ($row = $query->fetch_array()) {
echo "<td>" . $row['id'] . "</td>";
echo "<tr><td>" . $row['username'] . "</td></tr>";
echo "<tr><td>" . $row['email'] . "</td></tr>";
}
?>
</tbody>
</table>
[1]: https://i.stack.imgur.com/jPTR8.png
change while() loop code like below:
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
echo "<tr>"; //put <tr> opening code inside, not outside
echo "<td>".$row['id']."</td>";
echo "<td>".$row['username']."</td>"; //remove <tr></tr>
echo "<td>".$row['email']."</td>"; //remove <tr></tr>
echo "</tr>"; //</tr> need to be added at last
}
Try this
<?php
$query = $db->query("SELECT * FROM users ORDER by id");
while($row = $query->fetch_array()){
?>
<tr>
<td> <?php echo $row['id'] ?></td>
<td> <?php echo $row['username'] ?> </td>
<td> <?php echo $row['email'] ?> </td>
</tr>
<?php
}
?>
I have a question in relation to displaying PHP tables that should be straight forward but I cannot get my head around it at the moment so any help would be appreciated, basically what I want to do is display a team of players in a table, but display multiple tables of users with their team name display above it.
What I currently have : http://puu.sh/ilUJp/4a6ae5e47b.png
What I am looking to achieve : http://puu.sh/ilUJ8/7756033517.png
<div class="col-lg-6">
<h3>Team Name Goes Here </h3>
<?php
echo "<table class='table table-striped'>";
echo " <thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
";
while($row = mysqli_fetch_array($result)) {
$teamName = $row['teamName'];
$fName = $row['firstName'];
$surName = $row['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
?>
</div>
with my query :
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ";
I know the idea I need to do but cannot get it done, so any help would be appreciated.
Try this code
<?php $teemid=array();
while($row = mysqli_fetch_array($result)) {
if(!in_array($row['teamID'],$teemid)){
array_push($teemid,$row['teamID']);
if(!empty($teemid)){ ?>
</tbody>
</table>
</div>
<?php }
?>
<div class="col-lg-6">
<h3><?php echo $row['teamName']; ?></h3>
<table class='table table-striped'>
<thead>
<tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<?php } ?>
<tr>
<td><?php echo $row['teamName']; ?></td>
<td><?php echo $row['firstName']; ?></td>
<td><?php echo $row['surName']; ?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
SQL Query Change as below
$sql = "SELECT t.teamID,t.teamName,u.firstName,u.surName From users as u INNER JOIN team as t where u.teamID = t.teamID ORDER BY u.teamID";
You can do this logic
$teams = "get all teams sql query";
while ($row = mysqli_fetch_array($teams)) {
$teamid = $row['teamid'];
$teamname = $row['teamname'];
$teammemberquery = "select all member in the where team = $teamid sql query";
echo "<table>";
while ($r = mysqli_fetch_array($teammemberquery)) {
$teamName = $r['teamName'];
$fName = $r['firstName'];
$surName = $r['surName'];
echo "
<tbody>
<tr>
<td>$teamName</td>
<td>$fName</td>
<td>$surName</td>
</tr>
</tbody>
";
}
echo "</table>";
}
Try as below (Please replace table column name as yours and mysql to mysqli):
<?php
$link = mysql_connect('localhost', 'root', 'root');
$db_selected = mysql_select_db('test', $link);
$sql = "SELECT t.team_id,t.team,u.fname,u.lname,u.email From users as u INNER JOIN team as t where u.team_id = t.team_id order by t.team_id ";
$result = mysql_query($sql);
?>
<html><head><title>team</title></head><body><div class="col-lg-6">
<?php
echo "<table>";
$teamName = "";
$i=0;
while($row = mysql_fetch_array($result))
{
if($teamName == "" || $teamName != $row['team'])
{
if($i!=0)
echo "</table>";
echo "<tr><td colspan='3'><h3>".$row['team']."</h3></td></tr>";
$teamName = $row['team'];
$i=0;
}
$fName = $row['fname'];
$surName = $row['lname'];
$email = $row['email'];
if($i==0)
{
echo "<table class='table table-striped'><tr>
<th>Firstname</th>
<th>Lastname</th>
<th>Email</th>
</tr>";
}
echo "<tr>
<td>$fName</td>
<td>$surName</td>
<td>$email</td>
</tr>";
$i++;
}
echo "</table>";
?>
</div></body></html>
So far, I have this program that links my phpmyadmin database to my php script. Now, I need to display certain things in a table like "all records," "all contacts whose last name starts with S," "all pet owners," etc. My question is: is there a simpler way to insert code into my php script to display the information from my database. Right now I have that long echo statement to display the information. Is there a way I can just use something like a SELECT * statement to display all records and to simplify my code?
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli( $db_hostname,
$db_username,
$db_password,
$db_database);
if ($connection->connect_error) {
echo "Sorry";
} else {
echo "Connected!<br><br>";
$sql = "SELECT * FROM People";
$result = $connection->query($sql);
if (!$result) die ($connection->error);
$n = $result->num_rows;
for ($i=1; $i<=$n; $i++) {
$row = $result->fetch_array(MYSQLI_ASSOC);
echo "<table>
<tr><th>ID</th><th>First Name</th><th>Last Name</th>
<th>Street Address</th><th>City</th>
<th>State</th><th>Zip Code</th>
<th>Email Address</th><th>Comment</th>
<th>Number of pets</th></tr>";
echo "<tr><td width=20>" . $row['iD'] . "</td><td>" . $row['First Name'] . "</td><td width=40>" .
$row['Last Name'] . "</td><td width=200>" . $row['Street Address'] . "</td><td width=30>" .
$row['City'] . "</td><td width=40>" . $row['State'] . "</td><td width=30>" .
$row['Zip Code'] . "</td><td width=40>" . $row['Email Address'] . "</td><td width=20>" .
$row['Comment'] . "</td><td width=10>" . $row['Number of pets'] . "</td></tr>";
}
echo "</table>";
}
?>
You should a table structure first then insert your PHP codes within the structure. E.g:
<?php
// Put your MYSQLI retrievals codes here
?>
<table>
<tr>
<th>FIELD_1</th>
<th>FIELD_2</th>
<th>FIELD_3</th>
</tr>
<?php
while ($rows = $result->fetch_array(MYSQLI_ASSOC))
{
?>
<tr>
<td><?php echo $rows['field_1']; ?></td>
<td><?php echo $rows['field_2']; ?></td>
<td><?php echo $rows['field_3']; ?></td>
</tr>
<?php
}
?>
</table>
Technically you are doing everything alright, but there are some more sophisticated ways to develop the things you want.
Take a look at the following links:Object oriented programming in PHP and templating in PHP.
Hope this works for you:
<?php
$db_hostname='localhost';
$db_username='root';
$db_password='';
$db_database='Address Book';
$connection = new mysqli($db_hostname,$db_username,$db_password,$db_database);
if ($connection->connect_errno) {
echo "Failed to connect to MySQL: (" . $mysqli->connect_errno . ") " . $mysqli->connect_error;
} else {
echo "Connected!<br><br>";
}
$last_name = "Lastname to search"; //You can post or get lastname in here.
/* create a prepared statement */
if ($sql = $connection->prepare("SELECT * FROM People WHERE `Last Name`=?")) {
/* bind parameters for markers */
$sql->bind_param("s", $last_name);
/* execute query */
$sql->execute();
/* instead of bind_result: */
$result = $sql->get_result();
/* close statement */
$sql->close();
}
?>
<table>
<thead>
<tr>
<th>ID</th>
<th>First Name</th>
<th>Last Name</th>
<th>Street Address</th>
<th>City</th>
<th>State</th>
<th>Zip Code</th>
<th>Email Address</th>
<th>Comment</th>
<th>Number of pets</th>
</tr>
</thead>
<tbody>
<?
/* now you can fetch the results into an array */
while ($row = $result->fetch_assoc()) {
?>
<tr>
<td width=20><?=$row['iD'];?></td>
<td><?=$row['First Name'];?></td>
<td width=40><?=$row['First Name'];?></td>
<td width=200><?=$row['Street Address'];?></td>
<td width=30><?=$row['City'];?></td>
<td width=40><?=$row['State'];?></td>
<td width=30><?=$row['Zip Code'];?></td>
<td width=40><?=$row['Email Address'];?></td>
<td width=20><?=$row['Comment'];?></td>
<td width=10><?=$row['Number of pets'];?></td>
</tr>
<?
}
?>
</tbody>
</table>
I have this form to search names in mysql database
<form action="search.php" method="GET">
<input type="text" placeholder="Search" name="name">
<input type="submit" value="Search">
this is the search.php
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Description</th>
</tr>
</thead>
<tbody>";
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
}
echo
"</tbody>
</table>";
?>
so there is no problems when I search for a name that exists in database it displays correctly, but the problem comes when I search for a name that doesn't exist in database.. I want it to display only
"No data available for that name specified" for the output but I will also see empty table in the output like this ------------> IMAGE..
so how can I get rid of the empty table for the output?
Just pu the if outside the table....
<?php
name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
?>
change your if clause as below and remember to add exit() or die() function,this will end your php if there is no any data in database, and if there is any it will then start creating table for once and repeatedly fill up the table rows for given rows of data on database.
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
exit();
} else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while($row=mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>".$name."</td>
<td>".$email."</td>
<td>".$desc."</td>
</tr>";
}
echo
"</tbody>
</table>";
}
Move your echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
into the the if statement. This way it will only display the table when data is available!
$name = $_GET['name'];
require_once("connect.php");
$records = $connect->query("SELECT * FROM Userlists WHERE Name = '$name'");
if (mysqli_num_rows($records)== 0){
echo "No data available for that name specified";
}
else {
echo
"<table>
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Desc</th>
</tr>
</thead>
<tbody>";
while ($row = mysqli_fetch_array($records)) {
$name = $row['Name'];
$email = $row['Email'];
$desc = $row['Desc'];
echo
"<tr>
<td>" . $name . "</td>
<td>" . $email . "</td>
<td>" . $desc . "</td>
</tr>";
}
echo "</tbody></table>";
}
Try this one. But don't forget to escape $_GET['name'] like htmlspecialchars and real_escape_string