write a PDF file in Arabic using PHP - php

I have added PHP code to generate a PDF file in Arabic and English, but when content is in Arabic it shows like this php arabic output :
I have checked the font i'm using is compatible with the Arabic language. I am not sure what I am missing. I also have the unicode to be UTF-8 in my configuration file.
here is the code
<?php
$paid_amount = $this->invoice_model->calculate_to('paid_amount', $invoice_info->invoices_id);
$client_info = $this->invoice_model->check_by(array('client_id' => $invoice_info->client_id), 'tbl_client');
if (!empty($client_info)) {
$currency = $this->invoice_model->client_currency_sambol($invoice_info->client_id);
$client_lang = $client_info->language;
} else {
$client_lang = 'arabic';
$currency = $this->invoice_model->check_by(array('code' => config_item('default_currency')), 'tbl_currencies');
}
unset($this->lang->is_loaded[5]);
$language_info = $this->lang->load('sales_lang', $client_lang, TRUE, FALSE, '', TRUE);
$payment_status = $this->invoice_model->get_payment_status($invoice_info->invoices_id);
$uri = $this->uri->segment(3);
if ($uri == 'invoice_email') {
$img = base_url() . config_item('invoice_logo');
} else {
$img = $_SERVER['DOCUMENT_ROOT'] . '/' . config_item('invoice_logo');
$a = file_exists($img);
if (empty($a)) {
$img = base_url() . config_item('invoice_logo');
}
}
?>
<table class="clearfix">
<tr>
<td>
<div id="logo" style="margin-top: 8px;">
<img style=" height: 70px;" src="<?= $img ?>">
</div>
</td>
<td>
<div id="company">
<h2 class="name"><?= (config_item('company_legal_name_' . $client_lang) ? config_item('company_legal_name_' . $client_lang) : config_item('company_legal_name')) ?></h2>
<div><?= (config_item('company_address_' . $client_lang) ? config_item('company_address_' . $client_lang) : config_item('company_address')) ?></div>
<div><?= (config_item('company_city_' . $client_lang) ? config_item('company_city_' . $client_lang) : config_item('company_city')) ?>
, <?= config_item('company_zip_code') ?></div>
<div><?= (config_item('company_country_' . $client_lang) ? config_item('company_country_' . $client_lang) : config_item('company_country')) ?></div>
<div> <?= config_item('company_phone') ?></div>
<div><?= config_item('company_email') ?></div>
</div>
</td>
</tr>
</table>
<table id="details" class="clearfix">
<tr>
<td>
<div id="client">
<?php
if (!empty($client_info)) {
$client_name = $client_info->name;
$address = $client_info->address;
$city = $client_info->city;
$zipcode = $client_info->zipcode;
$country = $client_info->country;
$phone = $client_info->phone;
$email = $client_info->email;
} else {
$client_name = '-';
$address = '-';
$city = '-';
$zipcode = '-';
$country = '-';
$phone = '-';
$email = '-';
}
?>
<h2 class="name"><?= $client_name ?></h2>
<div class="address"><?= $address ?></div>
<div class="address"><?= $city ?>, <?= $zipcode ?>
,<?= $country ?></div>
<div class="address"><?= $phone ?></div>
<div class="email"><?= $email ?></div>
</div>
</td>
<td>
<div id="invoice">
<h1><?= $invoice_info->reference_no ?></h1>
<div class="date"><?= $language_info['invoice_date'] ?>
:<?= strftime(config_item('date_format'), strtotime($invoice_info->invoice_date)); ?></div>
<div class="date"><?= $language_info['due_date'] ?>
:<?= strftime(config_item('date_format'), strtotime($invoice_info->due_date)); ?></div>
<?php if (!empty($invoice_info->user_id)) { ?>
<div class="date">
<?= lang('sales') . ' ' . lang('agent') ?><?php
$profile_info = $this->db->where('user_id', $invoice_info->user_id)->get('tbl_account_details')->row();
if (!empty($profile_info)) {
echo $profile_info->fullname;
}
?>
</div>
<?php } ?>
<div class="date"><?= $language_info['payment_status'] ?>: <?= $payment_status ?></div>
</div>
</td>
</tr>
</table>
<table class="items" border="0" cellspacing="0" cellpadding="0" page-break-inside: auto;>
<thead>
<tr>
<th class="desc"><?= $language_info['items'] ?></th>
<?php
$invoice_view = config_item('invoice_view');
if (!empty($invoice_view) && $invoice_view == '2') {
?>
<th><?= $language_info['hsn_code'] ?></th>
<?php } ?>
<th class="unit"><?= $language_info['qty'] ?></th>
<th class="desc"><?= $language_info['price'] ?></th>
<th class="unit"><?= $language_info['tax'] ?></th>
<th class="total"><?= $language_info['total'] ?></th>
</tr>
</thead>
<tbody>
<?php
$invoice_items = $this->invoice_model->ordered_items_by_id($invoice_info->invoices_id);
if (!empty($invoice_items)) :
foreach ($invoice_items as $key => $v_item) :
$item_name = $v_item->item_name ? $v_item->item_name : $v_item->item_desc;
$item_tax_name = json_decode($v_item->item_tax_name);
?>
<tr>
<td class="desc"><h3><?= $item_name ?></h3><?= nl2br($v_item->item_desc) ?></td>
<?php
$invoice_view = config_item('invoice_view');
if (!empty($invoice_view) && $invoice_view == '2') {
?>
<td><?= $v_item->hsn_code ?></td>
<?php } ?>
<td class="unit"><?= $v_item->quantity . ' ' . $v_item->unit ?></td>
<td class="desc"><?= display_money($v_item->unit_cost) ?></td>
<td class="unit"><?php
if (!empty($item_tax_name)) {
foreach ($item_tax_name as $v_tax_name) {
$i_tax_name = explode('|', $v_tax_name);
echo '<small class="pr-sm">' . $i_tax_name[0] . ' (' . $i_tax_name[1] . ' %)' . '</small>' . display_money($v_item->total_cost / 100 * $i_tax_name[1]) . ' <br>';
}
}
?></td>
<td class="total"><?= display_money($v_item->total_cost) ?></td>
</tr>
<?php endforeach; ?>
<?php endif ?>
</tbody>
<tfoot>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['sub_total'] ?></td>
<td><?= display_money($this->invoice_model->calculate_to('invoice_cost', $invoice_info->invoices_id)) ?></td>
</tr>
<?php if ($invoice_info->discount_total > 0): ?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['discount'] ?>(<?php echo $invoice_info->discount_percent; ?>%)</td>
<td> <?= display_money($this->invoice_model->calculate_to('discount', $invoice_info->invoices_id)) ?></td>
</tr>
<?php endif;
$tax_info = json_decode($invoice_info->total_tax);
$tax_total = 0;
if (!empty($tax_info)) {
$tax_name = $tax_info->tax_name;
$total_tax = $tax_info->total_tax;
if (!empty($tax_name)) {
foreach ($tax_name as $t_key => $v_tax_info) {
$tax = explode('|', $v_tax_info);
$tax_total += $total_tax[$t_key];
?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $tax[0] . ' (' . $tax[1] . ' %)' ?></td>
<td> <?= display_money($total_tax[$t_key]); ?></td>
</tr>
<?php }
}
} ?>
<?php if ($tax_total > 0): ?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['total'] . ' ' . $language_info['tax'] ?></td>
<td><?= display_money($tax_total); ?></td>
</tr>
<?php endif;
if ($invoice_info->adjustment > 0): ?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['adjustment'] ?></td>
<td><?= display_money($invoice_info->adjustment); ?></td>
</tr>
<?php endif ?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['total'] ?></td>
<td><?= display_money($this->invoice_model->calculate_to('total', $invoice_info->invoices_id), $currency->symbol); ?></td>
</tr>
<?php
$paid_amount = $this->invoice_model->calculate_to('paid_amount', $invoice_info->invoices_id);
if ($paid_amount > 0) {
$total = $language_info['total_due'];
if ($paid_amount > 0) {
$text = 'style="color:red"';
?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><?= $language_info['paid_amount'] ?></td>
<td><?= $paid_amount ?></td>
</tr>
<?php } else {
$text = '';
} ?>
<tr class="total">
<td colspan="3"></td>
<td colspan="1"><span <?= $text ?>><?= $total ?></span></td>
<td><?= display_money($this->invoice_model->calculate_to('invoice_due', $invoice_info->invoices_id), $currency->symbol); ?></td>
</tr>
<?php } ?>
</tfoot>
</table>
<div id="thanks"><?= lang('thanks') ?>!</div>
<div id="notices">
<div class="notice"><?= $invoice_info->notes ?></div>
</div>
<?php
$invoice_view = config_item('invoice_view');
if (!empty($invoice_view) && $invoice_view > 0) {
?>
<style type="text/css">
.panel {
margin-bottom: 21px;
background-color: #ffffff;
border: 1px solid transparent;
-webkit-box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
box-shadow: 0 1px 1px rgba(0, 0, 0, 0.05);
}
.panel-custom .panel-heading {
border-bottom: 2px solid #2b957a;
}
.panel .panel-heading {
border-bottom: 0;
font-size: 14px;
}
.panel-heading {
padding: 10px 15px;
border-bottom: 1px solid transparent;
border-top-right-radius: 3px;
border-top-left-radius: 3px;
}
.panel-title {
margin-top: 0;
margin-bottom: 0;
font-size: 16px;
}
</style>
<div class="panel panel-custom" style="margin-top: 20px">
<div class="panel-heading" style="border:1px solid #dde6e9;border-bottom: 2px solid #57B223;">
<div class="panel-title"><?= lang('tax_summary') ?></div>
</div>
<table class="items" border="0" cellspacing="0" cellpadding="0" page-break-inside: auto;>
<thead>
<tr>
<th class="desc"><?= $language_info['items'] ?></th>
<?php
$invoice_view = config_item('invoice_view');
if (!empty($invoice_view) && $invoice_view == '2') {
?>
<th><?= lang('hsn_code') ?></th>
<?php } ?>
<th class="unit"><?= $language_info['qty'] ?></th>
<th class="desc"><?= $language_info['tax'] ?></th>
<th class="unit" style="text-align: right"><?= $language_info['total_tax'] ?></th>
<th class="total" style="text-align: right"><?= $language_info['tax_excl_amt'] ?></th>
</tr>
</thead>
<tbody>
<?php
$total_tax = 0;
$total_cost = 0;
if (!empty($invoice_items)) :
foreach ($invoice_items as $key => $v_item) :
$item_tax_name = json_decode($v_item->item_tax_name);
$tax_amount = 0;
?>
<tr>
<td class="desc"><?= $v_item->item_name ?></td>
<?php
$invoice_view = config_item('invoice_view');
if (!empty($invoice_view) && $invoice_view == '2') {
?>
<td><?= $v_item->hsn_code ?></td>
<?php } ?>
<td class="unit"><?= $v_item->quantity . ' ' . $v_item->unit ?></td>
<td class="desc"><?php
if (!empty($item_tax_name)) {
foreach ($item_tax_name as $v_tax_name) {
$i_tax_name = explode('|', $v_tax_name);
$tax_amount += $v_item->total_cost / 100 * $i_tax_name[1];
echo '<small class="pr-sm">' . $i_tax_name[0] . ' (' . $i_tax_name[1] . ' %)' . '</small>' . display_money($v_item->total_cost / 100 * $i_tax_name[1]) . ' <br>';
}
}
$total_cost += $v_item->total_cost;
$total_tax += $tax_amount;
?></td>
<td class="unit" style="text-align: right"><?= display_money($tax_amount) ?></td>
<td class="total" style="text-align: right"><?= display_money($v_item->total_cost) ?></td>
</tr>
<?php endforeach; ?>
<?php endif ?>
</tbody>
<tfoot>
<tr class="total">
<td colspan="3"></td>
<td><?= $language_info['total'] ?></td>
<td><?= display_money($total_tax) ?></td>
<td><?= display_money($total_cost) ?></td>
</tr>
</tfoot>
</table>
</div>
<?php } ?>
<footer>
<?= config_item('invoice_footer') ?>
</footer>

