bootstrap display data from mysql in table - php

I am trying to display my mysql rows to html bootstrap table. Database connection is working, displaying data is working, but it is not fancy as i want. I'd like to maybe save in arrayData maybe and then print this arrayData in html tag. Please any suggestions much appreciated. I want to do this easiest way and most convenient for editing later on. php code to display data :
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo "id: " . $row["id"]. " - Name: " . $row["name"]. " " . $row["lastName"]. "<br>"; /* and so on..*/
}
} else {
echo "0 results";
}
and this is my html code for bootstrap
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<tr>
<th scope="row">1</th>
<td>ales</td>
<td>king</td>
<td></td>
<td></td>
</tr>
<tr>
<th scope="row">2</th>
<td>love</td>
<td>2</td>
<td>code</td>
<td></td>
</tr>
</tbody>
</div>
</table>
EDIT: i want this, but loaded from database not manually typed :)!

You can use a php loop this way
<table class="table table-striped">
<div class="table responsive">
<thead>
<tr>
<th>#</th>
<th>Name</th>
<th>Last Name</th>
<th>Number</th>
<th>Info</th>
</tr>
</thead>
<tbody>
<?php
....
if ($result->num_rows > 0) {
// output data of each row
while($row = $result->fetch_assoc()) {
echo '<tr>
<td scope="row">' . $row["id"]. '</td>
<td>' . $row["name"] .'</td>
<td> '.$row["lastName"] .'</td>
</tr>';
}
} else {
echo "0 results";
}
?>
</tbody>
</div>
</table>

I would do something like this:
( but you should really read some basic PHP )
<?php
echo "<table>";
// table header
echo "<tr><th>id</th><th>Name</th><th>Lastname</th></tr>";
// output data of each row
while($row = $result->fetch_assoc()) {
echo "<tr><td>".$row["id"]."</td><td>".$row["name"]."</td><td>".$row["lastName"]."</td></tr>";
}
// table footer
echo "</table>";
?>

Related

how to change table cell color depending on values in php

I just started programming in php. I want to change cell color depending on some values. For example if quantity < 1000 it should display red color. I don't understand how to do it. Please help me. This is my code. This is what I have tried so far. please let me know what is wrong and what needs to be done. Thanx in advance
<div class="c-content-panel">
<div class="c-label">Basic example</div>
<div class="c-body">
<div class="row">
<div class="col-md-12">
<table class="table">
<caption>Optional table caption. </caption>
<thead>
<tr>
<th>#</th>
<th>Store Id</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row =mysqli_fetch_array($query))
{
echo " <tr>
<td>--</td>
<td>{$row['store_id']}
</td>
<td>{$row['store_name']}</td>
<td>{$row['quantity']}</td>
if($row['quantity']<1000)
{
echo "<td style='background-color: #FF0000;'></td>"
}
else
{
echo "< td style='background-color: #FFFF00;'></td>"
}
</tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['mphone']}</td>
<td>{$row['quantity_mphone']}</td> </tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['smart_devices']}</td>
<td> {$row['quantity_smart_devices']}</td> </tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['power']}</td>
<td>{$row['quantity_power']}</td>
</tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['audio']}</td>
<td>{$row['quantity_audio']}</td>
</tr>";}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
You can't write if in echo. You have to terminate echo before using if
<div class="c-content-panel">
<div class="c-label">Basic example</div>
<div class="c-body">
<div class="row">
<div class="col-md-12">
<table class="table">
<caption>Optional table caption. </caption>
<thead>
<tr>
<th>#</th>
<th>Store Id</th>
<th>Item Name</th>
<th>Quantity</th>
<th>Status</th>
</tr>
</thead>
<tbody>
<?php while ($row =mysqli_fetch_array($query))
{
echo " <tr>
<td>--</td>
<td>{$row['store_id']}
</td>
<td>{$row['store_name']}</td>
<td>{$row['quantity']}</td>";
if($row['quantity']<1000)
{
echo "<td style='background-color: #FF0000;'></td>";
}
else
{
echo "< td style='background-color: #FFFF00;'></td>";
}
echo "</tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['mphone']}</td>
<td>{$row['quantity_mphone']}</td> </tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['smart_devices']}</td>
<td> {$row['quantity_smart_devices']}</td> </tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['power']}</td>
<td>{$row['quantity_power']}</td>
</tr>";
echo " <tr>
<td>--</td>
<td>{$row['store_id']}</td>
<td>{$row['audio']}</td>
<td>{$row['quantity_audio']}</td>
</tr>";}
?>
</tbody>
</table>
</div>
</div>
</div>
</div>
1st: Simple break the previous echo using ;
2nd : simple use ternary operator
echo "<td style='background-color:".(($row['quantity'] < 1000) ? '#FF0000;' : '#FFFF00')."'></td>";

