Display info from database into HTML table in different cells - php

What I am trying to do is to assign a cell from my HTML table to a specific record from my database. I am doing a table with COLSPAN, so it is not the same number of columns in every row. The code for the table is something like this:
<!DOCTYPE html>
<html>
<head>
<style>
table, th, td {
border: 1px solid black;
}
</style>
</head>
<body>
<table>
<tbody>
<tr>
<td colspan="4">TITLE
</td>
</tr>
<tr>
<td colspan="2">tag1</td>
<td colspan="2">info from database</td>
</tr>
<tr>
<td >tag2</td>
<td>info from database</td>
<td>tag3</td>
<td>info from database</td>
</tr>
</tbody>
</table>
</body>
</html>
This would be the query
$con = new mysqli($servername, $username, $password, $database);
if ($con->connect_error) {
die("Connection failed: " . $con->connect_error);
}
$sql = "SELECT * FROM table WHERE id=id ";
$result = $con->query($sql);
Any help would be great! Thanks

You can simply fetch your results and for every result you echo your table out
while($row = mysqli_fetch_assoc($result)) {
echo "
<tr>
<td colspan='4'>TITLE
</td>
</tr>
<tr>
<td colspan='2'>tag1</td>
<td colspan='2'>".$row['NAME OF THE ROW']."</td>
</tr>
<tr>
<td >tag2</td>
<td>".$row['NAME OF THE ROW']."</td>
<td>tag3</td>
<td>".$row['NAME OF THE ROW']."</td>
</tr>
";
}

Please use the given below code to show you db data in table
while($result = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>".$res['name']."</td>";
echo "<td>".$res['age']."</td>";
echo "<td>".$res['email']."</td>";
echo "<tr>";
}

Related

Datatables PHP MYSQL - How to display specific entries

I have a database with the following columns:
(okladka, imie, nazwisko, tytul, rodzaj, info, download, data)
My DB name: serwer144221_SwiatBook
Table name: bazaplikow
In the value "rodzaj" each entry contains one word: "audiobook" or "ebook"
I would like to display in the table only those entries where the word in "ebook" appears.
How to read all rows from a database table where the column "rodzaj" contains a value "ebook"
This is my Source Code Datatables:
<?php
$servername = "xyz";
$username = "xyz";
$password = "xyz";
$database = "serwer144221_SwiatBook";
// Create connection
$connection = new mysqli($servername, $username, $password, $database);
// Check connection
if ($connection->connect_error) {
die("Connection failed: " . $connection->connect_error);
}
// read all row from database table
$sql = "SELECT DISTINCT rodzaj * FROM bazaplikow";
$result = $connection->query($sql);
if (!$result) {
die("Invalid query: " . $connection->error);
}
?>
<section class="section-1">
<h3 align="center">Baza plików</h3>
<br />
<div class="table-responsive">
<table id="bazaplikow_data" class="table table-striped table-bordered" width="100%";>
<thead>
<tr>
<td align="center">Okładka</td>
<td align="center">Imię</td>
<td align="center">Nazwisko</td>
<td align="center">Tytuł</td>
<td align="center">Rodzaj</td>
<td align="center">Info</td>
<td align="center">Download</td>
<td align="center">Data</td>
</tr>
</thead>
<?php
while($row = mysqli_fetch_array($result))
{
echo '
<tr>
<td align="center"><img src="'.$row["okladka"].'" width="98" height="128"></td>
<td align="center">'.$row["imie"].'</td>
<td align="center">'.$row["nazwisko"].'</td>
<td align="center">'.$row["tytul"].'</td>
<td align="center">'.$row["rodzaj"].'</td>
<td align="center"><i class="fa fa-info-circle" aria-hidden="true"></i></td>
<td align="center"><i class="fa fa-download" aria-hidden="true"></i></td>
<td align="center">'.$row["data"].'</td>
</tr>
';
}
?>
</table>
</div>
</section>
<script>
$(document).ready(function () {
$('#bazaplikow_data').DataTable({
order: [[7, 'desc']],
});
});
</script>
Your query is wrong it should be
SELECT * from bazaplikow where rodzaj = 'ebook';

Display image PHP MYSQLI use WHERE condition

