how to fetch data from database and display in table format? - php

I am using following code to fetch data from database . its show my output correct when i insert all columns value . for example i insert NULL values for 'Hip' so its not showing in tabular format .
Please help me for the same Thanks in advance!
$sizeArray = explode(',', $rows['sizes']);
$bustArray = explode(',', $rows['bust']);
$hipArray = explode(',', $rows['hip']);
$lengthArray = explode(',', $rows['length']);
$lengthArraybottom = explode(',', $rows['lengthbottom']);
$shoulderArray = explode(',', $rows['shoulder']);
foreach ($sizeArray as $key => $value) {
foreach ($bustArray as $bkey => $bvalue) {
foreach ($waistArray as $Wkey => $Wvalue) {
foreach ($hipArray as $hkey => $hvalue) {
foreach ($lengthArray as $lkey => $lvalue) {
foreach ($lengthArraybottom as $lkeybottom => $lbottomvalue) {
foreach ($shoulderArray as $skey => $svalue) {
if ($key == $bkey && $bkey == $Wkey && $Wkey == $hkey && $hkey == $lkey && $lkey == $lkeybottom && $lkeybottom == $skey) {
echo '<tr><td>' . $value . '</td>';
echo '<td>' . $bvalue . '</td>';
echo '<td>' . Wvalue . '</td>';
echo '<td>' . $hvalue . '</td>';
echo '<td>' . $lvalue . '</td>';
echo '<td>' . $lbottomvalue . '</td>';
echo '<td>' . $svalue . '</td></tr>';
}
}
}
}
}
}
}
}

<table class="data-table">
<caption class="title">Sales Data of Electronic Division</caption>
<thead>
<tr>
<th>NO</th>
<th>CUSTOMER</th>
<th>ITEM</th>
<th>DATE</th>
<th>AMOUNT</th>
</tr>
</thead>
<tbody>
<?php
$no = 1;
$total = 0;
while ($row = mysqli_fetch_array($query))
{
$amount = $row['amount'] == 0 ? '' : number_format($row['amount']);
echo '<tr>
<td>'.$no.'</td>
<td>'.$row['name'].'</td>
<td>'.$row['item'].'</td>
<td>'. date('F d, Y', strtotime($row['date'])) . '</td>
<td>'.$amount.'</td>
</tr>';
$total += $row['amount'];
$no++;
}?>
</tbody>
<tfoot>
<tr>
<th colspan="4">TOTAL</th>
<th><?=number_format($total)?></th>
</tr>
</tfoot>
</table>
Refrence : http://webdevzoom.com/display-mysql-data-html-5-table-using-php/

Related

date range wise fetch data