Related

Sort XML data by newest to oldest in PHP

I've got this code that works well now but thought it would be useful to have the Observed Data start from the newest date to the oldest. How can I flip only the Observed Data? Here is the code (Sorry if it doesn't look right on here, still getting use to posting on this site):
<?php
$url = "http://r7j8v4x4.map2.ssl.hwcdn.net/NOD_R.xml";
$xml = simplexml_load_file($url);
?>
<?php foreach ($xml->RESULTSET[0]->ROW as $MSG) :?>
<?php echo '<h4>', $MSG->MSG_TXT; '</h4>'; ?>
<?php endforeach; ?>
<!-- Table Style -->
<style>
table {border: 2px solid #fff;}
table td {height: 15px;}
table td {border: 1px solid #fff; }
table tr {border: 1px solid #fff; }
table td {padding: 3px; }
</style>
<h2>Observed Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:0px; font-weight:bold">Day</span></td>
<td><span style="margin:0px; font-weight:bold">Time(EST)</span></td>
<td><span style="margin:0px; font-weight:bold">Reservoir Elev.(behind dam)*</span</td>
<td><span style="margin:0px; font-weight:bold">Tailwater Elev.(below dam)*</span></td>
<td><span style="margin:0px; font-weight:bold">Avg Hourly Discharge* </span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[1]->ROW as $obs) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<h2>Predicted Data</h2>
<table>
<div style="overflow-x:auto;">
<thead>
<tr>
<td><span style="margin:25px; font-weight:bold">Day</span></td>
<td><span style="margin:5px; font-weight:bold">Average Inflow* </span</td>
<td><span style="margin:5px; font-weight:bold">Midnight Elevation*</span></td>
<td><span style="margin:5px; font-weight:bold">Average Outflow*</span></td>
</tr>
</thead>
<tbody>
<?php foreach ($xml->RESULTSET[2]->ROW as $pred) :?>
<tr>
<td><?php echo $pred->PREDICTED_DAY; ?></td>
<td><?php echo $pred->DAILY_AVG_INFLOW; ?></td>
<td><?php echo $pred->MIDNIGHT_ELEV; ?></td>
<td><?php echo $pred->DAILY_AVG_OUTFLOW; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>`
You can read them into arrays before you output.
foreach ($xml->RESULTSET[1]->ROW as $obs) {
$resultSet1[] = $obs;
}
foreach ($xml->RESULTSET[2]->ROW as $obs) {
$resultSet2[] = $obs;
}
That will make it easier for you to reverse them. Once they're in arrays there are various ways to do it. You can use array_reverse, a for loop with a decrementing index, or something like this:
<?php while ($obs = array_pop($resultSet1)) :?>
<tr>
<td><?php echo $obs->OBS_DAY; ?></td>
<td><?php echo $obs->OBS_HR; ?></td>
<td><?php echo $obs->UPSTREAM_ELEV; ?></td>
<td><?php echo $obs->DOWNSTREAM_ELEV; ?></td>
<td><?php echo $obs->AVG_HOURLY_DISCHARGE; ?></td>
</tr>
<?php endwhile; ?>

How to insert data by using td id

In my PHP code is below.
<table width="100%" >
<tr width="100%" class="bgcolor_02">
<th ROWSPAN="2" >SUBJECTS </th>
<th COLSPAN="2" >EXAMS </th>
<th COLSPAN="2" >PROJECTS </th>
</tr>
<tr class="bgcolor_02">
<th id="1E"> 1.EXAM</th>
<th id="2E"> 2.EXAM</th>
<th id="1P"> 1.PROJECT</th>
<th id="2P"> 2.PROJECT</th>
</tr>
<?php
$myexamresults = $db-> getrows (" SELECT * FROM `es_exam_result` WHERE `es_studentid` = '".$_SESSION['eschools']['user_id']."' ORDER BY `es_examorder` ASC ");
$pre_subjects=subjectnameByClass($es_subjectshortname);
foreach ($pre_subjects as $ndsdersliste ) {
$subjeid=$ndsdersliste['es_subjectid']; ?>
<tr > <td ><?php echo $ndsdersliste['es_subjectname']; ?></td>
<?php
foreach ($myexamresults as $ndsmyexam) {
$puan="";
if( $ndsdersliste['es_subjectname'] == $ndsmyexam['es_subjectname'] ) {
$examname = $ndsmyexam['es_examname'];
$puan = $ndsmyexam['es_puan'];
if ($examname =="1.Exam"){ ?> <td headers="1E" ><?php {echo $puan ;} ?> </td> <?php }
if ($examname =="2.Exam"){ ?> <td headers="2E" ><?php {echo $puan ;} ?> </td> <?php }
if ($examname =="1.Project"){ ?> <td headers="1P" ><?php {echo $puan ;} ?> </td> <?php }
if ($examname =="2.Project"){ ?> <td headers="2P" ><?php {echo $puan ;} ?> </td> <?php }
} } ?>
</tr>
<?php } ?>
</table>
I can get exam results from database. But I can not insert correct data under headers. If a student have just "1.Project" result, It is showing under "1.Exam". How to insert under correct title. Thanks..
You have to create empty <td> if there is no result to have the same number of <td> for each <tr>:
<tr > <td ><?php echo $ndsdersliste['es_subjectname']; ?></td>
<?php
$td = [];
foreach ($myexamresults as $ndsmyexam) {
$puan="";
if( $ndsdersliste['es_subjectname'] == $ndsmyexam['es_subjectname'] )
{
$examname = $ndsmyexam['es_examname'];
$puan = $ndsmyexam['es_puan'];
if ($examname =="1.Exam") { $td['1E'] = $puan ; }
if ($examname =="2.Exam") { $td['2E'] = $puan ; }
if ($examname =="1.Project") { $td['1P'] = $puan ; }
if ($examname =="2.Project") { $td['2P'] = $puan ; }
}
}
if (isset($td['1E'])) echo '<td headers="1E">'.$td['1E'] .'</td>' ; else echo '<td headers="1E"></td>' ;
if (isset($td['2E'])) echo '<td headers="2E">'.$td['2E'] .'</td>' ; else echo '<td headers="2E"></td>' ;
if (isset($td['1P'])) echo '<td headers="1P">'.$td['1P'] .'</td>' ; else echo '<td headers="1P"></td>' ;
if (isset($td['2P'])) echo '<td headers="2P">'.$td['2P'] .'</td>' ; else echo '<td headers="2P"></td>' ;
?>
</tr>

how to insert php cart data into mysql database

I have developed shopping cart in PHP but while inserting cart data into the database I got an error of undefined index. Can anybody please tell me how do I solve it? I have called all the three values using POST method and these all are also present in the database still i am getting this error.Why?
Code:
reviewcart.php
<?php
$servername = "localhost";
$username = "root";
$password = "";
$dbname = "midata";
// Create connection
$conn = new mysqli($servername, $username, $password, $dbname);
// Check connection
if ($conn->connect_error) {
die("Connection failed: " . $conn->connect_error);
}
if(isset($_POST['submit']))
{
$name=$_POST['name'];
$code=$_POST['code'];
$quantity=$_POST['quantity'];
$sql = "INSERT INTO order_final (name, code, quantity)
VALUES ('$name', '$code', '$quantity')";
if ($conn->query($sql) === TRUE) {
}
else{
}
}
else{ echo 'query doesnt execute';
}
?>
<div class="container">
<?php
session_start();
require_once("dbcontroller.php");
$db_handle = new DBController();
if(!empty($_GET["action"])) {
switch($_GET["action"]) {
case "remove":
if(!empty($_SESSION["cart_item"])) {
foreach($_SESSION["cart_item"] as $k => $v) {
if($_GET["code"] == $k)
unset($_SESSION["cart_item"][$k]);
if(empty($_SESSION["cart_item"]))
unset($_SESSION["cart_item"]);
}
}
break;
case "empty":
unset($_SESSION["cart_item"]);
break;
}
}
?>
<div class="c-layout-sidebar-content ">
<!-- BEGIN: PAGE CONTENT -->
<div class="c-content-title-1 c-margin-t-30">
<h3 class="c-font-uppercase c-font-bold c-center ">Place Order</h3>
<div class="c-content-panel">
<div class="c-body">
<div class="row">
<div class="col-md-12">
<table class="table table-condensed">
<div id="shopping-cart">
<div class="txt-heading">Shopping Cart <a id="btnEmpty" href="reviewcart.php?action=empty">Empty Cart</a></div>
<?php
if(isset($_SESSION["cart_item"])){
$item_total = 0;
?>
<table class="col-md-12" cellpadding="10" cellspacing="1" >
<tbody class="col-md-12">
<tr>
<form action="" method="post">
<th class="col-md-3" style="text-align:center;"><strong>Name</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Code</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Quantity</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Price</strong></th>
<th class="col-md-3" style="text-align:center;"><strong>Action</strong></th>
</tr>
<?php
foreach ($_SESSION["cart_item"] as $item){
?>
<tr>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><strong><?php $name=$_POST['name'] ; echo $item["name"]; ?></strong></td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $code=$_POST['code'] ; echo $item["code"]; ?></td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $quantity=$_POST['quantity'] ; echo $item["quantity"]; ?></td>
<!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;"><?php $price=$_POST['price'] ; echo $price ?></td> -->
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">Remove Item</td>
</tr>
<!-- <?php
$item_total += ($item["price"]*$item["quantity"]);
}
?>
-->
<tr>
<td colspan="5" align=right><input type="submit" name="submit" value="submit" /></td>
</tr></form>
</tbody>
</table>
<?php
}
?>
</div>
</table>
</div>
</div>
</div>
</div>
</div>
</div>
</div>
If we want to post any data via form mean we must want to use input tag then only we can accept it values on global arrays like $_POST, $_GET, $_FILES.
<tr>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<strong><?php echo $item["name"]; ?></strong>
<input type="hidden" name="name" value="<?php echo $item['name']?>">
</td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php echo $item["code"]; ?>
<input type="hidden" name="code" value="<?php echo $item['code']?>">
</td>
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php echo $item["quantity"]; ?>
<input type="hidden" name="quantity" value="<?php echo $item['quantity']?>">
</td>
<!-- <td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<?php $price=$_POST['price'] ; echo $price ?>
</td> -->
<td style="text-align:center;border-bottom:#F0F0F0 1px solid;">
<a href="reviewcart.php?action=remove&code=<?php echo $item["code"]; ?>" class="btnRemoveAction">
Remove Item
</a>
</td>
</tr>

No error but no ITEM is showing out

Hello this is the code in my php, i dont get error but the items in the database doesn't show and only the else tag is execute.
<?php
$sql = $bdd->prepare('SELECT * FROM bgk9z_items');
if ($sql->fetchColumn() > 0) { //
while ($result = $sql->fetch(PDO::FETCH_ASSOC)){
?>
<tr>
<th id="checkbox"><input type="checkbox" name="toggle" /></th>
<td id="titre"><? echo $result['title']; ?>
<div class="opt"><span class="up">Modifier</span> <span class="del">Supprimer</span> <span class="sho">Afficher</span></div>
</td>
<td id="extrait"><? $string = $result['introtext']; $long = (strlen($string) > 13) ? substr($string,0,280).'...' : $string; echo $long;?></td>
<td id="cat"><? include_once(dirname(__DIR__) . '/controlers/show-cat-name.php'); ?></td>
<td id="etat"><? $rep = $result['published']; if ($rep == 1) { echo "Oui";} else{echo "Non";} ?></td>
<td id="image"><? echo "Image"; ?></td>
<td id="ident"><? echo $result['id']; ?></td>
</tr>
<?
}
}
else { ?>
<tr><td colspan="7" style="text-align: center; color: rgb(192, 0, 0); font-size: 23px; font-weight: bold;">NO ITEM TO SHOW</td></tr>
<?php }
?

How to create HTML table using loop which display Mysql data using php?

I am new in php. I want to create a table using loop. I have a select query and i want that the result of that query is display in table. i have multiple columns in my table.
<?php
$con=mysqli_connect("localhost","root","","pacra1");
// $id2 = $_GET['id'];
$sql = "SELECT pc.id, l.id, l.to_name, l.to_designation, l.company, l.address, l.confidential, l.date, l.rating_type_title, l.opinion_type, l.dear_sir, l.company_id, l.first_opinion_name, l.first_opinion_rating_type, l.first_opinion_action, l.first_opinion_outlook, l.first_opinion_long_term, l.first_opinion_p_long_term, l.first_opinion_short_term, l.first_opinion_p_short_term, l.second_opinion_name, l.second_opinion_rating_type, l.second_opinion_action, l.second_opinion_outlook, l.second_opinion_long_term, l.second_opinion_p_long_term, l.second_opinion_short_term, l.second_opinion_p_short_term, l.third_opinion_name, l.third_opinion_rating_type, l.third_opinion_action, l.third_opinion_outlook, l.third_opinion_long_term, l.third_opinion_p_long_term, l.third_opinion_short_term, l.third_opinion_p_short_term, l.forth_opinion_name, l.forth_opinion_rating_type, l.forth_opinion_action, l.forth_opinion_outlook, l.forth_opinion_long_term, l.forth_opinion_p_long_term, l.forth_opinion_short_term, l.forth_opinion_p_short_term, l.y_truly, l.s_name, l.uh1, l.uh2, l.uh1_designation, l.uh2_designation, l.s_designation, l.chk, l.chk1
FROM letter l
LEFT JOIN pacra_clients pc
ON l.company_id = pc.id
ORDER BY l.id DESC
LIMIT 1";
$result=mysqli_query($con,$sql);
$row= (mysqli_fetch_array($result,MYSQLI_ASSOC));
$id= $row['id'];
$to_name= $row['to_name'];
$to_designation= $row['to_designation'];
$company= $row['company'];
$address= $row['address'];
$confidential = $row['confidential'];
$date = $row['date'];
$phpdate = strtotime( $date );
$date = date( 'M d, Y', $phpdate );
$rating_type_title= $row['rating_type_title'];
$opinion_type= $row['opinion_type'];
$dear_sir= $row['dear_sir'];
$company_id= $row['company_id'];
$first_opinion_name= $row['first_opinion_name'];
$first_opinion_rating_type= $row['first_opinion_rating_type'];
$first_opinion_action= $row['first_opinion_action'];
$first_opinion_outlook= $row['first_opinion_outlook'];
$first_opinion_long_term= $row['first_opinion_long_term'];
$first_opinion_p_long_term= $row['first_opinion_p_long_term'];
$first_opinion_short_term= $row['first_opinion_short_term'];
$first_opinion_p_short_term= $row['first_opinion_p_short_term'];
$second_opinion_name= $row['second_opinion_name'];
$second_opinion_rating_type= $row['second_opinion_rating_type'];
$second_opinion_action= $row['second_opinion_action'];
$second_opinion_outlook= $row['second_opinion_outlook'];
$second_opinion_long_term= $row['second_opinion_long_term'];
$second_opinion_p_long_term= $row['second_opinion_p_long_term'];
$second_opinion_short_term= $row['second_opinion_short_term'];
$second_opinion_p_short_term= $row['second_opinion_p_short_term'];
$third_opinion_name= $row['third_opinion_name'];
$third_opinion_rating_type= $row['third_opinion_rating_type'];
$third_opinion_action= $row['third_opinion_action'];
$third_opinion_outlook= $row['third_opinion_outlook'];
$third_opinion_long_term= $row['third_opinion_long_term'];
$third_opinion_p_long_term= $row['third_opinion_p_long_term'];
$third_opinion_short_term= $row['third_opinion_short_term'];
$third_opinion_p_short_term= $row['third_opinion_p_short_term'];
$forth_opinion_name= $row['forth_opinion_name'];
$forth_opinion_rating_type= $row['forth_opinion_rating_type'];
$forth_opinion_action= $row['forth_opinion_action'];
$forth_opinion_outlook= $row['forth_opinion_outlook'];
$forth_opinion_long_term= $row['forth_opinion_long_term'];
$forth_opinion_p_long_term= $row['forth_opinion_p_long_term'];
$forth_opinion_short_term= $row['forth_opinion_short_term'];
$forth_opinion_p_short_term= $row['forth_opinion_p_short_term'];
$y_truly= $row['y_truly'];
$s_name= $row['s_name'];
$s_designation= $row['s_designation'];
$chk = $row['chk'];
$chk1 = $row['chk1'];
$uh1 = $row['uh1'];
$uh2 = $row['uh2'];
$uh1_designation = $row['uh1_designation'];
$uh2_designation = $row['uh2_designation'];
if ($s_designation == "coo"){
$s_designation ="Chief Operating Officer";
}
elseif ($s_designation == "uh") {
$s_designation ="Unit Head";
}
?>
And HTML
<div style=" font-family: 'Times New Roman', Times, serif;">
<div style=" margin:auto; width:60px; height:auto; align: middle">
<img src="image/pacra_logo.png" alt="logo">
</div>
<div style="margin:auto; width:auto; text-align:center; font-family:'Times New Roman', Times, serif; font-variant: small-caps; font-size:20px; font-weight:bold">
The Pakistan Credit Rating Agency Limited
<hr>
</div>
</div>
<div style="margin-top: 7px; width: auto; font-family:'Times New Roman', Times, serif; float:left">NL FY <?php echo date('y') ?> - <?php echo $id ?>
</div>
<div style="clear: both;"></div>
<div style=" margin-top:20px; width:250px; float:left; font-family:'Times New Roman', Times, serif; text-align:left; font-size:14px; l">
<b> <?php echo $to_name?> </b> <br /><?php echo $to_designation?> </br> <?php echo $company?> </br> <?php echo $address ?>
</div>
<div style=" margin-top:20px; margin-left:300px; width:auto; float:left; font-family:'Times New Roman', Times, serif; font-size:14px;">
<b> <u> <?php echo $confidential ?> </u> </b> </br> <?php echo $date ?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:20px; margin-right:auto; width:auto; font-family:'Times New Roman', Times, serif; text-align:center; font-variant:small-caps; font-size:18px; font-weight:bold; color:#009"> <?php echo $company?> <br /> Ratings - <?php
if ($opinion_type == "up") {
echo "Update";}
elseif ($opinion_type =="in") {
echo "Initial";}
?>
</div>
<div style="margin-top:10px; width:auto; float:left; font-family:'Times New Roman', Times, serif">
<?php echo $dear_sir?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:10px; width:auto; float:left; font-family:'Times New Roman', Times, serif; text-align:justify">
<?php
if ($opinion_type == "up") {
echo "This is with the reference to"; echo ' '; echo "ratings of"; echo ' '; echo $company ;
echo ". PACRA has updated its opinions, following are the respective details.";
} elseif ($opinion_type == "in") {
echo "This is with the refrence to "; echo $rating_type_title; echo " ratings of "; echo $company ; echo ". PACRA assign its opinions, following are the rspective detail";
}
?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:20px; width:auto; align:middle; font-family:'Times New Roman', Times, serif">
<!--<table width="100%">
<tr> <td colspan="03"> <b> Name:</b> </td>
<td width="429"><b> <?php //echo $company?></b> </td>
</tr>
</table> -->
</div>
<div style="margin-top:40px; width:auto;font-family:'Times New Roman', Times, serif; text-align:left; font-size:12px; text-align:center">
<table width="657">
<tr>
<td width="225"> <strong>Opinion</strong></td>
<td width="62"> <strong>Action</strong></td>
<td colspan="4"><strong>Ratings</strong></td>
<td width="54"><strong>Outlook</strong></td>
<td width="67"><strong>Rating Type</strong></td>
</tr>
<tr>
<td width="225"> </td>
<td width="62"> </td>
<td colspan="2"><b>Long Term</b></td>
<td colspan="2"><b>Short Term</b></td>
<td width="54"> </td>
<td width="67"> </td>
</tr>
<tr>
<td width="225"> </td>
<td width="62"> </td>
<td width="52"><b>Current</b></td>
<td width="45"><b>Previous</b></td>
<td width="49"><b>Current</b></td>
<td width="51"><b>Previous</b></td>
<td width="54"> </td>
<td width="67"> </td>
</tr>
<tr>
<td><?php echo $first_opinion_name?></td>
<td><?php echo $first_opinion_action ?></td>
<td><?php echo $first_opinion_long_term ?></td>
<td><?php echo $first_opinion_p_long_term?></td>
<td><?php echo $first_opinion_short_term ?></td>
<td><?php echo $first_opinion_p_short_term ?></td>
<td><?php echo $first_opinion_outlook ?></td>
<td><?php echo $first_opinion_rating_type ?></td>
</tr>
<tr>
<td><?php echo $second_opinion_name?></td>
<td><?php echo $second_opinion_action ?></td>
<td><?php echo $second_opinion_long_term ?></td>
<td><?php echo $second_opinion_p_long_term?></td>
<td><?php echo $second_opinion_short_term ?></td>
<td><?php echo $second_opinion_p_short_term ?></td>
<td><?php echo $second_opinion_outlook ?></td>
<td><?php echo $second_opinion_rating_type ?></td>
</tr>
<tr>
<td><?php echo $third_opinion_name?></td>
<td><?php echo $third_opinion_action ?></td>
<td><?php echo $third_opinion_long_term ?></td>
<td><?php echo $third_opinion_p_long_term?></td>
<td><?php echo $third_opinion_short_term ?></td>
<td><?php echo $third_opinion_p_short_term ?></td>
<td><?php echo $third_opinion_outlook ?></td>
<td><?php echo $third_opinion_rating_type ?></td>
</tr>
<tr>
<td><?php echo $forth_opinion_name?></td>
<td><?php echo $forth_opinion_action ?></td>
<td><?php echo $forth_opinion_long_term ?></td>
<td><?php echo $forth_opinion_p_long_term?></td>
<td><?php echo $forth_opinion_short_term ?></td>
<td><?php echo $forth_opinion_p_short_term ?></td>
<td><?php echo $forth_opinion_outlook ?></td>
<td><?php echo $forth_opinion_rating_type ?></td>
</tr>
</table>
</div>
<div style="clear: both;"></div>
<div style="margin-top:50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif">
<?php echo $y_truly ?>
</div>
<div style="clear: both;"></div>
<div style="margin-top:70px; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
<?php
if ($s_name == "shahzad"){
echo '<b>'; echo "(Muhammad Shahzad Saleem)"; echo '</b>' ;
echo '<br/>';
echo $s_designation;
}
?>
</div>
<div style="margin-top:50px; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
<?php
if ($s_name == "hanif") {
echo '<b>'; echo "(Muhammad Jhangeer Hanif)"; echo '</b>' ;
echo '<br/>';
echo $s_designation;
}
elseif ($s_name == "rana") {
echo '<b>'; echo "(Rana Muhammad Nadeem)"; echo '</b>' ;
echo '<br/>';
echo $s_designation;
}
?>
</div>
<div style="margin-top:50px; text-align:left; font-family:'Times New Roman', Times, serif; float:left; margin-left:220px">
<?php
if ($s_name == "rana") {
echo '<b>'; echo "(Muhammad Jhangeer Hanif)"; echo '</b>' ;
echo '<br/>';
echo $s_designation;
}
elseif ($s_name == "hanif") {
echo '<b>'; echo "(Rana Muhammad Nadeem)"; echo '</b>' ;
echo '<br/>';
echo $s_designation;
}
?>
</div>
<div style="margin-top:50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif; float:left">
<?php if ($s_designation == "coo"){
echo "Chief Operating Officer";
}
elseif ($s_designation == "uh") {
echo "Unit Head";
}
?>
</div>
<div style="margin-top:auto; width:auto; text-align:left; font-family:'Times New Roman', Times, serif; float:left; margin-left:362px">
<?php if ($s_designation == "uh") {
echo "Unit Head";
}
?>
</div>
<div style="clear: both;"></div>
<div style="margin-top: 50px; width:auto; text-align:left; font-family:'Times New Roman', Times, serif">
<?php if ($chk == "p_release"){
echo "Encl: 1) Press Release";
}
echo '</br>';
if ($chk1 == "r_report"){
echo " "; echo "Rating Report";
}
?>
</div>
Why i want this?
Coz i display my record in table through my db. some times there is record only for two rows of table. I statically create four rows of table. If there is two row record then my reaming two rows will be shown empty. I want to create my table according to data in my db.
I have no idea how i can do it?
Can you gys please help me?
use foreach loop after mysql fetch query.
<?php
foreach($row as $rows)
{
$id= $rows['id'];
$to_name= $rows['to_name'];
$to_designation= $rows['to_designation'];
$company= $rows['company'];
$address= $rows['address'];
$confidential = $rows['confidential'];
$date = $rows['date'];
$phpdate = strtotime( $date );
$date = date( 'M d, Y', $phpdate );
$rating_type_title= $rows['rating_type_title'];
$opinion_type= $rows['opinion_type'];
$dear_sir= $rows['dear_sir'];
$company_id= $rows['company_id'];
$first_opinion_name= $rows['first_opinion_name'];
$first_opinion_rating_type= $rows['first_opinion_rating_type'];
$first_opinion_action= $rows['first_opinion_action'];
$first_opinion_outlook= $rows['first_opinion_outlook'];
$first_opinion_long_term= $rows['first_opinion_long_term'];
$first_opinion_p_long_term= $rows['first_opinion_p_long_term'];
$first_opinion_short_term= $rows['first_opinion_short_term'];
$first_opinion_p_short_term= $row['first_opinion_p_short_term'];
}
?>

Categories