I have a program that insert many records and image to a database. I must display my records and image into the web page (forms-output.php) but only 4 records and images. If I click the link (example: link in the ref_code) and records and image must display into a new web page (forms-full-data.php) but only records and image when I click what to display. Almost all work has been completed, but when I click a field in the ref_code all records have been successfully displayed except the images. Can you help me please?
This is forms-output.php:
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "profil";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
echo '
<div class="table-responsive">
<table class="table table-bordered table-striped mb-none" id="datatable-tabletools" data-swf-path="assets/vendor/jquery-datatables/extras/TableTools/swf/copy_csv_xls_pdf.swf">
<thead>
<tr align="center" bgcolor="#E9E9E9">
<td align="center" width="1"><b>No</td>
<td width="10%" align="center"><b>Photo</td>
<td width="100" align="center"><b>Ref Code</td>
<td width="100" align="center"><b>Name</td>
<td width="100" align="center"><b>Date</td>
</tr>
<tr>';
$no = 1; //inisialisasi untuk penomoran data
$sql= "SELECT image, name, ref_code, image, overseas, avaibility, sector, country, date, height, weight, religion, status, children, education, language, language2, experience1, experience2, experience3, experience4, experience5, other1, other2, other3, other4, other5, other6, working_experience, working_experience2, working_experience3, working_experience4, working_experience5 FROM tb_profil";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
while($row = $result->fetch_assoc()) {
$gambar="<img src='images/" . $row["image"] . "' width='200'" . "' height='150' ></td><td align='center'>";
$data="<b>$row[name]<br><br>$row[ref_code]<br><br>$row[overseas]<br><br>$row[avaibility]<br><br>$row[sector]<br><br>$row[country]<br><br>$row[date]<br><br>$row[height]<br><br>$row[weight]<br><br>$row[religion]<br><br>$row[status]<br><br>$row[children]<br><br>$row[education]<br><br>$row[language]<br><br>$row[language2]<br><br>$row[experience1]<br><br>$row[experience2]<br><br>$row[experience3]<br><br>$row[experience4]<br><br>$row[experience5]<br><br>$row[other1]<br><br>$row[other2]<br><br>$row[other3]<br><br>$row[other4]<br><br>$row[other5]<br><br>$row[other6]<br><br>$row[working_experience]<br><br>$row[working_experience2]<br><br>$row[working_experience3]<br><br>$row[working_experience4]<br><br>$row[working_experience5]$row[image]";
echo "<tr><td align='center'>" .$no. "</td><td>$gambar<a href='forms-full-data.php?p=$data'>" . $row["ref_code"] . "</td><td align='center'>" . $row["name"] . "</td><td align='center'>" . $row["date"] . "</td></tr>";
$no++;
}
echo "</table>";
} else {
echo "0 results";
}
?>
This is forms-full-data.php:
<?php
// server info
$host = 'localhost';
$user = 'root';
$password = '';
$database_name = 'profil';
$mysqli = new mysqli($host, $user, $password, $database_name);
$res = $mysqli->query("SELECT * FROM tb_profil WHERE ref_code='" . $_GET['p']."'");
$row = $res->fetch_assoc();
$gambar="<img src='images/" . $row["image"] . "' width='150'" . "' height='200' >";
if (isset($_GET['p'])) {
echo "<table width='100%' border='0'>
<tr>
<td rowspan='31' width='20'>
<td width='180'>Full Name</td>
<td rowspan='15' width='230'> </td>
<td rowspan='31' width='200'>$_GET[p]</td>
<td rowspan='5' width='200'>$gambar</td>
</tr>
<tr>
<td>Ref Code</td>
</tr>
<tr>
<td>Overseas Experience</td>
</tr>
<tr>
<td>Avaiability</td>
</tr>
<tr>
<td>Sector</td>
</tr>
<tr>
<td>Country</td>
</tr>
<tr>
<td>Date Of Birth</td>
</tr>
<tr>
<td>Height</td>
<td></td>
</tr>
<tr>
<td>Weight</td>
<td rowspan='17'> </td>
</tr>
<tr>
<td>Religion</td>
</tr>
<tr>
<td>Marital Status</td>
</tr>
<tr>
<td>Children</td>
</tr>
<tr>
<td>Education</td>
</tr>
<tr>
<td>Language</td>
</tr>
<tr>
<td>More Language</td>
</tr>
<tr>
<td rowspan='5'>Experience</td>
<td width='10'>Care For Children</td>
</tr>
<tr>
<td>Cooking</td>
</tr>
<tr>
<td>Care For Infant</td>
</tr>
<tr>
<td>Care For Newborn</td>
</tr>
<tr>
<td>Care For Elderly</td>
</tr>
<tr>
<td rowspan='6'>Other Information</td>
<td>Able To Handle Pork?</td>
</tr>
<tr>
<td>Able To eat Pork?</td>
</tr>
<tr>
<td>Able to care for dog/cat?</td>
</tr>
<tr>
<td>Able to swim?</td>
</tr>
<tr>
<td>Willing to lock after elderly forbidden?</td>
</tr>
<tr>
<td>Willing to work with no off days?</td>
</tr>
<tr>
<td>Working Experience</td>
<td rowspan='5'></td>
</tr>
<tr>
<td>More Working Experience 2</td>
</tr>
<tr>
<td>More Working Experience 3</td>
</tr>
<tr>
<td>More Working Experience 4</td>
</tr>
<tr>
<td>More Working Experience 5</td>
</tr>
</table>";
}
?>
Add '/', before 'images' catalog, while You build path.
Please read about 'SQL injection".
Please use ADODB to manage DB.