I have two date filters (from and to) which will represent days in the current month and I declare them as two variables.
How do I run a for loop in each field from start_date to end date?
I want to fetch the data in the selected date range. The table header is done, it show current selected date.
Here is my code:
$start_date = date('d', strtotime($from));
$end_date = date('d', strtotime($to));
?>
<div>
<div>
<table>
<thead>
<tr>
<th>Type</th>
<?php
$dates = range($end_date, $start_date);
foreach ($dates as $fpi) {
echo '<th>' . $fpi . '</th>';
}
?>
</tr>
</thead>
<tbody class="text-center">
<tr>
<td class="font-weight-bold text-no-wrapping">man</td>
<?php
foreach ($data['man'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">women</td>
<?php
foreach ($data['women'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">children</td>
<?php
foreach ($data['children'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">as</td>
<?php
foreach ($data['as'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">ot define</td>
<?php
foreach ($data['not_define'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
<tr>
<td class="font-weight-bold text-no-wrapping">old</td>
<?php
foreach ($data['old'] as $value) {
echo '<td>' . number_format($value) . '</td>';
}
?>
</tr>
</tbody>
</table>
</div>
</div>
this will loop through the dates range, feel free to change $date->format('Y-m-d) to what you need
$date = $start_date
while($date <= $end_date){
echo '<th>' . $date->format("Y-m-d"). '</th>';
$date->modify('+1 day');
}

Show table with results grouped 3 by 3

I am trying to show results that I get from a SQL table, it is this:
what I want to do is show results 3 by 3, like this:
I mean a table for every 3 results that the "assigned_bank" field matches, and if there are 4 results with the same number in "assigned_bank", I also show it in that same table, that is; one table for each different "assigned_bank" id.
I've been trying most of the day and the closest thing I've come to is this:
This is my last code:
<?php
$tables = sizeof($search) / 3;
for ($i = 0; $i < $tables; $i++) {
?>
<table class="table customers">
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>
<?php
foreach ($search as $item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
?>
</tbody>
</table>
<?php
echo "\r\n";
}
?>
Thank you very much for any possible help or comments and thank you for taking the time to respond.
<?php
$result = array();
foreach ($search as $key => $item) {
$result[$item['assigned_bank']][$key] = $item;
}
foreach($result as $key=>$search_items){
echo '<table class="table customers" border="2" >
<thead class="thead-blue">
<tr>
<th scope="col-xs-2">Name</th>
<th scope="col-xs-2">Lastname</th>
<th scope="col-xs-2">Bank ID</th>
</tr>
</thead>
<tbody>';
foreach($search_items as $skey=>$item){
echo '<tr align="left">';
echo '<td class="col-xs-2">' . $item["p_name"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["p_lastname"] . '</td>' . "\r\n";
echo '<td class="col-xs-2">' . $item["assigned_bank"] . '</td>' . "\r\n";
echo '</tr>';
}
echo '</tbody>
</table>';
}
<?>
You can use order by on assigned_bank column with ascending order:
SELECT p_name, p_lastname, assigned_bank FROM your_table order by
assigned_bank asc

Table with dynamic looping

I have created table with dynamic looping (if I have 100 of records, it should show only 15 record in one table)
With this I have 20 records creating two tables is done but the problem is in column dynamic is not getting and same records are displaying, for displaying in one table 15 records and 5 records in other table:
Here is my code:
<?php
$total = $totalQues / 15;
for ($row = 0; $row < $total; $row++)
{
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$j = 0;
$k = 0;
$l = 0;
for ($i = 0; $i < 15; $i++)
{
$j = $i + $totalQues;
$k = $i + 30;
$l = $i + 45;
$m = $i + 1;
$n = $k + 1;
$o = $l + 1;
if (empty($r[1][$i]->corr_ans))
{
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td></td>
<td></td>
</tr>';
}
else
{
$uans = strtolower($r[1][$i]->corr_ans);
$cans = strtolower($r[1][$i]->user_ans);
$u = ord($uans);
$cr = ord($cans);
// echo "u=".$u."cr=".$cr;
if ($u == $cr):
echo '<tr style = "color:green;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
else:
echo '<tr style = "color:red;">
<td>' . $m . '</td>
<td>' . strtoupper($r[1][$i]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$i]->user_ans) . '</td>
</tr>';
endif;
}
}
echo '</tbody>';
echo '</table>';
}
?>
I believe the following code should do the job for you:
$numberOfTables = $totalQues/15;
$maxRows = 15
for($table = 0; $table < $numberOfTables; $table ++){
echo '<table aria-describedby="example2_info" id="example2" class="table table-bordered table-hover dataTable custom-table pull-left col-xs-offset-1 col-lg-offset-2 col-sm-offset-2 col-md-offset-2">';
echo '<tbody aria-relevant="all" aria-live="polite" role="alert">';
echo '<tr class="odd black">';
echo '<td class=" text-center">Q</td>';
echo '<td class="text-center">C</td>';
echo '<td class="text-center">U</td>';
echo '</tr>';
$tableMax = ($table < $numberOfTables-1 ? $maxRows*($table+1) : $totalQues)
for($row = $table*$maxRows; $row < $tableMax; $row++){
if (!empty($r[1][$row]->corr_ans)) {
$uans = strtolower($r[1][$row]->corr_ans);
$cans = strtolower($r[1][$row]->user_ans);
$u = ord($uans);
$cr = ord($cans);
//echo "u=".$u."cr=".$cr;
$color = ($u == $cr ? 'green' : 'red');
echo '<tr style = "color:'.$color.'">
<td>' . $row . '</td>
<td>' . strtoupper($r[1][$row]->corr_ans) . '</td>
<td>' . strtoupper($r[1][$row]->user_ans) . '</td>
</tr>';
}
}
echo '</tbody>';
echo'</table>';
}
?>

display 5 arrays values per row

I am trying to make a calendar:
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
foreach($keys as $row => $value) {
echo "<td>" . $value . "</td>";
}
?>
</tr>
</table>
How can I do to echo 7 values per row? AS you can see in the image, it displays all days in the same row. (of course, because I have put it in the same , but, how can I make that starts a new every 7 echo values?
Thank you!!!
You can use modulus to check if it has reached the seventh element and end the row.
<?php
foreach($keys as $row => $value) {
if ($value % 7 == 0) {
echo "<td>" . $value . "</td></tr><tr>";
} else {
echo "<td>" . $value . "</td>";
}
}
?>
An iteration counter is a pretty simple method.
<table border="1">
<tr><th colspan="7"><?php echo $current_month ?></th></tr>
<tr>
<?php foreach ($days as $day) {
echo "<th>" . $day . "</th>";
} ?>
</tr>
<tr>
<?php
$i = 0; // Begin at 0 days written
foreach($keys as $row => $value) {
if ($i == 7){
// Create new table row after every 7th iteration
print "</tr><tr>"; // Add \n or \t for output formatting
$i = 0;
}
echo "<td>" . $value . "</td>";
$i++; // Increment $i each iteration
}
?>
</tr>
</table>
Try the below code,
You can split the array into chunks of particular sizes, See more information about this from here array chunk
<?php
$current_month = 'Oct';
$days = array('1',2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20,21,22,23,24,25,26,27,28,29,30,31);
$chunk = array_chunk($days,7);
?>
<table border="1">
<tr>
<th colspan="7"> <?php
echo $current_month; ?>
</th>
</tr>
<?php
foreach($chunk as $day) {
echo '<tr>';
foreach($day as $key) {
echo "<th>" . $key . "</th>";
}
echo '</tr>';
} ?>
</table>

Syntax error with Escaping html in php code

I am having syntax error with my following code
<?php
If (!empty($_SESSION['LogedinStudentId'])) {
echo '<h3>Your Scholarship Applications:</h3>
<table width="100%" class="table table-bordered">
<tr>
<th scope="col">Sr.No.</th>
<th scope="col">Date of Application</th>
<th scope="col">Course Type</th>
<th scope="col">Course Description</th>
<th scope="col">Subject</th>
<th scope="col">Applied for Semester No.</th>
<th scope="col">Scholarship Status</th>
<th scope="col">View / Print</th>
</tr>
<tr>
<td>' . ++$serialno . '</td>
<td>' . if(empty($row_studentdashboard['DateofApplication'])) {
echo ' ';
} else {
echo date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
. '</td>
<td>' . $row_studentdashboard['CourseType'] .'</td>
<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>
<td>' . $row_studentdashboard['Subject'] .'</td>
<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>
<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>
<td>View / Print</td>
</tr>
</table>';
} else {
echo '<h3>You do not have any application pending</h4>';
}
?>
I am getting syntax error on line no. 17 and 22. The second (nested) if statement is throwing syntax error. I can not judge what is wrong. If I run this second if statement outside of the html it is working fine.
Can anyone point out what's wrong?
What you are doing is an if-statement inside of echo-statement. It is wrong.
Run second if-statement outside of html and create a variable that you later print in your html.
A kind of this:
if(empty($row_studentdashboard['DateofApplication'])) {
$text = ' ';
} else {
$text = date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
}
.....
<td>' . ++$serialno . '</td>
<td>' . $text . '</td>
You 're not supposed to concatenate an if statement to a string. That is what you did on line 17/18
Try :
<?php
if (isset($_SESSION['LogedinStudentId']) && !empty($_SESSION['LogedinStudentId'])) {
$out = '<h3>Your Scholarship Applications:</h3>';
$out .= '<table width="100%" class="table table-bordered">';
$out .= '<tr>';
$out .= '<th scope="col">Sr.No.</th>';
$out .= '<th scope="col">Date of Application</th>';
$out .= '<th scope="col">Course Type</th>';
$out .= '<th scope="col">Course Description</th>';
$out .= '<th scope="col">Subject</th>';
$out .= '<th scope="col">Applied for Semester No.</th>';
$out .= '<th scope="col">Scholarship Status</th>';
$out .= '<th scope="col">View / Print</th>';
$out .= '</tr>';
$out .= '<tr>';
$out .= '<td>' . ++$serialno . '</td>';
$out .= '<td>';
if(!isset($row_studentdashboard['DateofApplication']) || empty($row_studentdashboard['DateofApplication'])) {
$out .= ' ';
} else {
$out .= date("d-m-Y", strtotime($row_studentdashboard['DateofApplication']));
};
$out .= '</td>';
$out .= '<td>' . $row_studentdashboard['CourseType'] .'</td>';
$out .= '<td>' . $row_studentdashboard['CourseDescriptionLong'] .'</td>';
$out .= '<td>' . $row_studentdashboard['Subject'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ApplyForSemYear'] .'</td>';
$out .= '<td>' . $row_studentdashboard['ScholarshipStatus'] .'</td>';
$out .= '<td>View / Print</td>';
$out .= '</tr>';
$out .= '</table>';
} else {
$out = '<h3>You do not have any application pending</h4>';
}
echo $out;

Categories