Outputting Data into an HTML table using php/html

I am trying to output data onto my webpage from a database. I am able to so successfully, but in a very untidy way. I have tried to put the data in a table on the website with no success.
Below, data is retrieved from the db, but just echoed out crudely. It looks untidy but it outputs successfully onto the webpage
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
echo "Name: " . $row["name"] . " - Email: " . $row["email"] . " - Address: " . $row["address"] . "<br>";
}
} else {
echo "0 results";
}
mysqli_close($conn);
I try to put the data into an HTML table(on the same page, by combining PHP and HTML with no success. How can put this data into an organised table successfully?
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
</tbody>
</table>
</div>
Below is roughly how I would like the data to be structured, how can achieve this?
Name | Email | Address
Jo |J#o.com|12 Street
Ben |B#e.com|23 street
try this:
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<?php
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php } ?>
</tbody>
</table>
</div>
Try below code :
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<tr>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) { ?>
<td><?php $row["name"]?></td>
<td><?php $row["email"]?></td>
<td><?php $row["address"]?></td>
<?php }
} ?>
</tr>
</tbody>
</table>
</div>
<?php
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
?>
<div class="container">
<h2>Bordered Table</h2>
<p>The .table-bordered class adds borders to a table:</p>
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead> <tbody>
<?php
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
?>
<tr>
<td><?php $row["name"] ?></td>
<td><?php $row["email"] ?></td>
<td><?php $row["address"]?></td>
</tr>
<?php }
} else {
echo "<tr><td colspan=3>0 results</td></tr>";
}
mysqli_close($conn);
?>
</tbody>
</table>
</div>
You can always print the results from the select like this:
while ($row = mysqli_fetch_assoc($result)) {
echo "<tr>
<td>".$row["name"]."</td>
<td>".$row["email"]."</td>
<td>".$row["address"] . "</td></tr>";
}
It's not a very clean way, but for each row should print another row, below the three table headers.
There isn't really a very neat way to do this beyond the other answers.
But what you can do is keep the HTML and PHP separate as much as possible in the same file.
The PHP at the top of the file can be as follows:
$query = "SELECT name, email, address FROM SHHowners";
$result = mysqli_query($conn, $query);
$htmlToDisplay = ''; //this variable will contain table rows
if (mysqli_num_rows($result) > 0) {
// output data of each row
while ($row = mysqli_fetch_assoc($result)) {
//create the HTML row - we'll use this later
$htmlToDisplay .= "<tr><td>".$row['name']."</td><td>". $row['email']."</td><td>".$row['address']."</td></tr>";
}
} else {
$htmlToDisplay = "<tr><td>0 results</td><tr>";
}
Then you can have your HTML after your closing PHP tag (?>) as follows:
<table class="table table-bordered">
<thead>
<tr>
<th>Name</th>
<th>Email</th>
<th>Address</th>
</tr>
</thead>
<tbody>
<!-- echo your php row(s) here in a slightly neater way since it's one line -->
<?php echo $htmlToDisplay; ?>
</tbody>
</table>
This will make the the different parts of the file (the PHP and HTML) more readable.
You could also look at using a PHP template engine like smarty.

What is the best way to write heredocs in php and how to keep function max 20 lines

Following is the code of Class Admin that contains two method
listRecord();
Details($id);
The code is working fine but i don't like this code because function is huge and coupled and i don't want to use heredocs in function Kindly guide me what is the best way to write heredocs and how to separate heredocs in separate function.
Admin Class
Class Admin{
public function listRecord() {
$query = "select Report_type, Report_Id, Report_Reults,Patient_name, Patient_Address, "
. "Patient_Phone from Patient join Reports "
. "on Patient.Patient_ID = Reports.Patient_ID ";
$result = $this->conn->query($query);
if ($result->num_rows > 0) {
// output data of each row
echo <<<HTML
<h2>Report's Data</h2>
<table>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Patient Phone</th>
<th>Report Type</th>
<th>Report Results</th>
</tr>
<tr>
HTML;
while ($row = $result->fetch_assoc()) {
echo"
<tr>
<td> $row[Patient_name] </td>
<td> $row[Patient_Address] </td>
<td> $row[Patient_Phone] </td>
<td> $row[Report_type] </td>
<td> $row[Report_Reults] </td>
<td><a href='../Admin/AdminData.php?del_id=$row[Report_Id]'>Delete</a></td>
<td><a href='../Admin/AdminData.php?report_id=$row[Report_Id]'>Details</a></td>
</tr>";
}
echo "</table>";
}
}
}
public function Details($id) {
$this->data_id = $id;
$query = "select Report_type, Report_Id, Report_Reults,Patient_name, Patient_Address, "
. "Patient_Phone from Patient join Reports WHERE "
. "Reports.Report_Id=$this->data_id and Patient.Patient_ID=$this->data_id ";
$result = $this->conn->query($query);
if ($result->num_rows > 0) {
// output data of each row
echo <<<HTML
<h2>$row[Patient_name] Data</h2>
<table>
<tr>
<th>Patient Name</th>
<th>Patient Address</th>
<th>Patient Phone</th>
<th>Report Type</th>
<th>Report Results</th>
</tr>
<tr>
HTML;
while ($row = $result->fetch_assoc()) {
echo"
<tr>
<td> $row[Patient_name] </td>
<td> $row[Patient_Address] </td>
<td> $row[Patient_Phone] </td>
<td> $row[Report_type] </td>
<td> $row[Report_Reults] </td>
</tr>";
}
echo "</table>";
}
}
Try saving the HTML for both functions in different files and including them when required. You could use .phtml format for these files, which is HTML with some PHP.
if ($result->num_rows > 0) {
require_once FILE_PATH; // Path to the specific file needed in the function.
}

