Foreach inside foreach - php

I am trying to make a user profile page system, but when I have a while, or foreach inside of a while, or foreach it doesn't execute any of the code inside, and after the loop.
foreach($db->query("SELECT * FROM `members` WHERE username = '$user'") as $row) {
echo "
<div class='page-header'>
<div class='row'>
<div class='span12'>
<div class='tabbable'>
<ul class='nav nav-tabs'>
<li class='active'>
<a data-toggle='tab' href='#about'>About $user</a>
</li>
<li>
<a data-toggle='tab' href='#infractions'>Infractions</a>
</li>
</ul>
<div class='tab-content' style='overflow: visible;'>
<div class='tab-pane active' id='about'>
<div class='row'>
<div class='span9'>
<div class='span4' style='float:left; width:25%'>
".$row['reputation']."
</div>
</div>
</div>
<div class='tab-pane' id='infractions'>
<div class='row'>
<div class='span12'>
<br>
<h3>Received Infractions</h3>
<table class='table table-bordered table-striped'>
<thead>
<tr>
<th width='13%'>Issuer</th>
<th width='13%'>Punished</th>
<th width='34%'>Reason</th>
<th width='7%'>Type</th>
<th width='7%'>Status</th>
<th width='9%'>Expires</th>
<th width='12%'>Date</th>
</tr>
</thead>
<tbody>
<tr class=''>";
//foreach($db->query("SELECT * FROM `infractions` WHERE reciver = '$user'") as $infraction) {
$stmt = $db->query("SELECT * FROM `infractions` WHERE reciver = '$user'");
while($infraction = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<td><a href='/profiles/".$infraction['issuer']."' style='color: #F55'>".$infraction['issuer']."</a></td>
<td><a href='/profiles/".$infraction['reciver']."'>".$infraction['reciver']."</a></td>
<td>".$infraction['reason']."</td>
<td style='color:";
if($infraction['type']=="ban"){echo "red";}
elseif($infraction['type']=="tempban"){echo "orange";}
elseif($infraction['type']=="kick"){echo "yellow";}
elseif($infraction['type']=="mute"){echo "blue";}
echo "'>";
if($infraction['type']=="ban"){echo "Ban";}
elseif($infraction['type']=="tempban"){echo "Tempban";}
elseif($infraction['type']=="kick"){echo "Kick";}
elseif($infraction['type']=="mute"){echo "Mute";}
echo "</td><td><span style='color:";
if($infraction['appstatus']=="0"){echo "N/A";}
elseif($infraction['appstatus']=="1"){echo "black";}
elseif($infraction['appstatus']=="2"){echo "blue";}
elseif($infraction['appstatus']=="3"){echo "red";}
elseif($infraction['appstatus']=="4"){echo "green";}
echo "orange";
echo "'>";
if($infraction['appstatus']=="0"){echo "N/A";}
elseif($infraction['appstatus']=="1"){echo "Not Appealed";}
elseif($infraction['appstatus']=="2"){echo "Appeal Pending";}
elseif($infraction['appstatus']=="3"){echo "Appeal Rejected";}
elseif($infraction['appstatus']=="4"){echo "Appeal Accepted";}
echo"</span>
</td>
<td>
Never
</td>
<td data-container='body' data-placement='left' rel='tooltip' title='' data-original-title='January 4th, 2014 - 6:20 PM'>
<a href='/infractions/52c8a54fc8f2382195d7b3c4'>
17 minutes
ago
</a>";
}
echo "</td>
</tr>
</tbody>
</table>
</section>
</section>";
(ignore the unmatched {} errors, as I didn't spend a lot of time shortening it, also I can connect to the database, I just didn't include it.)
I don't get any errors, and everything before the second foreach works fine.

Related

How to display Pagination and Search in JQUERY Datatables having images in the table data?

This is my table body.....
<section class="content">
<div class="row">
<div class="col-12">
<div class="card">
<div class="card-header">
<h3 class="card-title"></h3>
</div>
<!-- /.card-header -->
<div class="card-body">
<table id="example1" class="table table-bordered table-striped">
<thead>
<tr>
<th>Category ID</th>
<th>Category Image</th>
<th>Category Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</thead>
<tbody>
<?php
$sql = "SELECT * FROM `categories`";
$res_data = mysqli_query($con, $sql);
while ($row = mysqli_fetch_assoc($res_data)) {
// code...
?>
<tr>
<td><?php echo $row['Category_ID']; ?></td>
<td><?php echo "<img src=\"data:image;base64," . $row['Category_Image_Temp'] . "\" alt=" . $row['Category_Image_Name'] . " width=\"50px\" height=\"50px\">"; ?></td>
<td><?php echo $row['Category_Name']; ?></td>
<td><textarea><?php echo $row['Category_Name']; ?></textarea></td>
<td><a class="btn btn-info btn-sm" href="#">
<i class="fas fa-pencil-alt">
</i>
Update
</a></td>
<td><a class="btn btn-danger btn-sm" href="#">
<i class="fas fa-trash">
</i>
Delete
</a></td>
</tr>
<?php
}
?>
</tbody>
<tfoot>
<tr>
<th>Category ID</th>
<th>Category Image</th>
<th>Category Name</th>
<th>Edit</th>
<th>Delete</th>
</tr>
</tfoot>
</table>
</div>
<!-- /.card-body -->
</div>
<!-- /.card -->
</div>
<!-- /.col -->
</div>
<!-- /.row -->
</section>
So when I remove <td><?php echo "<img src=\"data:image;base64," . $row['Category_Image_Temp'] . "\" alt=" . $row['Category_Image_Name'] . " width=\"50px\" height=\"50px\">"; ?></td> this line from the code, then only the pagination and search appears.
Main issue is when I display images in the datatables the pagination and search doesn't appears.
I want to display pagination and search when I display images in datatables.
Anybody can help?
Thanks....

How to concatenate html stylized table with php and sql

So, i have this code that returns me a simple table with the correct db values:
<?php
echo "<table border='1'>
<tr>
<th>Código</th>
<th>Nome</th>
<th>Indicou</th>
</tr>";
while($coluna_bd_tabela = mysqli_fetch_array($sql_indicador_resul)){
echo "<tr>";
echo "<td>" . $coluna_bd_tabela['usu_codigo'] . "</td>";
echo "<td>" . $coluna_bd_tabela['usu_nome'] . "</td>";
echo "<td>" . $coluna_bd_tabela['usu_indicador_codigo'] . "</td>";
echo "</tr>";
}
echo "</table>";
?>
And this one is the stylized table without the querys working:
<table class="table table-striped projects">
<thead>
<tr>
<th style="width: 1%">#</th>
<th style="width: 20%">Nome</th>
<th>Membros Recentes</th>
<th>Project Progress</th>
<th>Status</th>
<th style="width: 20%">#Edit</th>
</tr>
</thead>
<tbody>
<tr>
<td>echo $coluna_bd_tabela['usu_codigo']</td>
<td>
<a> echo $coluna_bd_tabela['usu_nome']</a>
<br />
<small>echo $coluna_bd_tabela['usu_indicou']</small>
</td>
<td>
<ul class="list-inline">
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
</ul>
</td>
<td class="project_progress">
<div class="progress progress_sm">
<div class="progress-bar bg-green" role="progressbar" data-transitiongoal="57"></div>
</div>
<small>57% Complete</small>
</td>
<td>
<button type="button" class="btn btn-success btn-xs">Success</button>
</td>
<td>
<i class="fa fa-folder"></i> View
<i class="fa fa-pencil"></i> Edit
<i class="fa fa-trash-o"></i> Delete
</td>
</tr>
</tbody>
I want to show the stylized table with the values of the querys, but this is destroying me. The database connection is fine, these are the querys on a include connection, they are working fine too, as have been shown on the simple table:
$sql_indicador = "SELECT * FROM esc_usuarios WHERE usu_indicador_codigo = '" . $_SESSION['codigo'] . "'";
$sql_indicador_resul = mysqli_query($conexao, $sql_indicador);
To provide a full solution:
<table class="table table-striped projects">
<thead>
<tr>
<th style="width: 1%">#</th>
<th style="width: 20%">Nome</th>
<th>Membros Recentes</th>
<th>Project Progress</th>
<th>Status</th>
<th style="width: 20%">#Edit</th>
</tr>
</thead>
<?php while($coluna_bd_tabela = mysqli_fetch_array($sql_indicador_resul)){ ?>
<tbody>
<tr>
<td><?php echo $coluna_bd_tabela['usu_codigo']; ?></td>
<td>
<a><?php echo $coluna_bd_tabela['usu_nome']; ?></a>
<br />
<small><?php echo $coluna_bd_tabela['usu_indicou']; ?></small>
</td>
<td>
<ul class="list-inline">
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
<li>
<img src="images/user.png" class="avatar" alt="Avatar">
</li>
</ul>
</td>
<td class="project_progress">
<div class="progress progress_sm">
<div class="progress-bar bg-green" role="progressbar" data-transitiongoal="57"></div>
</div>
<small>57% Complete</small>
</td>
<td>
<button type="button" class="btn btn-success btn-xs">Success</button>
</td>
<td>
<i class="fa fa-folder"></i> View
<i class="fa fa-pencil"></i> Edit
<i class="fa fa-trash-o"></i> Delete
</td>
</tr>
</tbody>
<?php } ?>
</table>
You forgot to use the <?php and ?> opening and closing tags around the echo statements. Furthermore, you missed the ; at the end of each statement. I've also moved the beginning and the end of the table out of PHP's echo since I believe it looks much clearer this way.

not showing red when company is expired/suspended

needing some help reviewing some code a previous developer did. this code is on various sites and all seem to be working except for one. This company is expired and not showing up red when doing a search.
<?php
//$today = date ('Y-m-d');
//$expire_date = date ('Y-m-d', strtotime ( '-90 day' . $today));
if($result){
echo
'<table class="" style="margin:0 0 !important;">
<tr>
<th class="center hide" style="width:5px!important;">X2Pay</th>
<th class="header" > Company</th>
<th class="header"><a href="javascript:;" id="header_type">Type</th>
<th class="header"><a href="javascript:;" id="header_country">Country</th>
<th class="header"><a href="javascript:;" id="header_city">City</th>
<th class="header"><a href="javascript:;" id="header_quality">Quality<br />&Risk</th>
</tr>';
foreach ($result as $row) { //echo $row->x2paystatus.'<br />';
?>
<?php
//if($row->x2paystatus == 'Active (Y)'){
if($row->status == 'Active'){
//echo $row->x2paystatus;
$color = null;
}else{
$color = 'color:#dd2644;';
}
//echo $row->memberid;
?>
<tr style="<?php echo $color;?>" class="<?php echo $row->HQ_Br;?>">
<td class="center hide" style="vertical-align:middle;">
<?php if(trim($row->x2paystatus) == 'Active (Y)'){?>
<div style="border:0px solid red;">
<a href="http://x2payonline.com" target="_blank">
<img class="" style="width:25px;" src="<?php echo $wp_upload_dir.'/2015/10/x2pay.png';?>" alt="">
</a>
</div>
<?php } ?>
</td>
<td>
<a href="<?php echo site_url('member-area/view-company-profile?id='.$row->memberid);?>">
<?php echo $row->membername;?></a>
</td>
<td class="" ><?php echo $row->HQ_Br;?></td>
<td><?php echo $row->country;?></td>
<td><?php echo $row->city;?></td>
<td class="center hide">
<span class="tooltip tooltip-effect-5">
<a class="tooltiplink" href="<?php echo site_url('member-area/view-company-profile?id='.$row->memberid);?>"><span class="tooltip-item">View</span></a>
<span class="tooltip-content clearfix">
<span class="tooltip-text">
<p>Tier : <?php if($row->tierlevel){echo $row->tierlevel;}else{echo '-';}?></p>
<p>Risk level : <?php if($row->quality_risk_level){echo $row->quality_risk_level;}else{ echo '8'; }?></p>
<p class="img">
<?php
for($i=1;$i<=$row->quality_risk_startrating;$i++)
{
echo '<img style="width:16px!important;height:16px!important;margin:0;" src="'.$img_dir.'star-active.png" />';
}
?>
</p><br /><br />
<p>Since : <?php echo date('Y', strtotime($row->enrollment_date));?></p>
</span>
</span>
</span>
</td>
</tr>
<?php
}
echo '</table>';
}else{
//echo 'No result';
}
?>
Since it is rendering red on other sites that means this is clearly a css styling rule override which is preventing it from rendering red. I'd look into the style sheet for that site and see if background has tr css rule with the !important declaration. Many times when your dealing with tables people write css rules and accidentally override css on other tables unintentionally.
I would use F12 on table element and see what css rules are being applied to that element on the site in question.

How to work with dynamic checkboxes?

I am using code igniter. For employee i need to select possible services they can perform.i have a field service limitation where i need to store many service id that the employee can perform. my view file where i have service list is
<div class="container top">
<ul class="breadcrumb">
<li>
<a href="http://localhost/elfanto/elfanto_billing/admin/employee">
Admin </a>
<span class="divider">/</span>
</li>
<li class="active">
Service </li>
</ul>
<div class="row">
<div class="span12 columns">
<div >
<?php
$attributes = array('class' => 'form-inline reset-margin', 'id' => 'myform');
$options_manufacture = array(0 => "all");
foreach ($category as $row)
{
$options_manufacture[$row['id']] = $row['name'];
}
//save the columns names in a array that we will use as filter
$options_products = array();
foreach ($service as $array) {
foreach ($array as $key => $value) {
$options_products[$key] = $key;
}
break;
}
echo form_open('admin/employee', $attributes);
?>
</div>
<table class="table table-striped table-bordered table-condensed">
<thead>
<tr>
<th class="header">Service id</th>
<th class="yellow header headerSortDown">Service name </th>
<th class="green header">Service catogary</th>
<th class="red header">Service tax</th>
<th class="red header">Service length</th>
<th class="red header">Service price</th>
<th class="red header">Actions</th>
</tr>
</thead>
<tbody>
<?php
foreach($service as $row)
{
echo '<tr>';
echo '<td>'.$row['id'].'</td>';
echo '<td>'.$row['service_name'].'</td>';
echo '<td>'.$row['category'].'</td>';
echo '<td>'.$row['service_tax'].'</td>';
echo '<td>'.$row['service_length'].'</td>';
echo '<td>'.$row['service_price'].'</td>';
echo '<td class="crud-actions">
<input type="checkbox" value=""/>
</td>';
echo '</tr>';
}
?>
</tbody>
</table>
<?php echo '<div class="pagination">'.$this->pagination->create_links().'</div>'; ?>
</div>
</div>
<div>
<button class="btn btn-primary" type="submit">Save changes</button>
<button class="btn" type="reset">Cancel</button>
</div>
<?php echo form_close(); ?>
here i have multiple check boxes when i choose the check box i need to get the id value and update the employee service limitation field with 4 or more id
You can use input arrays -
<input type="checkbox" value="'.$row['id'].'" name="services[]"/>
And store it in database as json string.

couldnt show data php in html

i'm facing fetch problem in my code. the statement couldn't return the value in $row[]. i have declared the variable before, but only one variable could show in the result. here is my code :
<?php
//require_once('function.php');
//session_start();
//if (!is_user()) {
// redirect('signin.php');
//}
$page_title = "Purchase Order";
include_once 'header.php';
$no_po = $_GET['no_po'];
// include database and object files
include_once 'config/database.php';
//$total_harga = $harga_jual * $qty;
// instantiate database and product object
$database = new Database();
$db = $database->getConnection();
$query = "SELECT
po.no_po,
barang.nama_barang,
klien.nama_klien,
po.tgl_po,
po.qty,
barang.harga_jual,
po.status_po,
po.keterangan,
surat_pengantar.tgl_kirim,
gudang.nama_gudang,
barang.harga_jual * po.qty AS total_harga
FROM
public.po,
public.barang,
public.klien,
public.good_receipt,
public.gudang,
public.surat_pengantar
WHERE
no_po = :no_po AND
po.id_barang = barang.id_barang AND
po.id_klien = klien.id_klien AND
po.no_po = surat_pengantar.no_po AND
po.no_po = good_receipt.no_po AND
gudang.id_gudang = good_receipt.id_gudang";
$stmt = $db->prepare( $query );
$stmt->bindParam(':no_po',$no_po);
$result = $stmt->execute();
$no_po ;
$nama_barang=null;
$harga_jual=null;
$qty=null;
$nama_klien=null;
$total_harga=null;
$status_po=null;
$tgl_po=null;
$tgl_kirim=null;
$nama_gudang=null;
$keterangan=null;
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$no_po = $row['no_po'];
$nama_barang = $row['nama_barang'];
$nama_klien = $row['nama_klien'];
$tgl_po = $row['tgl_po'];
$qty = $row['qty'];
$harga_jual = number_format($row['harga_jual'],0,",",".");
$status_po = $row['status_po'];
$keterangan = $row['keterangan'];
$tgl_kirim = $row['tgl_kirim'];
$nama_gudang = $row['nama_gudang'];
$total_harga = number_format($row['total_harga'],0,",",".");
}
?>
<body>
<div class="container">
<div class="row">
<div class="col-xs-6">
<h1>
<a href="https://twitter.com/tahirtaous">
<img src="YSU.png">
Logo here
</a>
</h1>
</div>
<div class="col-xs-6 text-right">
<h1>Purchase Order</h1>
<h1><small><?php echo $no_po?></small></h1>
</div>
</div>
<div class="row">
<div class="col-xs-5">
<div class="panel panel-default">
<div class="panel-heading">
<h4>From: Your Name</h4>
</div>
<div class="panel-body">
<p>
Address <br>
details <br>
more <br>
</p>
</div>
</div>
</div>
<div class="col-xs-5 col-xs-offset-2 text-right">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Detail Invoice</h4>
</div>
<div class="panel-body">
<table border="0">
<tr>
<td>Tanggal PO</td>
<td>:</td>
<td><?php echo $tgl_po?></td>
</tr>
<tr>
<td>Gudang</td>
<td>:</td>
<td><?php echo $nama_gudang?></td>
</tr>
<tr>
<td>Nama Pelanggan</td>
<td>:</td>
<td><?php echo $nama_klien?></td>
</tr>
<tr>
<td>Status Purchase Order</td>
<td>:</td>
<td><?php echo $status_po?></td>
</tr>
<tr>
<td>Tanggal Kirim</td>
<td>:</td>
<td><?php echo $tgl_kirim?></td>
</tr>
<tr>
<td>Mata Uang</td>
<td>:</td>
<td>IDR(Rp)</td>
</tr>
<tr>
<td>Term Pembayaran</td>
<td>:</td>
<td>30 hari</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<!-- / end client details section -->
<table class="table table-bordered">
<thead>
<tr>
<th>
<h4>No.item</h4>
</th>
<th>
<h4>Nama barang</h4>
</th>
<th>
<h4>Quantity</h4>
</th>
<th>
<h4>Price</h4>
</th>
<th>
<h4>Keterangan</h4>
</th>
<th>
<h4>Sub Total</h4>
</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
echo "<tr>";
echo "<td>{$no}</td>";
echo"<td>{$nama_barang}</td>";
echo"<td>{$qty}</td>";
echo"<td class='text-right'>{$harga_jual}</td>";
echo"<td class='text-right'>{$keterangan}</td>";
echo"<td class='text-right'>{$total_harga}</td>";
echo"</tr>";
$no++;
echo"</tbody>";
echo"</table>";
?>
<div class="row text-right">
<div class="col-xs-2 col-xs-offset-8">
<p>
<strong>
Total : <br>
</strong>
</p>
</div>
<div class="col-xs-2">
<strong>
Rp<?php echo $total_harga?> <br>
</strong>
</div>
</div>
</div>
</div>
</body>
</html>
please help me to solve this. thanks
You are over-writing the results with each iteration of your while clause:
You want to output the HTML as you process each row from the recordset.
I think you are looking to do something like this though it has been quite a while since I wrote procedural PHP:
<body>
<?php
while($row = $stmt->fetch(PDO::FETCH_ASSOC)){
$no_po = $row['no_po'];
$nama_barang = $row['nama_barang'];
$nama_klien = $row['nama_klien'];
$tgl_po = $row['tgl_po'];
$qty = $row['qty'];
$harga_jual = number_format($row['harga_jual'],0,",",".");
$status_po = $row['status_po'];
$keterangan = $row['keterangan'];
$tgl_kirim = $row['tgl_kirim'];
$nama_gudang = $row['nama_gudang'];
$total_harga = number_format($row['total_harga'],0,",",".");
?>
<div class="container">
<div class="row">
<div class="col-xs-6">
<h1>
<a href="https://twitter.com/tahirtaous">
<img src="YSU.png">
Logo here
</a>
</h1>
</div>
<div class="col-xs-6 text-right">
<h1>Purchase Order</h1>
<h1><small><?php echo $no_po?></small></h1>
</div>
</div>
<div class="row">
<div class="col-xs-5">
<div class="panel panel-default">
<div class="panel-heading">
<h4>From: Your Name</h4>
</div>
<div class="panel-body">
<p>
Address <br>
details <br>
more <br>
</p>
</div>
</div>
</div>
<div class="col-xs-5 col-xs-offset-2 text-right">
<div class="panel panel-default">
<div class="panel-heading">
<h4>Detail Invoice</h4>
</div>
<div class="panel-body">
<table border="0">
<tr>
<td>Tanggal PO</td>
<td>:</td>
<td><?php echo $tgl_po?></td>
</tr>
<tr>
<td>Gudang</td>
<td>:</td>
<td><?php echo $nama_gudang?></td>
</tr>
<tr>
<td>Nama Pelanggan</td>
<td>:</td>
<td><?php echo $nama_klien?></td>
</tr>
<tr>
<td>Status Purchase Order</td>
<td>:</td>
<td><?php echo $status_po?></td>
</tr>
<tr>
<td>Tanggal Kirim</td>
<td>:</td>
<td><?php echo $tgl_kirim?></td>
</tr>
<tr>
<td>Mata Uang</td>
<td>:</td>
<td>IDR(Rp)</td>
</tr>
<tr>
<td>Term Pembayaran</td>
<td>:</td>
<td>30 hari</td>
</tr>
</table>
</div>
</div>
</div>
</div>
<!-- / end client details section -->
<table class="table table-bordered">
<thead>
<tr>
<th>
<h4>No.item</h4>
</th>
<th>
<h4>Nama barang</h4>
</th>
<th>
<h4>Quantity</h4>
</th>
<th>
<h4>Price</h4>
</th>
<th>
<h4>Keterangan</h4>
</th>
<th>
<h4>Sub Total</h4>
</th>
</tr>
</thead>
<tbody>
<?php
$no=1;
echo "<tr>";
echo "<td>{$no}</td>";
echo"<td>{$nama_barang}</td>";
echo"<td>{$qty}</td>";
echo"<td class='text-right'>{$harga_jual}</td>";
echo"<td class='text-right'>{$keterangan}</td>";
echo"<td class='text-right'>{$total_harga}</td>";
echo"</tr>";
$no++;
echo"</tbody>";
echo"</table>";
?>
<div class="row text-right">
<div class="col-xs-2 col-xs-offset-8">
<p>
<strong>
Total : <br>
</strong>
</p>
</div>
<div class="col-xs-2">
<strong>
Rp<?php echo $total_harga?> <br>
</strong>
</div>
</div>
</div>
</div>
<?
}
?>
</body>
</html>

Categories