Use Select option as search with dynamic fields

I am making a site linked with a database. It sorts transactions for budgeting reasons. For the store search section, I have a select drop down menu. I have linked up the data so that the drop down only shows stores that are in the database and automatically adds them as they change dynamically.
My issue is that I need a way to actually use the select options as search terms. Here is the initial page.
<!DOCTYPE html>
<html>
<head>
<title>Output 1</title>
</head>
<body>
<h1>Required Output 1</h1>
<h2>Transaction Search</h2>
<form action="output1out.php" method="get">
Search Store:<br/>
<input type="text" name="StoreName">
<br/>
<input name="Add Merchant" type="submit" value="Search">
</form>
<?php
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
if(mysqli_connect_error()){
die("Database Connection Failed: ".mysqli_connect_error()." (".mysqli_connect_errno().")");
};
$query = "SELECT * from PURCHASE";
//echo $query;
$result = mysqli_query($connection,$query);
if(!$result) {
die("Database Query Failed!");
};
$distinct = "SELECT DISTINCT StoreName FROM PURCHASE";
$distinctResult = mysqli_query($connection,$distinct);
if(!$result) {
die("Database Query Failed!");
};
echo '<select name="search_string" />'."\n";
while ($row = mysqli_fetch_assoc($distinctResult)){
echo '<option value="'.$row["StoreID"].'">';
echo $row["StoreName"];
echo '</option>'."\n";
};
echo '</select>'."\n";
mysqli_free_result($result);
mysqli_close($connection);
?>
Here is the output page.
<?php
$transaction = $_REQUEST["StoreName"];
require_once 'login.php';
$connection = mysqli_connect($db_hostname, $db_username,$db_password, $db_database);
$sql = "SELECT * FROM PURCHASE WHERE StoreName LIKE '%".$transaction."%'";
$result = $connection->query($sql);
?>
Purchases Made From <?php echo $transaction ?>
<table border="2" style="width:100%">
<tr>
<th width="15%">Item Name</th>
<th width="15%">Item Price</th>
<th width="15%">Purchase Time</th>
<th width="15%">Purchase Date</th>
<th width="15%">Category</th>
<th width="15%">Rating</th>
</tr>
</table>
<?php
if($result->num_rows > 0){
// output data of each row
while($rows = $result->fetch_assoc()){ ?>
<table border="2" style="width:100%">
<tr>
<td width="15%"><?php echo $rows['ItemName']; ?></td>
<td width="15%"><?php echo $rows['ItemPrice']; ?></td>
<td width="15%"><?php echo $rows['PurchaseTime']; ?></td>
<td width="15%"><?php echo $rows['PurchaseDate']; ?></td>
<td width="15%"><?php echo $rows['Category']; ?></td>
<td width="15%"><?php echo $rows['Rating']; ?></td>
</tr>
<?php
}
}
?>
I can get regular typing search to work but I can't think of a way to search using the same method. Is it possible?

PHP Include Page which shows information based on value of Page 1

