Laravel html table - php

I am trying to make a table for private messages that an user receives.
This is my code:
<table>
<?php
foreach ($mesaje as $mesaj)
{
foreach ($users as $user)
echo '<tr>';
{
if ($user->user_id == $mesaj->expeditor_id && Auth::user()->user_id == $mesaj->destinatar_id)
{
echo '<td style = " padding: 5px 0px 0px 100px;">' . $user->username . '</td>';
}
if (Auth::user()->user_id == $mesaj->destinatar_id)
{
echo '
<td style = " padding: 5px 0px 0px 195px;">' . $mesaj->subiect . '</td>
<td style = " padding: 5px 0px 0px 215px;">' . $mesaj->data_mesajului . '</td>
';
}
}
}
echo '</tr>'; ?>
</table>
Expeditor_id = the id of the user that sends the message
destinatar_id = the id of the user that receives the message
I want it to be a message then on another line another message(username, the subject and the data), I don't know why some usernames are displayed, some are not, can anyone give me a solution please?

try this
<table>
#foreach ($mesaje as $mesaj)
#foreach ($users as $user)
#if ($user->user_id == $mesaj->expeditor_id && Auth::user()->user_id == $mesaj->destinatar_id)
<tr>
<td style=" padding: 5px 0px 0px 100px;">{{$user->username}}</td>
<td style=" padding: 5px 0px 0px 195px;">{{$mesaj->subiect}}</td>
<td style=" padding: 5px 0px 0px 215px;"> {{$mesaj->data_mesajului}}</td>
</tr>
#endif
#endforeach
#endforeach
</table>

In your IF condition you need to use second IF condition
FOR Example :
<table>
<?php
foreach ($mesaje as $mesaj)
{
foreach ($users as $user)
echo '<tr>';
{
if ($user->user_id == $mesaj->expeditor_id && Auth::user()->user_id == $mesaj->destinatar_id)
{
echo '<td style = " padding: 5px 0px 0px 100px;">' . $user->username . '</td>';
if (Auth::user()->user_id == $mesaj->destinatar_id)
{
echo '
<td style = " padding: 5px 0px 0px 195px;">' . $mesaj->subiect . '</td>
<td style = " padding: 5px 0px 0px 215px;">' . $mesaj->data_mesajului . '</td>
';
}
}
}
}
echo '</tr>'; ?>
</table>

Related

MPDF not downloading PDF on cPanel but works fine when locally hosted

