I am trying to automatically page break if the data is more than 10 Im using mpdf + laravel + vue
This is my css code
<style>
div.breakNow { page-break-inside:avoid; page-break-after:always; }
</style>
This is my sale_pdf.blade.php
$i=0;
#foreach ($details as $detail)
$i++;
if( $i % 10 == 0 ){
<div class="breakNow">
<div id="details_inv">
<table class="table-sm">
<thead>
<tr>
<th>PRODUCT</th>
<th>UNIT PRICE</th>
<th>QUANTITY</th>
<th>DISCOUNT</th>
<th>TAX</th>
<th>TOTAL</th>
</tr>
</thead>
<tbody>
<tr>
<td>{{$detail['code']}} ({{$detail['name']}})</td>
<td>{{$detail['price']}} </td>
<td>{{$detail['quantity']}}/{{$detail['unitSale']}}</td>
<td>{{$detail['DiscountNet']}} </td>
<td>{{$detail['taxe']}} </td>
<td>{{$detail['total']}} </td>
</tr>
} #endforeach
</tbody>
</table>
</div>
</div>
This is the current output.
Did I missed something? Thank you in advance
There is two way you can resolve this.
Option 1
<!DOCTYPE html>
<html>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8"/>
<style>
.page-break {
page-break-after: always;
}
</style>
<body>
<h1>Page 1</h1>
<div class="page-break"></div>
<h1>Page 2</h1>
</body>
</html>
Option 2
Adding class between table row
$i=0;
#foreach ($details as $detail)
$i++;
<div class="{{ ( $i % 10 == 0 ) ? 'breakNow':''}}"></div>
#endforeach
Related
Could anybody tell me what I am going wrong?
I am trying to change CSS style asking with PHP.
<?php
include 'connect.php';
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="refresh" content="10">
<link href="https://cdn.jsdelivr.net/npm/bootstrap#5.2.2/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-Zenh87qX5JnK2Jl0vWa8Ck2rdkQ2Bzep5IDxbcnCeuOxjzrPF/et3URy9Bv1WTRi" crossorigin="anonymous">
<title>Lista de mensajes</title>
</head>
<body>
<div class="container-fluid mb-10">
<table class="table table-striped fs-2">
<thead class="text-uppercase">
<tr>
<th scope="col" class="col-sm-2">fecha</th>
<th scope="col">mensaje</th>
<th scope="col" class="col-sm-1">prioridad</th>
</tr>
</thead>
<tbody>
<?php
$sql="SELECT * FROM mensajes WHERE 1 ORDER BY prioridad DESC,fecha DESC";
$resultado=mysqli_query($con,$sql);
while($row=mysqli_fetch_assoc($resultado)){
$id=$row['id'];
$hotel=$row['hotel'];
$mensaje=$row['mensaje'];
$fecha=new datetime($row['fecha']);
$fecha=$fecha->format('d-m-y H:i:s');
$prioridad=$row['prioridad'];
if($prioridad==true){
echo '<tr class="text-danger">';
}else{
echo '<tr>';
}
echo '<td scope="row">'.$fecha.'</td>
<td>'.$hotel." - ".$mensaje.'</td>
<td>'.$prioridad.'</td>
</tr>';
}
?>
</tbody>
</table>
<?php
if(!mysqli_close($con)){
die(mysqli_error());
}
?>
</div>
</body>
</html>
HTML output seems to be correct but I can only see the last entry in red
<table class="table table-striped fs-2">
<thead class="text-uppercase">
<tr>
<th scope="col" class="col-sm-2">fecha</th>
<th scope="col">mensaje</th>
<th scope="col" class="col-sm-1">prioridad</th>
</tr>
</thead>
<tbody>
<tr class="text-danger"><td scope="row">23-10-22 10:58:54</td>
<td>PLB - rojo OOOO?</td>
<td>1</td>
</tr><tr class="text-danger"><td scope="row">23-10-22 08:58:02</td>
<td>BPC - BPC y prioridad</td>
<td>1</td>
</tr><tr><td scope="row">24-10-22 08:01:54</td>
<td>BPK - TEST</td>
<td>0</td>
</tr><tr><td scope="row">23-10-22 10:26:29</td>
<td>BGA - Prueba BGA con prioridad</td>
<td>0</td>
</tr>
</tbody>
</table>
connect.php just make a connection to a mysql database with a single table.
I would like to show the text in red when the priority is true or 1
Thanks in advance
Firstly, don’t use while for database fetch. Use instead if
Secondly, something like $avar=areturn() will always be true, so you should define $row separately before the loop.
How would I be able to make a table from php using html with a foreach loop?
My goal is to have a table with href tags for each of the sites. It's just that I'm a newbie to php
<?php
$baseConf = ['site' => [abc.com],[abc.org]];
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<table>
<!--<?php //$site; ?>-->
<?php
foreach($site as $value ) {
printf $value;
}
?>
</table>
</body>
</html>
You can use a simple foreach loop to put <a> tag with href in table.
<table>
<?php
$sites=$baseConf['site'];
foreach($sites as $item) {
?>
<tr>
<td>
<a href="<?= $item ?>">
<?=$item?>
</a>
</td>
</tr>
<?php
}
?>
</table>
It is very simple for loop and you can grab it from SO. Still I am solving it for you.
I assume you have a multidimensional array. Let's simplify it.
$baseConf = ['site' => [example.com],[example.org]];
$sites = $baseConf['site']; // we will loop sites now
Now Use the html like this
<table class="table">
<thead>
<tr>
<th scope="col">#</th>
<th scope="col">Links</th>
</tr>
</thead>
<tbody>
<?php for ($i = 0; $i < count($site); $i++){?>
<tr>
<th scope="row"><?php echo $i?></th>
<td><?php echo $site[$i]?></td>
</tr>
<?php}?>
</tbody>
</table>
I have fixed my code a bit and this is what worked out:
$baseConf = array(
'site' => array("abc.com","abc.org"),
);
?>
<!DOCTYPE html>
<html>
<head>
<title>Default Page</title>
</head>
<body>
<!--<?php //$site; ?>-->
<?php
foreach ($baseConf as $value) {
//echo "$value\n";
print_r($value);
}
?>
</body>
</html>
Background
I am trying to show a list of scheduled events for a user, to do this I am showing a bootstrap table with two columns, one is a generated list of 15m time intervals and the other is the event itself.
Scheduled events have a green background.
Problem
Due to the way I have setup my table and foreach loop I can't add a class to the <td> tag to change the background color depending on if there is an event or not.
What I have tried so far
Here is my view to display the data, I have added a div with a class 'event' which has a green background to denote that the event is scheduled.
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th>Time</th>
<th>Event</th>
<th></th>
</tr>
</thead>
<tbody>
<!-- shows times as 1472792400 => 6:00, 6:15, 6:30 -->
<?php foreach ($intervals as $seconds => $interval) : ?>
<tr>
<td class="col-md-1">
<div class="text-center">
<i class="fa fa-clock-o"></i>
<?php echo $interval; ?>
</div>
</td>
<td class="col-md-10">
<?php foreach ($events as $key => $event) : ?>
<?php if ($event->seconds_start == $seconds) : ?>
<div class="event-header">
<span class="label label-default"><?php echo $event->event; ?></span>
</div>
<?php elseif ($event->seconds_start < $seconds && $event->seconds_end >= $seconds + 900) : ?>
<div class="event"> </div>
<?php else : ?>
<!-- nothing -->
<?php endif; ?>
<?php endforeach; ?>
</td>
<td class="col-md-1"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Here is my events table
and my css file
.event-header {
color:#fff;
font-weight:600;
background: #75a575;
border-bottom:1px solid #75a575 !important;
margin: -8px -10px -10px;
}
.event {
background: #abdbab;
margin: -8px -10px -10px;
}
here is how the events page looks so far
question
So basically if the event time equals the displayed time then I want to change the entire background color of the <td> to show that there is an event scheduled... but I can't add a class to the <td> because my logic block is within the <td></td> already. How do I do this?
UPDATE 1
Adding the <td> tags within the if statement (the ideal solution) like this...
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th class="text-center">Time</th>
<th>Event</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($intervals as $seconds => $interval) : ?>
<tr>
<td class="col-md-1">
<div class="text-center">
<i class="fa fa-clock-o"></i>
<?php echo $interval; ?>
</div>
</td>
<?php foreach ($events as $key => $event) : ?>
<?php if ($event->seconds_start == $seconds) : ?>
<td class="col-md-10 event-header">
<span class="label label-default"><?php echo $event->event; ?></span>
</td>
<?php elseif ($event->seconds_start < $seconds && $event->seconds_end >= $seconds + 900) : ?>
<td class="col-md-10 event"><td>
<?php else : ?>
<td class="col-md-10"><td>
<?php endif; ?>
<?php endforeach; ?>
<td class="col-md-1"></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
Produces the following view:
UPDATE 2
Getting closer, by showing nothing in the else statement, however the layout is still very broken and I am having a hard time processing where I am going wrong...
<table class="table table-striped table-hover table-bordered">
<thead>
<tr>
<th class="text-center">Time</th>
<th>Event</th>
<th></th>
</tr>
</thead>
<tbody>
<?php foreach ($intervals as $seconds => $interval) : ?>
<tr>
<td class="col-md-1">
<div class="text-center">
<i class="fa fa-clock-o"></i>
<?php echo $interval; ?>
</div>
</td>
<?php foreach ($events as $key => $event) : ?>
<?php if ($event->seconds_start == $seconds) : ?>
<td class="col-md-10 event-header">
<span class="label label-default"><?php echo $event->event; ?></span>
</td>
<?php elseif ($event->seconds_start < $seconds && $event->seconds_end >= $seconds + 900) : ?>
<td class="col-md-10 event">hello<td>
<?php else : ?>
<!-- nothing -->
<?php endif; ?>
<?php endforeach; ?>
</tr>
<?php endforeach; ?>
</tbody>
</table>
now shows...
as you can see, whene there is not event matching the time then no column is shown, however if I add the column in the else statement and even add a break the layout goes haywire...
The specification exists but it looks browsers doesn’t support it yet.
https://www.w3.org/TR/selectors4/#subject
The proposed code would be something like this:
!td .event-header { /* styles */ }
The only solution I see is to fall back to JavaScript. It could be worse than to refactor PHP thought.
If you want to change the style of a DOM node, you HAVE to apply CSS rules to it. You can't get around that. If you don't want <td class="..."> to do that, then you'll have to do <td style="..."> instead. One way or another, you'll HAVE to write CSS for this to happen.
So I'm trying to use the datatable as seen in my code written below. Unfortunately for some unknown reasons the functions for sorting, searching and paging does not work. May I know what did I missed out or the reason for it not working?
#extends('app')
#section('header')
<link href="//maxcdn.bootstrapcdn.com/bootstrap/3.3.5/css/bootstrap.min.css" rel="stylesheet">
<link href="https://cdn.datatables.net/1.10.10/css/dataTables.bootstrap.min.css" rel="stylesheet">
#stop
#section('content')
<h1 class="page-header">View Log Information</h1>
<br/>
<div class="container">
<div class="col-xs-12 col-md-12" style="overflow:auto">
<table class="table table-hover" id="logTable">
<thead>
<tr>
<th>
User
</th>
<th>
Log Description
</th>
<th>
Time Stamp
</th>
</tr>
</thead>
#foreach ($logs as $log)
<tbody>
<tr>
<td>
{{ App\User::find($log->user_id)->name }}
</td>
<td>
{{ $log->log_description }}
</td>
<td>
{{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
</td>
</tr>
</tbody>
#endforeach
</table>
</div>
</div>
#stop
#section('scripts')
<!-- Bootstrap Based Data Table Plugin Script-->
<script src="http://code.jquery.com/jquery-2.1.1.min.js" type="text/javascript"></script>
<script src="https://cdn.datatables.net/1.10.10/js/jquery.dataTables.min.js"></script>
<script src="https://cdn.datatables.net/1.10.10/js/dataTables.bootstrap.min.js" type="text/javascript"></script>
<script>
$(document).ready(function() {
$('#logTable').DataTable();
} );
</script>
#stop
try this:
<table class="table table-hover" id="logTable">
<thead>
<tr>
<th>
User
</th>
<th>
Log Description
</th>
<th>
Time Stamp
</th>
</tr>
</thead>
<tbody>
#foreach ($logs as $log)
<tr>
<td>
{{ App\User::find($log->user_id)->name }}
</td>
<td>
{{ $log->log_description }}
</td>
<td>
{{ date("F d Y - g:i a",strtotime("$log->created_at")) }}
</td>
</tr>
#endforeach
</tbody>
</table>
Move the #foreach statement under tbody. It's not working because you keep duplicating the tbody instead of <tr> inside the <tbody>.
And this is just a suggestion. Don't do the query at view. it's not a good practice. Do all the query inside controller. You can use Laravel Relationship for App\User::find($log->user_id)->name.
I am trying to get a Fusion Table SQL response into a basic HTML table. This is for both search engine fodder and for use with google spreadsheets and their importhtml function.
The foreach to turn the response into a table are turning up some unusual responses, like 1 character at a time? Also the response appears to be formatted as something that can easily be made into an array, but my efforts have been futile. Have been working on this for over two days now, and I bet someone understands the formatting and knows the answer far better than I?
<?php
$result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<h1>A Table of Clicks</h1>
<table class='data'>
<thead>
<tr>
<th>date</th>
<th>fundraiser</th>
<th>name</th>
<th>link</th>
<th>price</th>
<th>image</th>
<th>ip</th>
<th>merchant</th>
</tr>
</thead>
<tbody>
<?php
list($part1, $part2) = explode(' "rows": [[', $result);
$rows = explode(' ], [', $part2);
echo $rows[0]."<br>";
foreach ($rows as $row) {
{
//$array = array($row);
///var_dump($array);
$boxes = explode(",", $row);
foreach ($boxes as $box) {
echo "
<tr>
<td>".$box[0]."</td>
<td>".$box[1]."</td>
<td>".$box[2]."</td>
<td>".$box[3]."</td>
<td>".$box[4]."</td>
<td>".$box[5]."</td>
<td>".$box[6]."</td>
<td>".$box[7]."</td>
</tr>";
}
}
}
?>
</tbody>
</table>
<hr />
</div>
</body>
Use json_decode to parse the response(it's valid JSON):
<?php
$result = file_get_contents("https://www.googleapis.com/fusiontables/v1/query?sql=SELECT%20*%20FROM%201QN6e86FybBULPekKvvXd_RF1jw01H7bZAJFjhUg&key=AIzaSyAm9yWCV7JPCTHCJut8whOjARd7pwROFDQ");
$result=json_decode($result,true);
?>
<!DOCTYPE html>
<html>
<body>
<div id="page">
<h1>A Table of Clicks</h1>
<table class='data'>
<thead>
<tr>
<th><?php echo implode('</th><th>',$result['columns']);?></th>
</tr>
</thead>
<tbody>
<?php
foreach($result['rows'] as $row){
echo '<tr><td>'.implode('</td><td>',$row).'</td></tr>';
}
?>
</tbody>
</table>
<hr />
</div>
</body>