use div inside give table - php

Can I use a div inside the table given here. I can't use the div tag inside foreach.
here is my code
<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td></td>
<?php foreach($tests as $test){?>
<td><?php echo $test->name;?></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<?php echo $test![enter image description here][1]_attribute->attribute_name;?>
</td>
</tr>
<?php } ?>
</tbody>
</table>

<table border="1" class="table table-stripped table-bordered">
<thead>
<tr>
<td> </td>
<?php foreach($tests as $test){?>
<td><div><?php echo $test->name;?></div></td>
<?php } ?>
</tr>
</thead>
<tbody>
<?php foreach($tests[0]->test_attributes as $test_attribute){?>
<tr>
<td>
<div><?php echo test_attribute->attribute_name;?></div>
</td>
</tr>
<?php } ?>
</tbody>
</table>

Related

how to display comma separated values from multiple columns in php inside html table

i have an sql table with three columns which have comma separated values, i am trying to print it inside an html table, my code looks like below:
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
foreach($one as $ones) {
?>
<tr>
<td>
<?php echo $ones?>
</td>
<td></td>
<td></td>
</tr>
<?php }?>
</tbody>
</table>
here am only able to get the values of first column, can anyone please tell me how to get values from all the three columns, thanks in advance
Use a counter - assuming exact same number of entries per row
http://sandbox.onlinephpfunctions.com/code/555be47daf3bc3e99d496585f702bfc9dfae4e4e
<?
$one=explode(',', $row['description']);
$two=explode(',', $row['make']);
$three=explode(',', $row['uom']);
$i=0;
?>
<table class="table custom-table m-0">
<thead>
<tr>
<th>Description</th>
<th>Make</th>
<th>UOM</th>
</tr>
</thead>
<tbody>
<?php
foreach($one as $ones) {
?>
<tr>
<td><?php echo $ones; ?></td>
<td><?php echo $two[$i]?></td>
<td><?php echo $three[$i]?></td>
</tr>
<?php $i++;}?>
</tbody>
</table>

Table inside nested Table

I am having a nested table with a while loop, I want to add one more nested table in the same row:
Now I want to add one more nested table as each cd contains more than one data like below:
My code is as follows
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td><?php echo $rowcd['well_no'] ?></td>
<td><?php echo $rowcd['well_name'] ?></td>
<td>
<table border="1" width="100%">
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
/* I want to add one more nested table here*/
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
I tried some thing like this,after my second while loop
while($rowcd = $resultcd->fetch_assoc()){
?>
<tr>
<td ><?php echo $rowcd['cd_no'] ?></td>
<td>
<table>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<tr>
<td><?php echo $rowl['logs'] ?></td>
</tr>
<?php
}
?>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
}
?>
</table>
</div>
but the result was messed up. I am confused, where I want to end my while loop, I think.
Finally i got as i wish, and i am sharing the code as below
<?php
if(isset($_POST['viewcd'])){
$queryw = "select * from lib_cd where id=".$_POST['idd'];
$resultw = $mysqli->query($queryw);
?>
<div class="container">
<table border="1" align="center" border-collapse="collapse">
<thead>
<tr >
<th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th width="100">CD No:</th>
<th width="150">Logs</th>
<th width="100">Bottom Depth</th>
<th width="100">Top Depth</th>
<th width="100">Date of Log</th>
</tr>
</thead>
<?php
while($rowcd = $resultw->fetch_assoc()){
?>
<tr>
<td><?php echo $rowcd['id'] ?> </td>
<td align="center"><?php echo $rowcd['well_no'] ?></td>
<td align="center"><?php echo $rowcd['well_name'] ?></td>
<td colspan="5">
<table rules="all">
<tr>
<?php
$querycd = "select * from cd where pidd=".$rowcd['id'];
$resultcd = $mysqli->query($querycd);
while($rowcd = $resultcd->fetch_assoc()){
?>
<td width="100" align="center"><?php echo $rowcd['cd_no'] ?></td>
<td colspan="4">
<table rules="all">
<tr>
<?php
$queryl = "select * from lib_cd_logs where pid=".$rowcd['cd_no'];
$resultl = $mysqli->query($queryl);
while($rowl = $resultl->fetch_assoc()){
?>
<td width="155"><?php echo $rowl['logs'] ?></td>
<td width="105" align="center"><?php echo $rowl['bottom'] ?></td>
<td width="100" align="center"><?php echo $rowl['top'] ?></td>
<td width="100" align="right"><?php echo $rowl['date'] ?></td>
</tr>
<?php
}
?>
</table>
</td>
</tr>
<?php
}
?>
</table>
</td>
<?php
}
}
?>
</tr>
</table>
I hope this is what you meant as per your data table shown above
<div>
<table border="1">
<thead>
<tr ><th >Select</th>
<th>Well_Number</th>
<th>Well_Name</th>
<th>CD No:</th>
<th >Logs</th>
</tr>
</thead>
<tr>
<td>id</td>
<td>well</td>
<td>name</td>
<td>
<table border="1" width="100%">
<tr>
<td>1</td>
</tr>
<tr>
<td>2</td>
</tr>
</table>
</td>
<td>
<table border="1" width="100%">
<tr>
<td>Log1</td>
</tr>
<tr>
<td>Log2</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