Have been struggling with making this work. Not sure where I'm going wrong.
main page: pkg_list_30d.php
<?php
$conn = new mysqli('host', 'user', 'pwd', 'db');
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$sql = "SELECT pkg.*, i.isotope AS pkgisotope
FROM tbl_packagereceipt pkg
INNER JOIN tbl_isotopes i
on
pkg.isotope = i.isoID
GROUP BY pkg.pkgID
ORDER BY `datereceived` DESC
LIMIT 5";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
echo '
<table width="568" border="0" cellspacing="1" cellpadding="1">
<tr>
<td width="80" > </td>
<td width="110"> </td>
<td width="108"> </td>
<td width="150"> </td>
<td width="120" > </td>
</tr>
<tr>
<td> </td>
<td><strong>Date</strong></td>
<td><strong>Package #</strong></td>
<td><strong>Isotope</strong></td>
<td> </td>
</tr>
</table>';
$c = false;
while($row = $result->fetch_assoc())
{
echo '<table width="568" border="0" cellspacing="1" cellpadding="1">
<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">
<td width="80"> </td>
<td width="110">'.date('d-M-Y', strtotime($row['datereceived'])).'</td>
<td width="108">'.$row['pkgnumber'].'</td>
<td width="150">'.$row['pkgisotope'].'</td>
<td width="120">' . '<a class="gegevens2"
href="../patientinjection/record_inj_form.php?id=' . $row['pkgID'] . '"> ' .
"Add Patient". '</a>' . '</td>
</tr>
</table>
<br />';
echo include 'pkg_patient.php';
}
}
else {
echo 'All packages returned.';
}
$conn->close();
?>
Directly under the table within the while() I would like it to display the patients for that result. I have tried doing an include of a page, but it just shows only the top row of the main page (instead of maybe 5 or 6 rows based on query results). And it shows the number 1 under it.
The code on the pkg_patient.php is:
<?php
$conn = new mysqli(removed);
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// check connection
if (mysqli_connect_errno()) {
exit('Connect failed: '. mysqli_connect_error());
}
// SELECT sql query
$pkgID = (int)$_GET[$id];
$sql = "SELECT pdi.*, radp.radiopharmaceutical AS radp, pkp.initials
FROM tbl_patientdoseinformation pdi
INNER JOIN tbl_isotopes i
ON
pdi.isotope = i.isoID
INNER JOIN tbl_radpharmaceuticals radp
ON pdi.isotope = radp.isotopeID
INNER JOIN tbl_packagepersonnel pkp
ON pdi.adminby = pkp.pkgpersonnelID
WHERE pdi.pkgnumberID='" . $pkgID . "'
GROUP BY pdi.patientdoseID
ORDER BY `datetimestated` DESC";
// perform the query and store the result
$result = $conn->query($sql);
// if the $result contains at least one row
if ($result->num_rows > 0) {
// output data of each row from $result
echo '<table width="1103" border="0" cellspacing="1" cellpadding="1">
<tr>
<td colspan="9"></td>
</tr>
<tr>
<td width="80" > </td>
<td width="110"> </td>
<td width="108"> </td>
<td width="161"> </td>
<td width="84"> </td>
<td width="151"> </td>
<td width="83"> </td>
</tr>
<tr>
<td> </td>
<td><strong>Date</strong></td>
<td><strong>Case No.</strong></td>
<td><strong>Radiopharmaceutical</strong></td>
<td><strong><div align="center">Dose</div></strong></td>
<td><strong><div align="right">State Date/Time</div></strong></td>
<td><strong><div align="right">Initials</div></strong></td>
</tr>
</table>
<br />';
$c = false;
while($row = $result->fetch_assoc())
{
echo '<table width="1103" border="0" cellspacing="1" cellpadding="1">
<tr style="background:',(($c=!$c)? '#eee' : '#ddd' ),'">
<td width="80">' . '<a class="gegevens" href="edit_inj_form.php?id=' .
$row['patientdoseID'] . '"> ' . "Edit". '</a>' . '</td>
<td width="117">'.date('d-M-Y', strtotime($row['datetimestated'])).'</td>
<td width="108">'.$row['patientID'].'</td>
<td width="161">'.$row['radp'].'</td>
<td width="84"><div align="right">'.$row['dose'].' mCi</div></td>
<td width="180"><div align="right">'.date('d-M-Y H:i',
strtotime($row['datetimestated'])).'</div></td>
<td width="83"><div align="right">'.$row['initials'].'</div></td>
</tr>
</table>';
}
}
else {
echo ' ';
}
$conn->close();
?>
You don't need to re-run the query in pkg_patient.php, all the variables from pkg_list_30d.php are accessible from pkg_patient.php once you've included it...
In pkg_list_30d.php on line 68 you use
echo include 'pkg_patient.php';
Instead you should echo the data you're wanting to display on pkg_patient.php and include it using include pkg_patient.php on pkg_list_30d.php

