Table splitting - php

Here is my code to display table:
<table id="ws_table" class="ws_table">
<thead>
<tr class="bg-light">
<th>WS Code</th>
<th>WS Name</th>
</tr>
</thead>
<tbody class="reportWs_table">
<?php
if (is_array($workschedules)) {
//$i=0;
foreach ($workschedules as $workschedulesingle) {
$cnt++;
?>
<tr>
<td class="">
<?php echo $workschedulesingle['workScheduleCode']; ?>
</td>
<td class="">
<?php echo $workschedulesingle['workScheduleName']; ?>
</td>
</tr>
<?php
}
} else {
}
?>
</tbody>
</table>
My problem is its a long table. So I want to split into two. How can I split the table vertically? i.e. When the entries exceeds 20 it will goes to next table. How can I create it?

• Set Index $i=0
• Loop every row
• Check if the index start on 0
• Check if Index reach 20/limit
• If reach 20/limit reset Index to 0 again.
<?php
//INIT INDEX $i
$i = 0;
if(is_array($workschedules)){
// FOR EACH ROW
foreach($workschedules as $workschedulesingle){
//EVERY TIME THE INDEX START PRINT THE TABLE
if($i==0){
?>
<table id="ws_table" class="ws_table">
<thead>
<tr class="bg-light">
<th>WS Code</th>
<th>WS Name</th>
</tr>
</thead>
<tbody class="reportWs_table">
<?php } ?>
<tr>
<td class=""><?php echo $workschedulesingle['workScheduleCode'];?></td>
<td class=""><?php echo $workschedulesingle['workScheduleName'];?></td>
</tr>
<?php
//IF THE INDEX REACH 20/LIMIT, PRINT CLOSING TABLE TAGS
if($i==20){ ?>
</tbody>
</table>
<?php
}
//IF THE INDEX EXCEED 20/LIMIT, RESET $i TO 0 AND START AGAIN
$i++;
if($i>20){
$i=0;
}
}
}else{
}
?>

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>

MySQL not fetching all results

Trying to fetch all results in PHP from MySQL database, but it is leaving the first query.
My MySQL table is in the below image: .
My Code:
<?php
$irn = "33857";
$stmt = $user_home->runQuery('SELECT * FROM invoice WHERE Inv = :inv ORDER BY Sr ASC ');
$stmt->bindParam(':inv',$irn);
$stmt->execute();
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
if($stmt->rowCount() > 0)
{
?>
<table id="chiru_inv" class="table table-striped table-hover table-bordered table-responsive">
<tr>
<td colspan="4" align="center">
<h1>Company</br><span style="font-size: 75%;">Number</span></h1>
</td>
</tr>
<tr>
<td colspan="2">
<h3><span style="float: left;"><?php echo $rowc['Customer']; ?> (<?php echo $rowc['Inv']; ?>)</span></h3>
</td>
<td colspan="2">
<h3><span style="float: right;"><?php echo $rowc['Date']; ?></span></h3>
</td>
</tr>
<tr>
<th>Sr.</th>
<th>Item</th>
<th>Qty</th>
<th>Amount</th>
</tr>
<?php
$i = 0;
while($row=$stmt->fetch(PDO::FETCH_ASSOC))
{
extract($row);
$i++;
?>
<tr>
<td><?php echo $i; ?></td>
<td><?php echo $Item; ?></td>
<td>5</td>
<td>200</td>
</tr>
<?php
}
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
</tr>
<tr>
<td colspan="2"><strong><?php echo getIndianCurrency(225); ?>Only</strong></td>
<td><strong>Total:</strong></td>
<td><strong>225</strong></td>
</tr>
</table>
<?php
}
?>
The result when I execute the above code is in the below image:
There are three queries with invoice number 33857, but only two are displayed (leaving the first one)!
I need all to be displayed as per invoice number.
Please help me from sorting out the error/code I made or I left.
The problem in your code is with the line:
$rowc = $stmt->fetch(PDO::FETCH_ASSOC);
in the beginning of your code you're fetching the first row.
You have to update your while loop, like this
<?php
$i = 0;
while($rowc)
{
extract($rowc);
$i++;
?>
<!-- Your HTML/PHP code for the table -->
<?php
$rowc=$stmt->fetch(PDO::FETCH_ASSOC);
}
?>
So, the idea is to fetch the row at the end of the while loop, in order to not lose your first row.

Im looping to display the rows in the database but other rows are not included inside the html table

