I have table Province District and Subdistrict in my database and i want to print as output in html table as below:
but i get this:
here are my code:
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
foreach ($QueryDistricts as $QueryDistrict ) {
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
<?php
}
?>
</tr>
<?php
}
?>
Please help me,
Thanks a lot.
You need to close and then open a new row with each loop of foreach ($QueryDistricts as $QueryDistrict ). Try something like this -
<?php
$orderProv = 1;
//$QueryProvinces = Query Province;
foreach ($QueryProvinces as $QueryProvince) {
?>
<tr>
<td> <?php echo $orderProv; ?></td>
<td> <?php echo $QueryProvince->nameProv; ?></td>
<?php
//$QueryDistricts = Query District;
$i = 0; // simple counter
foreach ($QueryDistricts as $QueryDistrict ) {
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td> </td>
<td> </td>
<?php
}
?>
<td> <?php echo $QueryDistrict ->nameDist; ?></td>
</tr>
<?php
$i++;
}
}
?>
Table Result:
html source code:
Complete code:
<table id="example1" class="table table-bordered table-condensed table-hover">
<thead>
<tr>
<th>No.</th>
<th width="20%">Provinsi</th>
<th>Kabupaten/Kotamadya</th>
<th>Kecamatan</th>
<th>Pendamping</th>
<th>Telepon</th>
<th>Email</th>
<th>Status Laporan</th>
</tr>
</thead>
<tbody>
<?php
$thecolor = array ("olive", "navy", "aqua", "maroon", "green", "yellow", "orange", "purple", "light-blue", "red");
//$infLokasis = Query;
$nomorProv = 1;
$thecolorProv = $thecolor;
$colorProv = 0;
foreach ($infLokasis as $infLokasi) {
# code... cetak nama provinsi
if ($colorProv >= 10) {
# code... warna set
$colorProv = 0;
}
?>
<tr>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $nomorProv.". ";?></strong></td>
<td><strong class="text-<?php echo $thecolorProv[$colorProv];?> small"><?php echo $infLokasi->lokasi_nama;?></strong></td>
<?php
$i = 0; // simple counter
//$infKabkotas = Query;
$nomorKabkota = 1;
$thecolorKabkota = $thecolor;
$colorKabkota = 0;
foreach ($infKabkotas as $infKabkota) {
# code... cetak nama kabupaten
if ($colorKabkota >= 10) {
# code... warna set
$colorKabkota = 0;
}
if($i>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKabkota[$colorKabkota];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$infKabkota->lokasi_nama; ?></span></td>
<?php
$j = 0;
$infKecamatans = Query;
$nomorKecamatan = 1;
$thecolorKecamatan = $thecolor;
$colorKecamatan = 0;
foreach ($infKecamatans as $infKecamatan) {
if ($colorKecamatan >= 10) {
# code... warna set
$colorKecamatan = 0;
}
if($j>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td><strong class="text-<?php echo $thecolorKecamatan[$colorKecamatan];?> small"><?php echo $nomorProv.".".$nomorKabkota.". ".$nomorKecamatan.". ".$infKecamatan->lokasi_nama; ?></strong></td>
<?php
$k = 0;
$idDaerah = substr($infKecamatan->lokasi_kode, 0,10);
$infPendampings = $db->get_results("SELECT id_p, nama_p, telepon_p, email_p FROM pendamping WHERE id_d LIKE '$idDaerah%' ");
$nomorPendamping = 1;
$thecolorPendamping = $thecolor;
$colorPendamping = 0;
if ($infPendampings) {
foreach ($infPendampings as $infPendamping) {
if($k>0){ // if not the 1st row, echo new row start
?>
<tr>
<td></td>
<td></td>
<td></td>
<td></td>
<?php
}
?>
<td>
<?php echo "<span class=\"text-".$thecolorPendamping[$colorPendamping]." small\">".$nomorPendamping.". "; ?></span>
<?php echo "<a href=\"vpendamping.php?str=".encryptor('encrypt', $infPendamping->id_p)."\" class=\"text-".$thecolorPendamping[$colorPendamping]." small\" target=\"_blank\">".namaGelar($infPendamping->nama_p); ?>
</td>
<td class="small"><?php echo $infPendamping->telepon_p; ?></td>
<td class="small"><?php echo $infPendamping->email_p; ?></td>
<td class="small"><?php echo "<i class=\"fa fa-fw fa-square-o text-red\"></i> Kosong"; ?></td>
<?php
$nomorPendamping++;
$colorPendamping++;
$k++;
}
}
else {
# code... row kosong
#if i romove 4 line code below... Provinsi name and Kabupaten/Kotamadya name should appear
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
echo "<td></td>\n";
}
?>
</tr>
<?php
$nomorKecamatan++;
$colorKecamatan++;
$j++;
}
?>
<?php
$i++;
$colorKabkota++;
$nomorKabkota++;
} // End infKabkota
$nomorProv++;
$colorProv++;
}
?>
</tbody>
</table>
Related
I have two tables meals type and meals. I want to print each meals corresponding to its meals type.
Error is that only showing last meals type meals only
view page is like
View
<?php
foreach ($mealstype as $type) {
?>
<h2><?php echo $type->type; ?></h2>
<table class="table table-bordered">
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
foreach ($meals as $meals_get) {
?>
<tr>
<td><?php echo $meals_get->item; ?></td>
<td><?php echo $meals_get->price; ?></td>
</tr>
<?php } ?>
<tr>
<td> <input type="checkbox" class="ck" value="total"> Toal</td>
<td>2050</td>
</tr>
</tbody>
</table>
<?php
}
?>
Controller
function availability() {
$this->data['mealstype'] = $this->Home_model->mealstype();
foreach ($this->data['mealstype'] as $type) {
$typeid = $type->id;
$meals[] = $this->Home_model->meals($typeid, $hotel_id);
}
$this->data['meals'] = $meals[];
$this->load->view('home2', $this->data);
}
Please check this answer
Controller:
public function availability() {
$build_array = array();
mealstype= $this->Home_model->mealstype();
foreach($mealstype as $row){
$build_array[] = array(
'parent_array' => $row,
'child_array' =>$this->Home_model->meals($row->id,$hotel_id),
'child_sum' =>$this->Home_model->meals_sum($row->id,$hotel_id)
);
}
$this->data['build_array'] = $build_array;
}
and the view page is like this:
View:
<?php foreach($build_array as $type){?>
<h2><?php echo $type['parent_array']->type;?></h2>
<table class="table table-bordered">
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php
foreach ($type["child_array"] as $meals_get) {?>
<tr>
<td><?php echo $meals_get->item;?></td>
<td><?php echo $meals_get->price;?></td>
</tr>
<?php }?>
<tr>
<td> Toal</td>
<td>2050</td>
</tr>
<?php } ?>
Controller, check "hotel_id"
function availability()
{
$this->data['mealstype'] = $this->Home_model->mealstype();
if( count( $this->data['mealstype'] ) > 0 )
{
foreach($this->data['mealstype'] as $type)
{
$type->meals = $this->Home_model->meals( $type->id, $hotel_id ); // hotel_id no declared???
}
}
$this->load->view('home2', $this->data);
}
View
<?php if( count( $mealstype ) > 0): foreach( $mealstype as $type ): ?>
<h2><?php echo $type->type; ?></h2>
<table class="table table-bordered">
<thead>
<tr>
<th>Meals</th>
<th>Price</th>
</tr>
</thead>
<tbody>
<?php if( count( $type->meals ) > 0 ): foreach( $type->meals as $meals_get ): ?>
<tr>
<td><?php echo $meals_get->item; ?></td>
<td><?php echo $meals_get->price; ?></td>
</tr>
<?php endforeach; endif; ?>
<tr>
<td><input type="checkbox" class="ck" value="total"> Toal</td>
<td>2050</td>
</tr>
</tbody>
</table>
<?php endforeach; endif ?>
Try to replace into this :
function availability() {
$data = array();
$data['mealstype'] = $this->Home_model->mealstype();
foreach ($data['mealstype'] as $type) {
$typeid = $type->id;
$type->meals = $this->Home_model->meals($typeid,$hotel_id);
}
$this->load->view('home2', $data);
}
check it and print_r($data); what it comes set on the basis of that in view file..
You have $hotel_id I am not sure where you set this I can not see it any where else.
We also need to see your model
public function availability() {
$data['mealstypes'] = array();
$mealstypes = $this->home_model->mealstype();
foreach ($mealstypes as $mealstype)
{
$meals = array();
$meals_results = $this->home_model->meals($mealstype->id, $hotel_id);
foreach ($meals_results $meal_result) {
$meals[] = array(
'mealstype' => $meal_result->mealstype,
'price' => $meal_result->price
);
}
$data['mealstypes'][] = array(
'type' => $mealstype->type
'meals' => $meals
);
}
$this->load->view('someview', $data);
}
View
<?php foreach ($mealstypes as $mealstype) {?>
<h2><?php echo $mealstype['type'];?></h2>
<?php foreach ($mealstype['meals'] as $meal) {?>
<?php echo $meal['mealstype'];?>
<?php echo $meal['price'];?>
<?php }?>
<?php }?>
I was trying to get month and year from database when I click on the specific month from select option it should display month and as well as year, the result should fetch from database.
below is my code
<?php
$mon_arr=array("Jan"=>"01","Feb"=>"02","Mar"=>"03","Apr"=>"04","May"=>"05","Jun"=>"06","Jul"=>"07","Aug"=>"08","Sep"=>"09","Oct"=>"10","Nov"=>"11","Dec"=>"12");
?>
<select name="month" id="monthr">
<?php foreach($mon_arr as $x=>$xval){ ?>
<option value="<?php echo $xval; ?>" <?php if($m == $xval) echo "selected=selected"; ?>><?php echo $x; ?></option>
<?php } ?>
</select>
<?php //echo $xval; ?>
<?php //echo $m; ?>
<?php //echo $x; ?>
<select name="year" id="yearr">
<?php for($yi=2014;$yi<=2025;$yi++){ ?>
<option value="<?php echo $yi; ?>" <?php if($y == $yi) echo "selected=selected"; ?>><?php echo $yi; ?></option>
<?php } ?>
</select>
</div>
<table class="table">
<thead>
<tr>
<th>#</th>
<th>Description</th>
<th>File</th>
<th>Submitted By</th>
<th>Date</th>
<th></th>
</tr>
</thead>
<tbody>
<?php $sel_month=mysql_query("SELECT * FROM ".TABLE_MONTHLY." WHERE pid='".$_GET['pid']."' ORDER BY m_date DESC ");
$f=new all_calls();
while($res_month=mysql_fetch_array($sel_month))
{
?>
<tr>
<td></td>
<td><?php echo $res_month['m_desc']; ?></td>
<td><?php $pathsi=explode(',',$res_month['m_file']);
for($ji=1;$ji<count($pathsi);$ji++) {
?>
Download<br />
<?php
}
?></td>
<td><?php echo $f->get_username($res_month['m_by']); ?></td>
<td><?php echo $res_month['m_date']; ?></td>
<td><i class="fa fa-pencil"></i></td>
</tr>
<?php } ?>
</tbody>
</table>
<?php
class monthly_report{
function validate_and_insert($data,$mid,$mfile)
{
//var_dump($mid); die;
if(empty($data['mdesc'])) $err='Description Required';
if(empty($err))
{
//echo 'hello'; die;
//var_dump($mid); die;
if(strlen($mid)==0)
{
$insert=mysql_query("INSERT INTO ".TABLE_MONTHLY." SET pid='".$_POST['hide_pid']."', m_desc='".$data['mdesc']."', m_file='".$mfile."', m_by='".$_SESSION['ID']."', m_date=CURDATE()");
if($insert)
{
return 0;
}
}
else
{
$a=new all_calls();
$paths=$a->get_monthly_file($mid);
if(strlen($mfile)==0)
{
$newfile=$paths;
}
else
{
$newfile=$paths.','.$mfile;
}
//var_dump($mid);die;
$update=mysql_query("UPDATE ".TABLE_MONTHLY." SET m_desc='".$data['mdesc']."', m_file='".$newfile."' WHERE mid='".$mid."' ");
if($update)
{
return 0;
}
}
}
}
}
?>
Can anyone help me out with this issue.
I try to display 2 table side by side with php code....
but i need to display only 6 table on single page ......and rest on another page......
so plz can any one help me to break page or break loop after 6th iteration..... 7th table display on another page like wise...
plz see my image attached below....i am facing problem on print preview...
page breaks my table during print...like below image...
I attached croped imaged here...
my page actuaally display 8 tables on single page...but i need is only 6 on one page.
below is my code..
<?php if (is_array($data)) { foreach($data as $row) { ?>
<table border="1px solid #666" summary="" width="48%" class="pos_fixed1">
<thead>
<tr>
<td colspan="4">Dainik Bhaskar Nagpur</td>
</tr>
<tr>
<td>Receipt</td>
<td><?php echo htmlspecialchars($row['receipt_no']); ?></td>
<td>Coupon</td>
<td><?php echo htmlspecialchars($row['coupon']); ?></td>
</tr>
<tr>
<td>Receipt Date</td>
<td><?php echo htmlspecialchars($row['bookingdate']); ?></td>
<td>Coupon Date</td>
<td><?php echo htmlspecialchars($row['coupondate']); ?></td>
</tr>
<tr>
<td>Copy Start Date</td>
<td><?php echo htmlspecialchars($row['startingdate']); ?></td>
<td>HawkerName</td>
<td><?php echo htmlspecialchars($row['hawkername']); ?></td>
</tr>
<tr>
<td>SubagentName</td>
<td><?php echo htmlspecialchars($row['subagentname']); ?></td>
<td>CenterName</td>
<td><?php echo htmlspecialchars($row['ward']); ?></td>
</tr>
<tr>
<td>customer</td>
<td><?php echo htmlspecialchars($row['customer_name']); ?></td>
<td>Address</td>
<td><?php echo htmlspecialchars($row['society']); ?></td>
</tr>
</thead>
</table>
<?php } }?>
Try using CSS:
<style>
#media print {
.pageBreak {
page-break-after: always;
}
}
</style>
And at every 6 table, add a pageBreak:
<?php
$lineCounter = 0;
if (is_array($data)) {
foreach($data as $row) {
$lineCounter++;
?>
<!-- output a table... -->
<?php
if($lineCounter % 6 == 0) {
echo '<span class="pageBreak"></span>' . PHP_EOL;
}
}
}
?>
<?php
// get total records
TotalNoOfRecords = count($data);
$Count = 1;
foreach($data as $row) {
// write here your content
// break page after 6 rows
if($Count % 6 == 0 && $Count != $TotalNoOfRecords)
{
echo "<p style='page-break-after:always'></p>";
}
$Count++;
}
?>
try this is code or visit link
<?php
$q = "SELECT * FROM your_table ";
$myq = mysqli_query($link, $q);
$fixtures ='';
$i=0;
while($row=mysqli_fetch_assoc($myq)) {
$r[]=$row;
}
foreach ($r as $val) {
$i++;
?>
<!-- your value from database -->
<table>
<tr>
<td><?php echo $val['your_column']; ?></td>
</tr>
</table>
<!-- your value from database -->
<?php
if($i % 6==0){
echo '<div style="page-break-after: always;">[------ break ------]</div>' . PHP_EOL;
$i=0;
}
}
?>
I want to prevent amaun_caj , amaun_pelbagai , amaun_penalti , amaun_tunggakan from being repeated so if the id_akaun is the same the data will not appear, my code only works on amaun_caj, and for the rest, not a single data appear, what's the problem?
<?php
$i = 0; $id_akaun_old ="";
while($output = mysql_fetch_array($result)) {
$i++;
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td>
<?php echo $i; ?>
</td>
<td>
<?php echo $jenis; ?>
</td>
<td>
<?php echo $id_akaun;
?>
</td>
<td>
<?php echo $no_telefon; ?>
</td>
<td>
<?php echo $lokasi; ?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_caj;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_pelbagai;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_penalti;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $amaun_tunggakan;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<td align="center">
<?php
if($id_akaun != $id_akaun_old):
echo $jumlah_bayaran;
$id_akaun_old = $id_akaun;
else: echo '';
endif;?>
</td>
<?php
}
?>
Using temporary variable $old_id_akaun might not be the best practice. If you still want to do it like that, i suggest you user ORDER BY id_akaun in your SQL syntax.
In my oppinion, you might get rid of temporary variable and follow these steps,
1. Create empty array outside your while loop. For the sake of easy
understanding, let's called it $list_id_akaun.
2. Inside your while loop, after you get $id_akaun, check whether $id_akaun
is inside $list_id_akaun
3. If not exists, insert it to $list_id_akaun and continue echoing your
table row
4. If exists, skip to the next row.
I don't know why you don't want to use a select distinct clause in this case, but just put them inside a container then check inside the loop:
<?php
$temp = array();
$i = 1;
while($output = mysql_fetch_array($result)):
$id_akaun = $output["id_akaun"];
$lokasi = $output["lokasi"];
$amaun_caj = $output["amaun_caj"];
$amaun_tunggakan = $output["amaun_tunggakan"];
$amaun_penalti = $output["amaun_penalti"];
$amaun_pelbagai = $output["amaun_pelbagai"];
$jumlah_bayaran = $output["jumlah_bayaran"];
?>
<tr>
<td><?php echo $i; $i++; ?></td>
<td><?php echo $jenis; ?></td>
<td><?php echo $id_akaun; ?></td>
<td><?php echo $no_telefon; ?></td>
<td><?php echo $lokasi; ?></td>
<?php if(!in_array($id_akaun, $temp)): ?>
<td><?php echo $amaun_penalti; ?></td>
<td><?php echo $amaun_tunggakan; ?></td>
<td><?php echo $jumlah_bayaran; ?></td>
<?php $temp[] = $id_akaun; ?>
<?php else : ?>
<td></td><td></td><td></td>
<?php endif; ?>
</tr>
<?php endhile; ?>
Here is a code.
This loads all the header part (i.e the header for the table) dynamically from the database.
The below code works fine. But the column is mismatched.
i.e. the first row first column of the header is blank and there is a dislocation in the table.
Code
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
$columns = array_keys($row);
?>
<th>
<?php
foreach($columns as $column)
{
?>
<td><?php echo $column; ?> </td>
</th>
<?php
}
?>
<tr>
<?php
foreach($row as $key=>$value)
{
?>
<td><?php echo $value; ?></td>
<?php
}
?>
</tr>
<?php
$i++;
}
?>
</table>
EDIT:
Here is my print_r($columns) value:
Array ( [0] => Author Name [1] => Book Name [2] => Rating [3] => Location )
I know the problem is with the loop. Could someone help me out?
You can try to change
<th>
<?php
foreach($columns as $column)
{ ?>
<td><?php echo $column; ?> </td>
<?php
}
?>
</th>
to
<tr>
<?php
foreach($columns as $column)
{ ?>
<th><?php echo $column; ?> </th>
<?php
}
?>
</tr>
Just remove TH tag because its create one extra cell, so your layout messed up
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
$i = 0;
while($row = mysql_fetch_assoc($book_query))
{
if($i == 0){
$columns = array_keys($row);
?>
<?php
foreach($columns as $column){ ?>
<td><?php echo $column; ?> </td>
<?php } ?>
<?php } ?>
<tr>
<?php
foreach($row as $key=>$value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
<?php
$i++;
}
?>
</table>
Hope this will help someone.
Just I have replaced the TH tag with TR and the output is perfect.
<table border="1">
<?php
$book_query = mysql_query("select * from book_master");
while($row = mysql_fetch_assoc($book_query))
{
$columns = array_keys($row);
?>
<tr>
<?php
foreach($columns as $column){ ?>
<td><?php echo $column; ?> </td>
<?php } ?>
</tr>
<tr>
<?php
foreach($row as $key=>$value){ ?>
<td><?php echo $value; ?></td>
<?php } ?>
</tr>
</table>