Table with dynamic looping - php

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>';
}
?>

Related

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

Add table row with jquery after two for loops

Normally I specify the wanted element by eq() function from jQuery.
Now I need to specify a table row within two for loops but I can't access to $i and $l.
This is the code.
$content_count = count($content);
$languages_count = count($languages);
for ($i = 0; $i < $content_count; $i++) {
for ($l = 0; $l < $languages_count; $l++) {
echo '<tr>';
echo ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">' . $lang_img . TEXT_TITLE . '</td>';
echo ' <td class="dataTableConfig col-single-right">';
echo xtc_draw_input_field('content_title[' . $i . '][' . $languages[$l]['id'] . ']', ((isset($content_lang['content_title'])) ? $content_lang['content_title'] : ''), 'size="60"');
echo ' </td>';
echo '</tr>';
}
}
I tried it whith this code
echo '<tr>';
echo ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">' . $lang_img . TEXT_MASK_ALIAS . '</td>';
echo ' <td class="dataTableConfig col-single-right">';
echo xtc_draw_input_field('mask_alias[' . $i . ']['. $languages[$l]['id'] . ']', ((isset($content_lang['mask_alias'])) ? $content_lang['mask_alias'] : ''), 'size="60"');
echo ' </td>';
echo '</tr>';
?>
<script>
$(document).ready(function() {
var content = <?php echo $content_count; ?>;
var languages = <?php echo count($languages); ?>;
for (var i = 0; i < content.length; i++) {
for (var l = 0; l < languages.length; l++) {
$("[name^='mask_alias']").closest("tr").detach().insertAfter($("[name=^'content_title']").closest("tr"));
}
}
});
</script>
But it's not at the right place and the counts of $i and $l are wrong.
I found a solution with this code
<script>
$(document).ready(function() {
var content = <?php echo $content_count; ?>;
var languages = <?php echo json_encode($languages); ?>;
var row = '';
for (var i = 0; i < content; i++) {
$.each(languages, function(key, value) {
row = '<tr>';
row += ' <td class="dataTableConfig col-left" style="border-right:1px solid #a3a3a3;">';
row += ' <div style="float:left;margin-right:5px;">';
row += ' <img src="/lang/' + value['directory'] + '/admin/images/' + value['image'] + '" alt="' + value['name'] + '" title="' + value['name'] + '" style="border:0;">';
row += ' </div>';
row += ' <?php echo TEXT_MASK_ALIAS; ?>';
row += ' </td>';
row += ' <td class="dataTableConfig col-single-right">';
row += ' <input type="text" name="mask_alias[' + i + '][' + value['id'] + ']" value="<?php echo (isset($content_lang['mask_alias']) ? $content_lang['mask_alias'] : ''); ?>" size="60">';
row += ' </td>';
row += '</tr>';
$('[name="content_title[' + i + '][' + value['id'] + ']').closest('tr').after(row);
});
}
});
</script>

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

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/

Looking to to determine Elapsed Time in PHP

I am working on a project where I am determining the time between two times. The times are in the format HH:MM AM/PM. How would you convert this to an elapsed time in hours and minutes?
Here is the code below.
<?php
$flightcount = 0;
$pdo = Database::connect();
$pdo1 = Database::connect();
$sql = "SELECT * FROM FlightBasic ORDER BY STR_TO_DATE(DepartTime, '%l:%i %p')";
$sql2 = "SELECT * FROM Connections";
$Org = $_GET["origin"];
$Dest = $_GET["destination"];
$DDate = $_GET["ddate"];
$ADate = $_GET["adate"];
$sql2 = "SELECT * FROM connections";
foreach ($pdo->query($sql) as $row) {
if($row["Date"] == $DDate){
if($row["Date"] == $DDate){
foreach ($pdo1->query($sql2) as $row2) {
$conn = $row2["Connection"];
if($row2["Origin"]==$Org && $row2["Destination"]==$Dest){
if($row["Origin"]==$Org && $row["Destination"]==$conn){
$fldpt = $row["DepartTime"];
$flarr = $row["ArriveTime"];
$flnum = $row["FlightNumber"];
$sql3 = "";
echo
$fltr = $row2["TravelTime"];
foreach ($pdo->query($sql) as $row) {
if($row["Origin"]==$conn && $row["Destination"]==$Dest){
echo '<tr style="color:black; height:20px; background-color:#95AFB8">';
echo '<td align="center">' . $fldpt . '</td>';
echo '<td align="center" >' . $flarr . '</td>';
echo '<td align="center" >' . $flnum . '</td>';
echo '<td align="center" valign="middle" rowspan="2" width="108">1 Stop: <br> Stops In ' . $row2['Connection'] . '</td>';
echo '<td align="center" valign="center" rowspan="2">' . $fltr . '</td>';
echo '</tr>';
echo '<tr style="color:black; height:20px; background-color:#DFEBF0">';
echo '<td align="center" width="82">' . $row['DepartTime'] . '</td>';
echo '<td align="center" width="84">' . $row['ArriveTime'] . '</td>';
echo '<td align="center" width="94">' . $row['FlightNumber'] . '</td>';
echo '</tr>';
}
}
}
}
}
}
if($row["Origin"]==$Org && $row["Destination"]==$Dest){
echo '<tr style="color:black; height:40px; background-color:#95AFB8">';
echo '<td align="center">' . $row['DepartTime'] . '</td>';
echo '<td align="center">' . $row['ArriveTime'] . '</td>';
echo '<td align="center">' . $row['FlightNumber'] . '</td>';
echo '<td align="center">Nonstop</td>';
echo '<td align="center">' . $row['TravelTime'] . '</td>';
echo '</tr>';
}
}
}
Database::disconnect();
?>
Thanks for the help!
Here is a simple code calculating difference between times. Your code has nothing clear on what you really want, therefor, transform this according to your needs.
<?php
$first = new DateTime( Your time );
$second = new DateTime( Another time );
$diff = $first->diff( $second );
This prints something like: Monday 8th of August 2005 03:12:46 PM
echo date('l jS \of F Y h:i:s A');
echo $diff->format( '%H:%I:%S' ); // -> Difference
?>
The A (or a) shows the AM / PM format. More details in PHP documentation http://php.net/manual/en/function.date.php