Here is the generatepdf.php:
<?php
include('db.php');
require_once 'vendor/autoload.php';
ob_start();
?>
<?php
$student_id = 0;
if (isset($_GET['id'])) {
$student_id = $_GET['id'];
}
// var_dump($student_id);
$fetchUser = $conn->query("SELECT * from student where id = $student_id")->fetchAll(PDO::FETCH_ASSOC);
$subjects = $conn->query("SELECT * FROM subjects WHERE student_id = $student_id")->fetchAll(PDO::FETCH_ASSOC);
//
// var_dump($fetchUser);
// Student Info
$name = $fetchUser[0]['name'];
$rollno = $fetchUser[0]['rollno'];
$image = $fetchUser[0]['image'];
$center = $fetchUser[0]['center'];
$division = $fetchUser[0]['division'];
$academic_year = $fetchUser[0]['academic_year'];
$class = $fetchUser[0]['class'];
$session = $fetchUser[0]['session1'] . ' - ' . $fetchUser[0]['session2'];
$father = $fetchUser[0]['father'];
$stream = $fetchUser[0]['stream'];
$totalmarks = $fetchUser[0]['totalmarks'];;
$obtmarks = $fetchUser[0]['obtmarks'];
$marksinwords = $fetchUser[0]['marksinwords'];
$percentage = ($fetchUser[0]['obtmarks'] / $fetchUser[0]['totalmarks']) * 100;
$percentage = number_format($percentage, 2);
$todaysDate = gmdate("M d, Y", strtotime('now'));
$html = '
<html>
<head>
<style>
body {font-family: sans-serif;
font-size: 10pt;
}
p { margin: 0pt; }
table.items {
border: 0.1mm solid #000000;
}
td { vertical-align: top; }
.items td {
border-left: 0.1mm solid #000000;
border-right: 0.1mm solid #000000;
}
table thead td { background-color: #EEEEEE;
text-align: center;
border: 0.1mm solid #000000;
font-variant: small-caps;
}
.items td.blanktotal {
background-color: #EEEEEE;
border: 0.1mm solid #000000;
background-color: #FFFFFF;
border: 0mm none #000000;
border-top: 0.1mm solid #000000;
border-right: 0.1mm solid #000000;
}
.items td.totals {
text-align: right;
border: 0.1mm solid #000000;
}
.items td.cost {
text-align: "." center;
}
</style>
</head>
<body>
<!--mpdf
<htmlpageheader name="myheader">
<table width="100%"><tr>
<td width="80%" style="color:#000; "><span style="font-weight: bold; font-size: 14pt;">Online Marksheet For TELEGANA UNIVERSITY</span></td>
// <td width="20%" style="text-align: right;">Date: ' . $todaysDate . '<br />
</tr></table>
</htmlpageheader>
<htmlpagefooter name="myfooter">
<div style="border-top: 1px solid #000000; font-size: 9pt; text-align: center; padding-top: 3mm; ">
Page {PAGENO} of {nb}
</div>
</htmlpagefooter>
<sethtmlpageheader name="myheader" value="on" show-this-page="1" />
<sethtmlpagefooter name="myfooter" value="on" />
mpdf-->
<img style="margin-top: -50px;padding: 15px; border: 2px solid #000; height: 100px;margin: auto !important;width: 100px;background-size: contain;background-repeat: no-repeat;background-position: center;margin-left: 80px !important;" src="images/' . $image . '" class="img-thumbnail">
<table width="100%" style="font-family: serif;" cellpadding="10"><tr>
<td width="55%"><br /><br /><span style="font-weight: bold;">Name:</span> ' . $name . '<br /><span style="font-weight: bold;">Father:</span> ' . $father . '<br /><span style="font-weight: bold;">Center:</span> ' . $center . '<br /><span style="font-weight: bold;">Division:</span> ' . $division . '<br /><span style="font-weight: bold;">Class:</span> ' . $class . '</td>
<td width="5%"> </td>
<td width="40%"><br /><br /><span style="font-weight: bold;">Hall Ticket / Roll No:</span> ' . $rollno . '<br /><span style="font-weight: bold;">Session:</span> ' . $session . '<br /><span style="font-weight: bold;">Stream:</span> ' . $stream . '<br /><span style="font-weight: bold;">Year of Passing:</span> ' . $academic_year . '</td>
</tr></table>
<br />
<table class="items" width="100%" style="font-size: 9pt; border-collapse: collapse; " cellpadding="8">
<thead>
<tr>
<td width="15%">Sub Code</td>
<td width="40%">Subject Name</td>
<td width="10%">Theory</td>
<td width="15%">Practicals</td>
<td width="20%">Marks Secured</td>
</tr>
</thead>
<tbody>
<!-- ITEMS HERE -->
';
foreach ($subjects as $subject) {
$html .= '<tr>';
$html .= '<td align="center">' . $subject['subcode'] . '</td>';
$html .= '<td align="center">' . $subject['subject_name'] . '</td>';
$html .= '<td>' . $subject['total_marks'] . '</td>';
$html .= '<td class="cost">' . $subject['obtained_marks'] . '</td>';
$html .= '<td class="cost">' . $subject['marksinwords'] . '</td>';
$html .= '</tr>';
}
$html .= '
<tr style="border-top: 2px solid #000;">
<td style="border-top: 2px solid #000;" align="center" >Percentage: ' . $percentage . '</td>
<td style="border-top: 2px solid #000;" align="center"></td>
<td style="border-top: 2px solid #000;">' . $totalmarks . '</td>
<td style="border-top: 2px solid #000;" class="cost">' . $obtmarks . '</td>
<td style="border-top: 2px solid #000;" class="cost">' . $marksinwords . '</td>
</tr>
</tbody>
</table>
</body>
</html>
';
require_once 'bootstrap.php';
$mpdf = new \Mpdf\Mpdf([
'margin_left' => 10,
'margin_right' => 10,
'margin_top' => 30,
'margin_bottom' => 25,
'margin_header' => 10,
'margin_footer' => 10
]);
$mpdf->SetTitle("Online Marksheet");
$mpdf->SetDisplayMode('fullpage');
$mpdf->WriteHTML($html);
$mpdf->Output();
This is working perfectly fine in XAMPP for me, and is downloading the PDF every time I load the page.
But when I hosted these exact same files on cPanel's File Manager - it's only showing me a blank page. No errors. What can I do?
I've checked the PHP versions of both my XAMPP (v8.0) and PHP(v7.3) but that doesn't seem to be the issue (unless one of you think it is?)
To Troubleshoot:
Comment out
$mpdf->WriteHTML($html);
$mpdf->Output();
instead, just echo $html; first, just to see if all is correct.
Why?
Make sure that your html is not malformed.
If there's error messages or any php headers are sent already PDF will be corrupted and will show error on render.
Things to check before output();
All paths include/require paths are still correct or you uploaded them differently than your XAMPP?
Why ob_start() but I do not see where you ob_get_contents() and ob_end_clean() in the code? Effectively it would keep buffering without outputting. Server might be more strict than your XAMPP and gives you warning.
After all errors checked, paths corrected if were incorrect - your user input is not santised thats a big NO NO.
regarding your it's only showing me a blank page. No errors. What can I do? statement. There's either critical error and you need to check logs.
In this case, likely "require" path to bootstrap is incorrect.
If it's no errors in error logs, then likely your ob_start() would forbid you from outputting anything.