Loading an image

echo '<table class="table table-striped">
<tr>
<th>Character Name</th>
<th>Ban Reason</th>
<th>Ban Length</th>
<th>Proof</th>
<th>Banned On</th>
</tr>';
while($row = $result->fetch_assoc()){
echo '<tr>
<td>'.$row['charactername'].'</td>
<td>'.$row['banMessage'].'</td>
<td>'.convertToHumanReadableTime($row['banDuration']).'</td>
<td>'<img src='".$row['spy']."' />;'</td>
<td>'.date("m/d/Y", strtotime($row['banTime'])).'</td>
</tr>';
}
echo '</table>';
the Image loading isn't working cause idk it should get the link ending on .jpg inside the db
You have an issue with your quotes.
Change
echo '<tr>
<td>'.$row['charactername'].'</td>
<td>'.$row['banMessage'].'</td>
<td>'.convertToHumanReadableTime($row['banDuration']).'</td>
<td>'<img src='".$row['spy']."' />;'</td>
<td>'.date("m/d/Y", strtotime($row['banTime'])).'</td></tr>';
To
echo '<tr>
<td>'.$row['charactername'].'</td>
<td>'.$row['banMessage'].'</td>
<td>'.convertToHumanReadableTime($row['banDuration']).'</td>
<td><img src="'.$row['spy'].'" />;</td>
<td>'.date("m/d/Y", strtotime($row['banTime'])).'</td></tr>';

getting data from MYSQL database to a html tabel using php

I design a table with html (bootstrap style) now i want to get data from my database table and fetch it in table using php, here is my html code:
Thanks for you help. :)
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
<th>Province</th>
<th>User</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
PHP code i tried but i didnt get any result it gives me errors:
<?php
$username = "root";
$password = "=";
$host = "localhost";
$connector = mysql_connect($host,$username,$password)
or die("Unable to connect");
echo "Connections are made successfully::";
$selected = mysql_select_db("user", $connector)
or die("Unable to connect");
//execute the SQL query and return records
$result = mysql_query("SELECT * FROM table_one ");
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<tr class="gradeX">
<td>test</td>
<td>test</td>
<td>test</td>
<td class="center">test</td>
<td class="center">test</td>
</tr>
</tfoot>
</table>
</div>
</div>
<?php
while( $row = mysql_fetch_assoc( $result ) ){
echo
"<tr>
echo "<td>" . $row['Category'] . "</td>";
<td>{$row\['regdate'\]}</td>
<td>{$row\['occdate'\]}</td>
</tr>\n";
}
?>
Since mysql_* is depreciated I will answer this using mysqli_*.
You have already closed the table in your example before looping through the fetched data so I am unsure where you want these rows to populate however I made a guess.
Another error with your attempt is you have echo " ... echo "";
<?php
$mysqli = new mysqli(HOST, USER, PASSWORD, DATABASE);
if (mysqli_connect_error()) {
die('Connect Error (' . mysqli_connect_errno() . ') '
. mysqli_connect_error());
}
$query = 'SELECT * FROM table_one';
$data = mysqli_query($mysqli, $query);
?>
<div class="ibox-content">
<table class="table table-striped table-bordered table-hover dataTables-example" >
<thead>
<tr>
<th>Category</th>
<th>Register Date</th>
<th>Occurrence Date</th>
</tr>
</thead>
<tbody>
<?php
while($row = mysqli_fetch_array($data))
{
echo " <tr>
<td>" . $row['Category'] . "</td>
<td>" . $row['regdate'] . "</td>
<td>" . $row['occdate'] . "</td>
</tr>";
}
?>
</tbody>
<tfoot>
</tfoot>
</table>
</div>
I have indented the echo inside the while loop to keep it inline with the other html.

Categories