Mixed Bag of HTML and PHP

In my PHP page, I am extracting a bunch of variables from the URL, and formatting their output into a nice HTML table. One section in the table needs to be dynamically created, dependent upon what was ordered on the previous webpage. Finally, I'm using the $mail function to send the HTML table with all the info to an email recipient.
The table works great, EXCEPT for the dynamic section with the while loop. The compiler is getting confused because my syntax is wrong. I suspect this is because my code is inside the $message'...' variable. Any advice?
<?php
// Extracting the variables from URL
$params = $_SERVER['QUERY_STRING'];
// Placing the variables into $array
$array=array();
parse_str($params,$array);
// Identifying the length of the main array and creating an array of KEYS
$keys = array_keys($array);
$keysCount = count($keys);
// And creating an array of corresponding values
$values = array_values($array);
$to = "steve#dutchlandfrozenfoods.com";
$subject = "NEW EUROCLASSIC ORDER";
$message = '
<HTML>
<HEAD>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>EuroClassic Fine Family of Pastries - Ordering</title>
</HEAD>
<BODY bgcolor="#F0EFEE">
<table id="hor-minimalist-b" summary="Customer Information">
<thead>
<tr>
<th scope="col">Customer Contact:</th>
<th scope="col">Shipping To:</th>
</tr>
</thead>
<tbody>
<tr>
<td>' . $values[0] . '</td>
<td>' . $values[1] . '</td>
</tr>
<tr>
<td>' . $values[2] . '</td>
<td>' . $values[3] . '</td>
</tr>
<tr>
<td>' . $values[4] . '</td>
<td>' . $values[5] . '</td>
</tr>
<tr>
<td></td>
<td>' . $values[6] . '</td>
</tr>
</tbody>
</table>
<table id="hor-minimalist-b" summary="Order Details">
<thead>
<tr>
<th scope="col">Product:</th>
<th scope="col">Item Code:</th>
<th scope="col">Quantity:</th>
<th scope="col">Ext Price:</th>
</tr>
</thead>
<tbody>
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
<tr>
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
$i = $i+1;
<td>' . $values[$i] . '</td>
</tr>
}
</tbody>
</table>
</BODY>
</HTML>
';
$headers = "MIME-Version: 1.0\r\n";
$headers .= "Content-type: text/html; charset=iso-8859-1\r\n";
$headers .= "To:Steve <steve#dutchlandfrozenfoods.com>\r\n";
mail($to, $subject, $message, $headers);
?>
Prepare the tbody in a variable before.
$tbody = '';
while ($i = 13; $i < $keysCount-3; $i = $i+2;) {
$tbody .= '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td>';
$i = $i+1;
$tbody .= '<td>' . $values[$i] . '</td></tr>';
}
And then:
$message = "...<tbody>" . $tbody . "</tbody> ....";
But I must say, the logic here isn't understandable, and if something changes, for example, count of columns, code could become quite hard to maintain.
Briedis point is important and necessary. but more simply - that is not how a while loop works
while(exp)
{
//body runs while exp remains true
}
You don't put three expressions up there like you do in a for loop. If you want to keep it as a while loop, do
$i = 13
while($i < $keysCount-3)
{
//body, with necessary increments
}
And if you decide to do a for loop instead, there is no semi-colon after the third (generally the incrementing) expression
Whichever method of loop you use, you need to take it out of the string as Briedis indicated. either make a temp variable such as $tbody or repeatedly concatenate to $message in the loop body.
As far as I know, you cannot call a PHP function and set it equal to a variable at the same time. you would need to do something similar to the following:
'... <tbody>';
while ($i = 13; $i < $keysCount-3; $i = $i+2;)
{
$message = '<tr><td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td>';
$i = $i+1;
$message = '<td>' . $values[$i] . '</td></tr>';
}
$message = '</tbody> ...'

Categories