display all records of database table in div in php

here it showing all records from database but it shows all empty div first then below it shows all records .i want to show each record to be shown in single div in rows.
<div id="winnercontainer">
<h2 style="background-color:#9966FF; width:850px; height:30px; font:bold; font- size:22px; color:#FFFFFF; padding-left:20px;">Winners</h2>
<?php
include('pagination/ps_pagination.php');
$conn = mysql_connect('localhost','root','');
if(!$conn) die("Failed to connect to database!");
$status = mysql_select_db('gunjanbid', $conn);
if(!$status) die("Failed to select database!");
$sql = 'SELECT * FROM winners';
$pager = new PS_Pagination($conn, $sql,10, 10);
//The paginate() function returns a mysql result set for the current page
$rs = $pager->paginate();
?>
<table>
<?php
while($row=mysql_fetch_array($rs)){
?>
<div id="winner">
<tr>
<td ><img src="memberpic/myphoto.jpg" width="150" height="150" alt="001" /></td>
<td > </td>
<td ><table >
<tr>
<td >Auction Item : <?php echo $row['Items']; ?></td>
</tr>
<tr>
<td>Rs. <?php echo $row['Rs']; ?></td>
</tr>
<tr>
<td>Winning Bid Value : Rs. <?php echo $row['WinningBidValue']; ?></td>
</tr>
<tr>
<td>MRP : Rs. <?php echo $row['MRP']; ?></td>
</tr>
<tr>
<td>Auction closed on : <?php echo $row['enddate']; ?></td>
</tr>
<tr>
<td>Winner : <?php echo $row['Winner']; ?></td>
</tr>
<tr>
<td>City: <?php echo $row['City']; ?></td>
</tr>
<tr>
<td >Delivery Status: <?php echo $row['DeliveryStatus']; ?></td>
</tr>
</table></td>
<td > </td>
<td id=save><font color=black>SAVED:</font> Rs.<?php echo $row['MRP']- $row['WinningBidValue']; ?></td>
<td > </td>
<td ><img src=productimage/1/1.1.jpg width="200" height="193" alt="001" /></td>
</tr>
</div>
<?php
}
?>
</table>
<?php
//Display the navigation
//echo $pager->renderFullNav();
echo '<div style="text-align:center">'.$pager->renderFullNav().'</div>';
?>
</div>
abd my stylesheet s are
#winnercontainer
{
width:870px;
height:auto;
background-color:#CCCCCC;
border:solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin:10px;
position:relative;
}
#winner
{
width:840px;
height:250px;
background-color: #999999;
border: solid #9966FF;
border-radius:10px;
margin:auto;
clear:both;
margin-top: 10px;
margin-right: 10px;
margin-bottom: 10px;
margin-left: 15px;
position:relative;
}
i want all my records to be place in div means each div have one record to be display.plz help me ,i am new here.
I'll give you a start with some old code I have that reads a table from a MySQL database and prints it out on a screen. You can modify it as you see fit. It's a php file that resides on the server.
<?php
echo "<head><title>Read list</title></head>";
$host = "xxxxxxxxxxx";
$user = "xxxxxxxxxxx";
$password = "xxxxxxxxxxx";
$dbname = "xxxxxxxxxxx";
$cxn = mysqli_connect($host,$user,$password,$dbname);
if (mysqli_connect_errno()) {echo "No connection" . mysqli_connect_error();}
$query = " select * from list where uniqueid >= 0 ";
$result = mysqli_query($cxn, $query) or die ("could not query database");
echo "<table cellpadding=1px border=1>";
while ($row = mysqli_fetch_array($result))
{
$uniqueid = $row['uniqueid'];
$localid = $row['localid'];
$mdid = $row['mid'];
$type = $row['type'];
echo "<tr><td>".$uniqueid."</td><td>".$localid."</td><td>".$mdd."</td><td>".$type."</td></tr>";
}
echo "</table>";
?>

Categories