php html table remove one data

i have problem with my table inside my table. it removes the last data in the second table.
the first table gives the correct output but the second table gives the wrong output. the second table can be shown by clicking the table row. the table row is expandable.
this is my code
<div class="form-group">
<?php
require_once 'dbconfig.php';
try {
$conn = new PDO("mysql:host=$host;dbname=$dbname",
$username, $password);
$stmt1 = $conn->prepare("select * from table where name=?");
$stmt1->bindParam(1, $_temppp1, PDO::PARAM_STR, 30);
$stmt1->execute();
} catch (PDOException $pe) {
die("Error occurred:" . $pe->getMessage());
}
?>
<table id="report" class="table table-bordered ">
<thead>
<tr>
<th>Track Number</th>
<th>Document Title</th>
<th>Document Type</th>
<th>Date Filled</th>
<th> </th>
</tr>
</thead>
<?php while ($row1 = $stmt1->fetch(PDO::FETCH_ASSOC)): ?>
<tr>
<td><?php echo $row1['tracknum']; ?></td>
<td><?php echo $row1['doctitle']; ?></td>
<td><?php echo $row1['doctype']; ?></td>
<td><?php echo $row1['datefilled']; ?></td>
<td>
<div class="arrow"><a id="menu-toggle" href="#" ><i class="glyphicon glyphicon-plus-sign"></i></a></div>
</td>
</tr>
<tr><td colspan="5">
<table class="table">
<thead>
<tr>
<th>Row</th>
<th>First Name</th>
<th>Last Name</th>
<th>Email</th>
</tr>
</thead>
<tbody>
<tr>
<td>1</td>
<td>John</td>
<td>Carter</td>
<td>johncarter#mail.com</td>
</tr>
<tr>
<td>2</td>
<td>Peter</td>
<td>Parker</td>
<td>peterparker#mail.com</td>
</tr>
<tr>
<td>3</td>
<td>John</td>
<td>Rambo</td>
<td>johnrambo#mail.com</td>
</tr>
</tbody>
</table>
</td>
</tr>
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
and this is the output
Your code is wrong i think
change this
</td>
</tr>
<?php endwhile; ?>
</table>
</div>
to that
<?php endwhile; ?>
</td>
</tr>
</table>
</div>
and try again or loock in this region..

PHP TCPDF heredoc

