I have the following php code which queries a table of my database according to place.
The table structure is as follows :
s/n | inventory no | asset no | place
=====+==============+============+=============
1 | 125A | 5245 | London
2 | 1254B | 7545 | London
3 | 128A | 5645 | New York
4 | 254B | 1545 | Tokyo
5 | 6545 | 1456 | Tokyo
And the code:
$location=$_POST['loc'];
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
if($result==null)
{
echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
}
$row = mysql_fetch_array($result);
if($row==null)
{
echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
continue;
}
mysql_data_seek($result, 0);
?><table border='1' id="desktop" caption=<?php $location ?> >
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>
<?php
while($row = mysql_fetch_array($result)) {?>
<tr class="alt">
<?php
echo "<td>" . $row['S/N'] . "</td>";
echo "<td>" . $row['Inventory no'] . "</td>";
echo "<td>" . $row['Asset No'] . "</td>";
echo "<td>" . $row['Place'] . "</td>";
echo "</tr>";
}
echo "</table>";
}
Edit :
CSS code :
#desktop {
font-family: "Trebuchet MS", Arial, Helvetica, sans-serif;
width: 100%;
border-collapse: collapse;
}
#desktop th {
font-size: 1.1em;
text-align: left;
padding-top: 5px;
padding-bottom: 4px;
background-color: #FFCC33;
color: #ffffff;
}
#desktop td, #desktop th {
font-size: 1em;
border: 1px solid #FFCC33;
padding: 3px 7px 2px 7px;
}
#desktop tr.alt td {
color: #000000;
background-color: #FFFFCC;
}
Here, $location is storing the locations selected in the html form.
I want the returned result of the query for different places to be displayed as a single table. Currently it is displaying different place's result as different tables having their own table headers. I want the returned query to be appended into one variable.
Just place your table before the foreach loop.As it is inside the loop it repeats each time the loop continues
<table border='1' id="desktop" caption=<?php $location ?> >
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
...
...
}
}
echo "</table>";
try with this code, I have changed location array default to london,
you have changed case for field name first letter should not be capital. Hope this help :)
$location=array('London','Tokyo');
$str.='<table border="1" id="desktop">
<tr>
<th>S/N</th>
<th>Inventory No</th>
<th>Asset No</th>
<th>Place</th>
</tr>';
foreach ($location as $chk1)
{
echo "<br>";
$sql="SELECT * FROM desktop WHERE Place='$chk1';";
$result=mysql_query($sql);
if($result==null)
{
echo '<script> alert("No entry for location '.$chk1.' anditem '.'desktopreturned");</script>';
continue;
}
$row = mysql_fetch_array($result);
if($row==null)
{
echo '<script>alert("No entry for location '.$chk1.' and item '.'desktop returned");</script>';
continue;
}
mysql_data_seek($result, 0);
while($row = mysql_fetch_array($result)) {
$str.='<tr class="alt">
<td>' . $row['sr'] . '</td>
<td>' . $row['inventory_no'] . '</td>
<td>'. $row['asset_no'] . '</td>
<td>' . $row['place'] . '</td>
</tr>';
}
}
$str.='</table>';
echo $str;
?>
Related
I’m working on a php snippet and i made a table. I tried putting a td tag inside but when i do this, a lot disappears. This is a piece of my code:
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</tr>';
}
}
echo '</tbody></table>';
The code behind the // is where i tried to put the td tag but when i put it there, the images that normally appear go blank and when i inspect my code there is a lot of other code that also disappears. What am i doing wrong here?
Thank you for your help!
First I can see a problem with these lines :
echo '<table><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
echo '<table style="border-style: solid; border-width:1px;">';
Because you just close your first TABLE at the end, but not the other inside :
echo '</tbody></table>';
There are some inconsistency of your <td> to <th>. You can have a look below
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;">
<thead>
<tr>
<th>Product</th>
<th>Naam</th>
<th>Prijs</th>
<th>Qte</th>
</tr>
</thead><tbody>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo "<tr>
<td style='display: flex; border: 1px solid black;'>
<img src='$product->url' class='php_image' style='width: 15%; height: 15%;'/>
</td>
<td>Your product name</td>
<td>$product->description</td>
<td>
<p style='color: green;'>$product->price1</p>
<p style='color: red;'>$product->price</p>
</td>
<td>$product->stock</td>
</tr>";
}
}
echo '</tbody></table>';
?>
There's a lot problems with the code. From what i can see in your code, you have a table inside a tbody and at the end closed only one table.
Secondly you're also trying to put a td inside another td which is not the right thing to do. Check out mozilla developer website for more information on using HTML tables.
//Use the functions of the client, the params of the function are in
//the associative array
$params = array('customerid' => '1532');
$response = $soapclient->ca_customer_products($params);
echo '<table style="border-style: solid; border-width:1px;"><tbody><tr><th>Product</th><th>Naam</th> <th>Prijs</th><th>Qte</th></tr>';
foreach($response->list->element as $product) {
if($product->stock > 0) {
echo '<tr>';
echo '<td style="display: flex; border: 1px solid black;">';
//echo '<td>';
echo '<img src="' . $product->url . '" class="php_image" style="width: 15%; height: 15%;"/>';
//echo '<img style="width: 15%;">';
//echo '</td>';
print_r($product->description);
echo "<p style='color:green;'>".$product->price1."</p>";
echo "<p style='color:red; text-decoration: line-through'>".$product->price2."</p>";
print_r($product->price1);
print_r($product->price2);
print_r($product->stock);
echo '</td></tr>';
}
}
echo '</tbody></table>';
You are not closing your tags correctly. Also checkout the docs just as #christopher_bincom has mentioned.
I have a problem in inserting table from my database to div.
html code:
<div class="content">
<?php
$query = "SELECT name, surname FROM players";
$response = #mysqli_query($dbc, $query);
if($response) {
echo' <table align="left"
cellspacing="5" cellpadding="8" >
<tr><td align="left"><b>First Name</b></td>
<td align="left"><b>Last Name</b></td></tr>';
while($row = mysqli_fetch_array($response)) {
echo '<tr><td align="left">' .
$row['imie'] . '</td><td align="left">' .
$row['nazwisko'] . '</td><td align="left">';
echo '</tr>';
}
echo '</table>';
} else {
echo "Couldn't issue database query";
echo mysqli_error($dbc);
}
mysqli_close($dbc);
?>
</div>
css code:
.content {
width: 1000px;
margin-left: auto;
margin-right: auto;
background-color: #ffffff;
color: #000000;
border-bottom: 14px solid #333333;}
Is there a way to insert this table to this div? Because when I'm loading this, the table is under my div'content'. The table should be in blank field 'a'.
Screenshot
Correctly indenting your code would have allowed you o spot the error relatively easily - there is an open td element. Either remove the last td from the echo statement in the loop or add additional td pair to first row in table so that they balance ( unless you use colspan attributes )
<style>
.content table tr td{ align:left;padding:5px;color:black;background:white; }
</style>
<div class='content'>
<?php
$sql = 'select `name`, `surname` from `players`';
$response = mysqli_query( $dbc, $sql );
if( $response ) {
echo "
<table align='left' cellspacing='5' cellpadding='8' >
<tr>
<td><b>First Name</b></td>
<td><b>Last Name</b></td>
</tr>";
while( $row = mysqli_fetch_array( $response ) ) {
echo "
<tr>
<td>{$row['imie']}</td>
<td>{$row['nazwisko']}</td>
</tr>";
}
echo "
</table>";
} else {
printf( 'Couldn\'t issue database query: %s', mysqli_error( $dbc ) );
}
mysqli_close( $dbc );
?>
</div>
There is an open <td> tag in your code you should close it before closing the <tr>
Why am I getting those extra layers in the table output? I need to remove those extra layers from the table.What am I doing wrong? What is going on here exactly? Updated code.
<?php
$con=mysqli_connect("localhost","root", "", "cop");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query ($con, "SELECT FirstName, Practice FROM Members");
echo "
<style>th {
font: bold 11px 'Trebuchet MS', Verdana, Arial, Helvetica,
sans-serif;
color: #6D929B;
border-right: 1px solid #C1DAD7;
border-bottom: 1px solid #C1DAD7;
border-top: 1px solid #C1DAD7;
letter-spacing: 2px;
text-transform: uppercase;
text-align: left;
padding: 6px 6px 6px 12px;
background: #CAE8EA url(images/bg_header.jpg) no-repeat;
}
th.nobg {
border-top: 0;
border-left: 0;
border-right: 1px solid #C1DAD7;
background: none;
}</style>
<table border='1'>
<tr>
<th>FirstName</th>
<th>Practice</th>
</tr>";
while($row = mysqli_fetch_array($sql))
{
echo "<tr>";
echo "<td>" . $row['FirstName'] . "</td>";
echo "<td>" . $row['Practice'] . "</td>";
echo "</tr>";
}
echo "</table>";
Unusual Table output
If you were to do something like this you would be able to tell if there were empty column values in the data
while( $row = mysqli_fetch_array($sql) ){
if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
$firstname=$row['FirstName'];
$practice=$row['Practice'];
} else{
$firstname=$practice='empty';
}
echo "
<tr>
<td>{$firstname}</td>
<td>{$practice}</td>
</tr>";
}
Alternatively just output the row if it is known to contain values rather than having empty cells
while( $row = mysqli_fetch_array($sql) ){
if( !empty( $row['FirstName'] ) && !empty( $row['Practice'] ) ){
$firstname=$row['FirstName'];
$practice=$row['Practice'];
echo "
<tr>
<td>{$firstname}</td>
<td>{$practice}</td>
</tr>";
}
}
I pasted your code on my Notepad++ and in it were errors which the browser cannot forgive. In fact you can even receive a 1 month suspended sentence from your browser. Below is my version of your code. I have ignored the style. You can add style after the code is working.
<?php
$con=mysqli_connect("localhost","root", "", "cop");
if (mysqli_connect_errno()){
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$sql= mysqli_query($con, "SELECT FirstName, Practice FROM Members");
$result = '';
if(mysqli_num_rows($sql) > 0){
while($row = mysqli_fetch_array($sql)){
$FirstName = $row['FirstName'];
$Practice = $row['Practice'];
$result .= "<tr><td>" . $FirstName . "</td><td>" . $Practice . "</td></tr>";
}
}
if(isset($result)){
echo "<table border='1'>";
echo "<tr><th>FirstName</th><th>Practice</th></tr>";
echo $result;
echo "</table>";
}
?>
Following is my code to display a pdf using dompdf but the error was failed to load pdf
PDF.js v0.8.787 (build: 58407cf)
Message: InvalidPDFException
table name is acad_act and database name is pbas_db. Details of database connection is stored in db_connect.php. Any help would be appreciable
<?php
include('db_connect.php');
require 'dompdf/dompdf_config.inc.php';
$sqlinv="SELECT * FROM acad_act";
$res_inv = mysql_query($sqlinv) or die(mysql_error());
while ($sql3=mysql_fetch_array($res_inv)) {
$inv_id = $sql3['User_id'];
$date = $sql3['Year'];
$cname = $sql3['Gen_Info_AQ'];
$subtotal = $sql3['Gen_Info_NOC'];
$disc = $sql3['Gen_Info_Place'];
$subtotal2 = $sql3['Gen_Info_Duration'];
$tax = $sql3['Gen_Info_SA'];
$gtotal = $sql3['Gen_Info_Aqyear'];
}
$dompdf= new DOMPDF();
$html .="<html>
<head>
<style>
body {
font-family: arial;
font-size: 13px;
}
table {
width: 100%;
border-collapse: collapse;
}
th {
font-weight: bold;
font-size: 14px;
border: 1px solid #333333;
}
tr {
width: 100%;
}
td {
border: 1px solid #333333;
text-align: center;
padding: 10px;
font-weight: normal!important;
}
</style>
</head><body>";
$html .="
<table>
<thead>
<tr>
<th class=''>Sr.</th>
<th class=''>Particulars</th>
<th class=''>Size</th>
<th class=''>Quantity / Insertion</th>
<th class=''>Rate</th>
<th class=''>Amount</th>
<th class=''>Amount 2</th>
<th class=''>Amount 3</th>
</tr>
</thead>
<tbody>
<tr>
<td>" . $inv_id . "</td>
<td>" . $date . "</td>
<td>" . $cname . "</td>
<td>" . $subtotal . "</td>
<td>" . $disc . "</td>
<td>" . $subtotal2 . "</td>
<td>" . $tax . "</td>
<td>" . $gtotal . "</td>
</tr>
</tbody>
</table>
</body>
</html>";
$dompdf->load_html($html);
$dompdf->render();
$dompdf->stream("sample.pdf");
exit(0);
?>
The exception is due to pdf.js plugin installed in the browser. Instead of rendering the error the pdfjs shows an exception which doesn't display the error. Just remove the pdfjs plugin than you will be able to see the required error.
I am trying to create a border around the top row ONLY! Currently, a border will be displayed only around the outside of the table. But I also want a border around the first row. Can someone help me do this? I want the row with 'Team, Correct Picks, and Points' to have a border around it.
<body>
<?=$leaguename?>
<center><table cellspacing="0" style="width:400px; border:1px solid gray">
<?
echo "<tr border=\"1\"><td> Team </td><td>Correct Picks</td><td>Points</td></tr>";
while($row = mysql_fetch_array($memberslist)) {
if ($row['User_ID'] == $id) {
echo "<tr style=\"border:1px solid gray\" bgcolor=\"gray\"><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
} else {
echo "<tr><td>" . $row['User_ID'] . "</td><td><b>" . $row['Correct_Picks'] . " </b> /" . $maxcorrectpicks . "</td><td>" . $row['Points'] . "</td></tr>";
}
}
?>
</table></center>
</body>
This is presentational, so mixing it into the PHP is a bad idea. Instead, use CSS:
tr:first-child td { border-top: 1px solid black; border-bottom: 1px solid black; }
tr:first-child td:first-child { border-left:1px solid black; }
tr:first-child td:last-child { border-right:1px solid black; }
IE7+8 support for the *-child selectors can be a bit buggy. If it's not working in those browsers, consider using JavaScript polyfills.
You may also want to put the headers inside <thead> and <th> elements to make the <tr> with data the first row in a <tbody> element.
e.g.
<table>
<thead>
<tr>
<th>Team</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
</tr>
</tbody>
</table>