How to change the padding with HTML/PHP?

This is my php code I use. But for on or other reason it doesn't change with my CSS. I keeps on giving it weight of the picture lenght & height. You can see in the screenshot what goes wrong. The HtmlTable of my does change but my doesn't. WORKS -> PHP ERROR CSS -> ERROR
$htmlTabel = '';
$htmlTabel .= '<table border="1px">';
$htmlTabel .= '<tr>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Foto</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Type</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Prijs</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Adres</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Woonopp</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Grondopp</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Slaapkamers</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Badkamers</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Bebouwing</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Bouwjaar</th>';
$htmlTabel .= '<th style="padding: 10px; padding-right: 35px; padding-left: 35px;">Beschrijving</th>';
$htmlTabel .= '</tr>';
for ($i = 1; $i <= $adsId; $i++) {
$url = "http://localhost:55342/api/tekoop/" . $i; // path to your JSON file
$data = file_get_contents($url); // put the contents of the file into a variable
$apidata = json_decode($data); // decode the JSON feed
$img = "<img src='img/$apidata->images' width='500px' height='500px'></img>";
$htmlTabel .= '<tr>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $img . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->type . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->prijs . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->adres . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->woonopp . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->grondopp . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->slaapkamers . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->badkamers . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->bebouwing . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->bouwjaar . '</td>' .
'<td style="padding: 10px; padding-right: 35px; padding-left: 35px;">' . $apidata->text . '</td>' .
'</tr>';
}
$htmlTabel .= '</table>';
$conn->close();
?>
HTML CODE
<div class="container" style="margin-left:150px; margin-right:150px; font-family:Georgia; margin-top: 75px; text-align: center; ">
<?php echo $htmlTabel ?>
</div>

Alternating and colored lines in php