I got this in my heredoc in my TCPDF.
I basically want to create a dynamic pdf with the data of my database.
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname<th>
</tr>
</thead>
<tbody>
<tr>
<th></th>
<th></th>
</tr>
</tbody>
</table>
EOD;
I want to make this dynamic like this, to create the data from my database dynamically.
<?php
foreach($result_set as $result) {
?>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
<?php
}
?>
I tried this so far but I cannot find a suitable solution:
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
<tr>
<td>
<?php echo $result['firstname']; ?>
</td>
<td>
<?php echo $result['lastname']; ?>
</td>
</tr>
</tbody>
</table>
EOD;
can anyone help please.
Ok I somehow did it, but still if there is any better solution feel free to post:)
Code:
$loopHereFirstname = '';
$loopHereLastname = '';
foreach($result_set_random_01 as $result_dish_usr_01) {
$tr_start = '<tr>';
$tr_end = '</tr>';
$td_start = '<td>';
$td_end = '</td>';
$loopHereFirstname .= $result_dish_usr_01['firstname']."\n";
$loopHereLastname .= $result_dish_usr_01['lastname']."\n";
}
$html = <<<EOD
<table border="1">
<thead>
<tr>
<th>firstname</th>
<th>lastname</th>
</tr>
</thead>
<tbody>
$tr_start
$td_start
$loopHereFirstname
$td_end
$td_start
$loopHereLastname
$td_end
$tr_end
</tbody>
</table>
EOD;
You've to break your code into two part if you want to use a loop with <<
<?php
$html =<<<EOD
<table border="1">
<thead>
<tr>
<th>Vorname</th>
<th>Nachname</th>
<th>Von</th>
<th>Bis</th>
</tr>
</thead>
<tbody>
EOD;
foreach($result_set as $result) {
$html.=<<<EOD
<tr>
<td>
{$result['firstname']}
</td>
<td>
{$result['lastname']}
</td>
</tr>
EOD;
}
$html .= '</tbody></table>';

foreach loop not generating the right html table

I'm trying to get this output from returned data fetched from mysql:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
<td>LCD</td><td>Yes</td><td>No</td>
<td>Auto Pilot</td><td>No</td><td>No</td>
<td>Fast Generation</td><td>Yes</td><td>No</td>
<td>Dual Cores</td><td>No</td><td>Yes</td>
</tr>
</tbody>
</table>
But I have trouble getting the following code to achieve that output with one foreach loop. It uses in_array to check whether each value from $featured_tests exists in the returned data.
$featured_tests = array("LCD","Auto Pilot","Fast Generation","Dual Cores");
$table_head ="<table><thead><tr><th></th>";
$table_colspan1 = "</tr></thead><tbody><tr><td colspan='5'>Features</td></tr>";
$table_end ="</tr></tbody></table>";
foreach($rows as $row)
{
$special_features = explode(",",$row->special_features);
$header_image .= "<th><img src='".$row->image_url."'></th>";
foreach($featured_tests as $featured_test)
{
$featured .= "<tr><td>".$featured_test."</td>";
if(in_array($featured_test,$special_features))
{
$featured .= "<td>Yes</td>";
}
else
{
$featured .= "<td>No</td>";
}
}
}
$table_html = $table_head.$header_image.$table_colspan1.$featured.$table_end;
But the result I'm getting is a mess. Each value in $featured_tests is iterating over and over again for each product and thus results in a very long table. Can anyone help me correct my code to get the ideal output?
Here's the result:
<table>
<thead>
<tr>
<th></th><th><img src='img1.jpg'></th><th><img src='img2.jpg'></th>
</tr>
</thead>
<tbody>
<tr>
<td colspan='5'>Features</td>
</tr>
<tr>
<td>LCD</td><td>Yes</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>Yes</td>
</tr>
<tr>
<td>Dual Cores</td>No<td>
</tr>
<tr>
<td>LCD</td><td>No</td>
</tr>
<tr>
<td>Auto Pilot</td>No<td></td>
</tr>
<tr>
<td>Fast Generation</td><td>No</td>
</tr>
<tr>
<td>Dual Cores</td>Yes<td>
</tr>
</tbody>
</table>
you are creating new rows <tr> inside on most deep foreach...
take this example to make what you want:
<table>
<thead>
<th>Col 1</th><th>Col 2</th>
</thead>
<tbody>
<?php foreach($large_array as $less_array): ?>
<tr>
<?php foreach($less_array as $row): ?>
<!-- <td> contents etc</td>-->
<?php endforeach?>
</tr>
<?php endforeach;?>
</tbody>
</table>

Categories