if(isset($_POST["Search"]))
{
$resz=mysqli_query($link,"select*from tblschedule where Destination =
'$_POST[Destination]' and Origin = '$_POST[Origin]' and Date =
'$_POST[Date]'");
Code for search
$count=mysqli_num_rows($resz);
if($count>=1){
if there's a row to search then display
echo "</br></br>
<div class=container>
<div class=table-responsive>
<table class = table>
<tr>
<th class=col-xs-2> TripCode </th>
<th class=col-xs-2> Origin </th>
<th class=col-xs-2> Destination </th>
<th class=col-xs-2> Date </th>
<th class=col-xs-2> Time </th>
<th class=col-xs-2> Action </th>
</tr>";
while($row = mysqli_fetch_array($resz))
{?>
<tr>
<td> <?php echo $row['TripCode']; ?> </td>
<td> <?php echo $row['Origin']; ?> </td>
<td> <?php echo $row['Destination']; ?> </td>
<td> <?php echo $row['Date']; ?> </td>
<td> <?php echo $row['Time']; ?> </td>
<td> <?php echo "<a href=webbook.php?
id=".$row['TripCode'].">Book</a><br />"; ?></td>
</tr>
</table>
</div>
</div>
<?php }} else{ echo "<center>No results were found.</center>"; }}
?>
Code is successfully getting the rows I want but only one row has a table and the others don't have. I want each row to have a table of their own.
seems like your <table> and the two <div> tag is within the while loop. remove it and put outside of the loop.

Create a Counter in a ranking table

I have a table of articles that are arranged by the number of visits, where the most visited one is on top, i.e it has Rank of 1. I tried adding a Rank column in the table, but can't get it to work. The Rank column does not exist in the Database, so this should be an incremental counter that starts with 1 and ends with the number of articles listed in the table.
Here's my code, which does not work
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php foreach($popular_articles as $article) {
//create array for most popular
?>
//set counter variable
$counter = 1;
while($row = mysql_fetch_array($result))
<tr>
echo "<td style="width:10%">".$counter."</td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php if($article->art_hit_count){ echo '<b>'.$article->art_hit_count.'</b>'; }else { echo '<b>0</b>'; } ?> times.</td>
</tr>
$counter++; //increment counter by 1 on every pass
echo <?php } ?>
</table>
Increasing the counter must be done in PHP. Currently the line that does that is in the HTML:
$counter++; //increment counter by 1 on every pass
I also removed the while, since it didn't seem to do anything, as if it was accidentally pasted there.
The loop should look like this:
<?php
foreach($popular_articles as $article) {
//set counter variable
$counter = 1;
<tr>
<td style="width:10%"><?php echo $counter; ?></td>";
<td style="width:50%"><?php echo $article->art_title; ?></td>
<td style="width:20%"><?php echo $article->art_type; ?></td>
<td style="width:20%"><?php
if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
}else {
echo '<b>0</b>';
} ?> times.</td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} ?>
Alternatively, you can display the counter and update it in one go:
<td style="width:10%"><?php echo $counter++; ?></td>";
...
<?php
// $counter++; // You won't be needing this anymore.
} ?>
I think you missed to initialize your counter before the loop (in the commentaries, I can see a $counter = 1, but it's trully executed.
And at the end, you try to increment a variable but you're not in a PHP script, so it can not work. Try my corrected version to see if it work.
<table class="resultsTable" width="100%">
<tr>
<th style="width:10%">Rank</th>
<th style="width:50%">Post Title</th>
<th style="width:20%">Post Type</th>
<th style="width:20%">Hits</th>
</tr>
<?php
$counter = 1;
foreach($popular_articles as $article) {
//create array for most popular
?>
<tr>
<td style="width:10%"> <?php echo $counter; ?></td>;
<td style="width:50%"> <?php echo $article->art_title; ?></td>
<td style="width:20%"> <?php echo $article->art_type; ?></td>
<td style="width:20%">
<?php if($article->art_hit_count){
echo '<b>'.$article->art_hit_count.'</b>';
} else {
echo '<b>0</b>';
} ?> times. </td>
</tr>
<?php
$counter++; //increment counter by 1 on every pass
} //the end of FOR loop ?>
</table>
EDIT
If you want to make a link for the art_title. Just replace this line :
<td style="width:50%"> <?php echo $article->art_title; ?></td>
By this one :
<td style="width:50%"> <?php echo $article->art_title; ?></td>

Putting html and PHP code inside a variable

I have this code that produces a table exactly how I want it. I want to put this whole code assigned to a PHP variable e.g.: $table=the posted code. I tried concatenating and heredoc but couldn't get it to output my table as it is when doing and echo $table;.
Any input is appreciated
<table id=patients>
<tr>
<th>Pt. username</th>
<th>Pt. number</th>
<th>Full Name</th>
<th>Added on</th>
</tr>
<?php $x=1;
foreach ($users as $patient) {
?> <tr <?php if ($x % 2 == 0) {echo "class='alt'"; } ?>>
<td> <?php echo $patient['username'];?></td>
<td> <?php echo $patient['id'];?></td>
<td> <?php echo $patient['name'];?></td>
<td> <?php echo $patient['joined'];?></td>
</tr>
<?php
$x++;
} ?>
</table>
Just use output buffering to put the output into the internal buffer and then capture it.
<?php
ob_start();
?>
<table id=patients>
<tr>
<th>Pt. username</th>
<th>Pt. number</th>
<th>Full Name</th>
<th>Added on</th>
</tr>
<?php $x=1;
foreach ($users as $patient) {
?> <tr <?php if ($x % 2 == 0) {echo "class='alt'"; } ?>>
<td> <?php echo $patient['username'];?></td>
<td> <?php echo $patient['id'];?></td>
<td> <?php echo $patient['name'];?></td>
<td> <?php echo $patient['joined'];?></td>
</tr>
<?php
$x++;
} ?>
</table>
<?php
$table = ob_get_clean();
?>

Categories