I'm trying to apply color on alternate lines in the following code in PHP, but I already tried in various shapes and examples here in the forum and it did not solve. I need only leave one white line, and another gray, filling in according to the mysql query. I would like to make this list without using CSS. Can someone help?
<style type="text/css">
.whiteBackground { background-color: #fff; }
.grayBackground { background-color: #ccc; }
</style>
<table class="table-bordered" style="text-align: center; margin-left: -50px; font-family: Verdana, Geneva, sans-serif; margin-top: 20px;" border="0" cellspacing="1" cellpadding="2" align="center">
<table class="table-bordered" style="text-align: center; margin-left: -50px; font-family: Verdana, Geneva, sans-serif; margin-top: 20px;" border="0" cellspacing="1" cellpadding="2" align="center">
<thead>
<tr>
<th style="font-size: 1em; padding: 5px;">Client</th>
<th style="font-size: 1em; padding: 5px;">Tech</th>
<th style="font-size: 1em; padding: 5px;">Status</th>
<th style="font-size: 1em; padding: 5px;">Data</th>
<th style="font-size: 1em; padding: 5px;">Start</th>
<th style="font-size: 1em; padding: 5px;">End</th>
<th style="font-size: 1em; padding: 5px;">Total Hours</th>
</tr>
</thead>
<tbody>
<?php
$x++;
$class = ($x%2 == 0)? 'whiteBackground': 'graybackground';
echo "<tr class='$class'>";
foreach ($os as $c) {
$query = "SELECT nome from users where idUsers=$c->users_id";
$data= $this->db->query($query)->result();
echo '<tr >';
echo '<td >' . $c->nameclient . '</td>';
echo '<td >' . $data[0]->name . '</td>';
echo '<td >' . $c->status . '</td>';
echo '<td >' . date('d/m/Y', strtotime($c->startDate)) . '</td>';
echo '<td >' . $c->startTime . '</td>';
echo '<td >' . $c->endTime . '</td>';
$time_diff = strtotime($c->endTime) - strtotime($c->startTime);
$var= $time_diff/60;
echo '<td >'.date('H:i', mktime(0,$var)).'</td>';
echo '</tr>';
}
?>
I'll show the example of using % (or Mod) to alternate the background color on a simple array.
$arr = array("fred","tim","bob","jimmy");
echo "<table>";
$i=1;
foreach ($arr as $row) {
$class = ($i % 2) ? " odd": " even"; //even or odd?
echo "<tr class=" . $class . ">";
echo "<td>" . $row . "</td>";
echo "</tr>";
$i++;
}
echo "</table>"
?>
<style type="text/css">
.even, .odd {
color:white;
}
.even {
background-color: blue;
}
.odd {
background-color: red;
}
</style>
And, how to do it in purely CSS. (IE 9 or greater: https://www.w3schools.com/cssref/sel_nth-child.asp)
$arr = array("fred","tim","bob","jimmy");
echo "<table>";
$i=1;
foreach ($arr as $row) {
echo "<tr>";
echo "<td>" . $row . "</td>";
echo "</tr>";
$i++;
}
echo "</table>"
?>
<style type="text/css">
.even, .odd {
color:white;
}
table tr:nth-child(even) {
background-color: blue;
}
table tr:nth-child(odd) {
background-color: red;
}
</style>
Just change your css to this
<style type="text/css">
/* .whiteBackground { background-color: #fff; } */
/* .grayBackground { background-color: #ccc; } */
tbody tr:even{background-color:#fff;}
tbody tr:odd{background-color:#ccc;}
</style>
I dont think many people use the HTML attribut bgcolor anymore, but you can use it in the TR tag bgcolor="#cccccc". Or you can just use an inline style of background-color: #cccccc;
$highlight = false;
foreach ($os as $c)
{
$query = "SELECT nome from users where idUsers=$c->users_id";
$data= $this->db->query($query)->result();
$time_diff = strtotime($c->endTime) - strtotime($c->startTime);
$var= $time_diff/60;
$highlight = !$highlight;
echo '
<tr '.($highlight ? 'bgcolor="#cccccc"' : '').'>
<td >' . $c->nameclient . '</td>
<td >' . $data[0]->name . '</td>
<td >' . $c->status . '</td>
<td >' . date('d/m/Y', strtotime($c->startDate)) . '</td>
<td >' . $c->startTime . '</td>
<td >' . $c->endTime . '</td>
<td >'.date('H:i', mktime(0,$var)).'</td>
</tr>';
}

Unable to block while loop repeats

Working on invoice module which has two different tables master_table and detail_table. Know i want to display master_table's data above the table then in the table structure showing data of detail_table for each three rows header.
My code looks this.
$n = 0;
$htmlpage = 1;
$query = $dbConnection->prepare("SELECT * FROM master WHERE (DocNo BETWEEN ? AND ?) ORDER BY DocNo ASC");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch())
{
$n++;
$noInvs = $rows['No_of_Inv'];
$dno = $rows['DocNo'];
$name = $rows['Customer_Name'];
Step-1: Now i'm pulling client data from another table
$qry = $dbConnection->prepare("SELECT * FROM client WHERE client_name=?");
$qry->execute(array($name));
$row = $qry->fetch();
$add = $row['address'];
$city = $row['city'];
$proj = $rows['Project'];
$projNo = $rows['Project_No'];
$sermnth = $rows['Service_Month'];
$grandtotal = $rows['Total_Amt'];
$rupee = number2word($grandtotal);
$index = 1;
$ratetot = 0;
$subtotal = 0;
$lastPage = ceil($noInvs/3);
$pageCount = 0;
Step-2: Now check the loop iteration
while($index <= $noInvs)
{
$cnt = 1;
$body = "<div class='main-container'><table width='100%' style='padding:0 0 0 0;' align='center' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' align='center' style='border:1px solid #000;'>
<strong style='font-size:30px;'>Data</strong>
</td>
</tr>
<tr>
<td valign='top' align='left' style='padding: 0 0px 0 0px;border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width='100%' align='left' cellpadding='0' cellspacing='0'>
<tr>
<td valign='top' style='width:50%; padding:5px; font-size:13px; border-right: solid 1px #000000;text-align:left;'><strong>CUSTOMER:</strong> <br /><p style='font-size:12px;'>$name<br />$add, $city</p></td>
<td valign='top' style='width:50%; font-size:13px;'>
<table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='border-right: solid 1px #000000;padding:5px;width:40%;font-size:12px;'><br />Invoice No:<br />Invoice Date:</td>
<td align='left' style='padding:5px;font-size:12px;'><br />$ino<br />$invdate</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
</td>
</tr>
<tr>
<td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;'>
<table width=100% cellpadding='0' cellspacing='0' >
<tbody>
<tr style='border-right:solid 1px #000000;border-bottom:solid 1px #000000;'>
<td style='width:5%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>SN.</td>
<td style='width:45%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Description of Services</td>
<td style='width:13%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Per</td>
<td style='width:10%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Quantity</td>
<td style='width:12%;text-align:center; border-right:solid 1px #000000;border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Rate</td>
<td style='width:15%;text-align:center; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>Amount in Rs.</td>
</tr>";
Step-4: check the loop condition
while(true)
{
Step-5: checking the iteration count
if($cnt>3)
{
$pageCount++;
break;
}
Step-6: Pulling data from details table
$query1 = $dbConnection->prepare("SELECT * FROM details WHERE DocNo=? AND DocNO_Index=?");
$query1->execute(array($dno,$index));
$rowCnt = $query1->rowCount();
while($rows1 = $query1->fetch())
{
$desg = $rows1['Item'];
$des = $rows1['Description'];
$Quantity = $rows1['Quantity'];
$Rate = $rows1['Rate'];
$per = $rows1['Per'];
$gross = $rows1['Gross'];
$SERVICE_TAX = $rows1['SERVICE'];
$SERVICE_TAX = ($SERVICE_TAX==null)? 0.0 : $SERVICE_TAX;
$SB_Tax = $rows1['SB_Tax'];
$SB_Tax = ($SB_Tax==null)? 0.0 : $SB_Tax;
$Krishi_Kalyan_Cess = $rows1['Krishi_Kalyan_Cess'];
$Krishi_Kalyan_Cess = ($Krishi_Kalyan_Cess==null)? 0.0 : $Krishi_Kalyan_Cess;
$ratetot = $ratetot + $Rate;
$subtotal = $subtotal + $gross;
if($cnt == 1 && $index > 1)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>Opening Balance</td><td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;text-align:right;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot - $Rate, 2)." /-</td><td style='width:15%;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal-$gross, 2)." /-</td></tr>";
}
$body.= "<tr><td style='width:5%;height:90px;vertical-align:top;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>$index</td><td style='width:45%;height:90px;vertical-align:top; border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($proj!=null)
$body.= "$proj<br/>";
if($desg!=null)
$body.= "$desg<br/>";
if($des!=null)
$body.= "$des<br/>";
if($projNo!=null)
$body.= "$projNo<br/>";
if($sermnth!=null)
$body.= "$sermnth<br/>";
$body.= "</td><td style='width:13%;height:90px;vertical-align:top;text-align:center; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>$per</td><td style='width:10%;height:90px;vertical-align:top; border-right:solid 1px #000000;padding:3px 5px 3px 5px;text-align:center;;font-size:12px;'>$Quantity</td><td style='width:12%;height:90px;vertical-align:top;text-align:right; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($Rate,2)."/-</td><td style='width:15%;height:90px;vertical-align:top; padding:3px 5px 3px 5px; text-align:right;;font-size:12px;'>". number_format($gross,2)."/-</td></tr>";
if($cnt == 3 || $index == $noInvs)
{
$body.= "<tr><td style='width:5%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:45%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($index != $noInvs)
{
$body.= "Closing Balance";
}
$body.= "</td> <td style='width:13%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:10%;border-right:solid 1px #000;padding:3px 5px 3px 5px;font-size:12px;'></td><td style='width:12%;border-right:solid 1px #000;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($ratetot,2)."/-</td><td style='width:15%;border-top:solid 1px #000;text-align:right;padding:3px 5px 3px 5px;font-size:12px;'>". number_format($subtotal,2)."/-</td></tr>";
}
}
$index++;
$cnt++;
}
$body .= "</tbody></table></td></tr>";
$stax = round($subtotal*($SERVICE_TAX/100),2);
$sbc = round($subtotal*($SB_Tax/100),2);
$kkc = round($subtotal*($Krishi_Kalyan_Cess/100),2);
$grandtotal = round(($subtotal+$stax+$sbc+$kkc),2);
$rupee = number2word($grandtotal);
$body .="<tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table cellpadding='0' cellspacing='0' width='100%'><tr><td rowspan='2' style='width:50%;height:50px; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= "<strong style='font-size:12px;'>Narration : </strong><br/>Amount in Words (Rupees):<br/>";
}
$body .= "<br/>$rupee";
$body .= "</td><td style='width:35%; border-bottom:solid 1px #000000;border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Sub Total : <br />Service Tax # 14% <br />Swachh Bharat Cess # 0.5% <br />Krishi Kalyan Cess # 0.5% <br />";
}
$body .= "</td><td style='width:15%;text-align:right; border-bottom:solid 1px #000000;padding:3px 5px 3px 5px;;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "". number_format($subtotal,2)."/-<br />".number_format($stax,2)."/-<br />".number_format($sbc,2)."/-<br />".number_format($kkc,2)."/-<br />";
}
$body .= "</td></tr><tr><td style='width:35%; border-right:solid 1px #000000;padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount==$lastPage)
{
$body .= "Total Amount";
}
$body .= "</td><td style='width:15%;text-align:right; padding:3px 5px 3px 5px;font-size:12px;'>";
if($pageCount == $lastPage)
{
$body .= number_format($grandtotal,2)."/-";
}
$body .= "</td></tr></table></td></tr><tr><td valign='top' align='left' style='border-left: solid 1px #000000;border-right: solid 1px #000000;border-bottom: solid 1px #000000;;font-size:12px;'><table width='100%' cellpadding='0' cellspacing='0'>
<tbody>
<tr>
<td style='width:50%; padding:3px 5px 3px 5px;border-right:1px solid #000;font-size:12px;'>
<strong>Thank you</strong>
</td>
<td valign='bottom' align='center' style='width:50%; text-align:center;font-size:12px;'>
<strong>AUTHORISED SIGNATORY</strong>
</td>
</tr>
</tbody>
</table>
</td>
</tr>
</table>
<p> </p></div>";
$body .= "<html><head><title>$ino</title><style type='text/css'>.main-container{display: block;} #media print{.main-container{display: block;}#page{size:portrait;margin:10px auto;padding:0}.main-container{page-break-after:always!important;}}</style><script language='javascript' type='text/javascript'>function PrintPage() { window.print(); } </script></head><body style='font-size: 12px ! important;'>$body</body></html><!-- onload='window.print()'-->";
echo "$body";
}
}
Issue is loop iteration.
My code output like this
Any suggestions on how to do this or to point me in the right direction would be greatly appreciated.
You're trying to re-use an active database connection. That doesn't work. You need two connections.
Specifically, you're in the process of reading a result set from a query on your connection,
$query = $dbConnection->prepare("some query");
$query->execute(array($fdcu,$tdcu));
while($rows = $query->fetch()) {
...
when you issue another query on the same $dbConnection ...
$qry = $dbConnection->prepare("another query");
$qry->execute(array($name));
You Can't Do Thatâ„¢ while the first query is still active. Open up and use a different connection for the queries issued from inside your loop.

PHP alternating colors

$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
while($row = mysql_fetch_array($result)) {
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
I wanted to add an alternating color in the loop. what code should I add?
$rowCount = 0;
$colorOne = '#ffffff';
$colorTwo = '#f3f3f3';
while($row = mysql_fetch_array($result)){
$rowColor = ($rowCount % 2) ? $colorOne : $colorTwo;
echo "<element bgcolor='$rowColor'></element>";
$rowCount++;
}
Edited after #Jayrox comment.
Use CSS classes instead of inline styles. They're easier to manipulate. Then style the .myclass_row_0 and .myclass_row_1 in your CSS for the alternate row color as well as .col1 and .col2 for the column styles.
$c=1; // or 0 (added after deceze's comment)
while($row = mysql_fetch_array($result)) {
$c=1-$c; // magic!
echo "
<tr class=\"myclass_row_$c\">
<td class=\"col1\">".$row['first_name']." ".$row['last_name']."</td>
<td class=\"col2\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
Or using inline styles:
$colors = array('#ffffff','#f3f3f3');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr style=\"background-color:{$colors[$c]};\">
<!-- tds here -->
</tr>
";
}
Or using preset classes:
$styles = array('odd','even');
$c=1;
while($row = mysql_fetch_array($result)) {
$c=1-$c;
echo "
<tr class=\"{$styles[$c]}\">
<!-- ... -->
</tr>
";
}
There are already a lot of solutions, but this one use only two lines of css.
You can set specific css on even and odd rows:
tr:nth-child(even) {background: #CCC}
tr:nth-child(odd) {background: #FFF}
Usually, I will declare an index(int) value and increment for each time through the loop. Then do a check to see if index mod 2 = 1. If so, then output a tr with the style that you want to apply to show an alternating row.
$color = 0;
while($row = mysql_fetch_array($result)) {
if($color % 2 == 1){
echo "<tr>";
}else{
echo "<tr class=\"altRow\">";
}
echo "
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color++;
}
As it's bumped already, here is my 5 cents.
7 answers and not a single one using templates.
We can write a thousand articles of the templates necessity, but such an examples will always win.
So, based on the OP's code and stagas' answer:
business logic part:
$c = 1;
$DATA = array();
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql) or trigger_error(mysql_error().$sql);
while($row = mysql_fetch_array($result)) {
$row['c'] = $c=1-$c;
$DATA[] = $row;
}
and template part:
<tr class="myclass_row_<?=$row['$c']?>">
<td class="col1"><?=$row['first_name']?> <?=$row['last_name']?></td>
<td class="col2">
<input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn">
</td>
</tr>
$color = 1;
while($row = mysql_fetch_array($result)) {
if($color == 1){
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$color = 2;
}
else
{
echo "
<tr>
<td style=\"padding-left: 20px; border-bottom: 1px solid #555; border-right: 1px solid #555;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #555; border-right: 1px solid #555; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
}
$color = 1;
}
// here it is in less code
<?php
$dbc = mysql_connect('localhost','root','') or die (mysql_error());
mysql_select_db('payroll') or die (mysql_error());
$sql = "SELECT * FROM employee ORDER BY employee_id DESC";
$result = mysql_query($sql);
$colors = array('FFF', '000000'); // valid hex colors
$numOfColors = sizeOf($colors); $i = 0
while($row = mysql_fetch_array($result)) {
$i++;if($i>$numOfColors){$i=0;}
?>
<tr>
<td style="padding-left: 20px; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>;"> <?=$row['first_name']?> <?=$row['last_name']?> </td>
<td style="text-align: center; border-bottom: 1px solid #<?=$colors[$i]?>; border-right: 1px solid #<?=$colors[$i]?>; padding-top: 2px ; padding-bottom: 3px ;"><input type="button" name="edit" value="Edit" class="selbtn"> <input type="button" name="delete" value="Delete" class="selbtn"></td>
</tr>
<?php
}
?>
Just keep track whether it is an alternating row with a Boolean. Initialize it to false before your loop, not it for each iteration, then you can set the row style based on its value. Something like:
...
$isAlternatingRow = false;
while($row = mysql_fetch_array($result)) {
echo "
<tr class=\"" . $isAlternatingRow ? "defaultRow" : "alternatingRow" . "\">
<td style=\"padding-left: 20px; border-bottom: 1px solid #999; border-right: 1px solid #999;\">".$row['first_name']." ".$row['last_name']."</td>
<td style=\"text-align: center; border-bottom: 1px solid #999; border-right: 1px solid #999; padding-top: 2px ; padding-bottom: 3px ;\"><input type=\"button\" name=\"edit\" value=\"Edit\" class=\"selbtn\"> <input type=\"button\" name=\"delete\" value=\"Delete\" class=\"selbtn\"></td>
</tr>
";
$isAlternatingRow = !($isAlternatingRow);
}
Then just define styles for tr.defaultRow td and tr.alternatingRow td.
$class="even"
while($row = mysql_fetch_array($result))
{
if($class == "even")
{
echo "<tr class='$class'>";
$class="odd"
}
else
{
echo "<tr class='$class'>";
$class="even";
}
...
}
I did it like this:
Remember to add a CSS class called "even", with styling of course.
<?php
include 'connect.php';
echo "<table id='hor-zebra'>";
$i = 0;
while($row = mysql_fetch_array($result))
{
if($i % 2 == 0)
{
echo "<tr class='even'>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
else
{
echo "<tr>";
echo "<td>" . $row['something'] . "</td>";
echo "</tr>";
}
$i++;
}
echo "</table>";
mysql_close($con);
?>
Using predefined color outside the loop.
$rowCount = 0;
$color = array('#ffffff','#f3f3f3');
while($row = mysql_fetch_array($result)){
$i = ($rowCount % 2);
echo "<element bgcolor='".$color["$i"]."'></element>";
$rowCount++;
}

Categories