I'm using codeigniter and postgresql
I'm getting troubled using IF statement in Views
What I want to do is that if seq=3 I dont want to put this code.
<td> EDIT/ADD</td>
Sample Table
seq column1 column2
1 null null EDIT/ADD
2 null null EDIT/ADD
3 null null EDIT/ADD
Result should be like this:
seq column1 column2
1 null null EDIT/ADD
2 null null EDIT/ADD
3 null null
The actual code for this in Views for Sample Table above
<tbody>
<?php foreach ($value as $v){ ?>
<tr>
<td><?php echo $v->seq?></td>
<td><?php echo $v->column1?></td>
<td><?php echo $v->column2?></td>
<td> EDIT/ADD</td>
</tr>
<?php } ?>
</tbody>
Please help me.
try this code
<?php
$count = 0;
foreach ($value as $v){
$count++;
?>
<tr>
<td><?php echo $v->seq?></td>
<td><?php echo $v->column1?></td>
<td><?php echo $v->column2?></td>
<td>
<?php
if($count!=3)
{
?>
EDIT/ADD
<?php
}
else
{
echo " ";
}
?>
</td>
</tr>
<?php
}
?>
<tbody>
<?php foreach ($value as $v){ ?>
<tr>
<td><?php echo $v->seq?></td>
<td><?php echo $v->column1?></td>
<td><?php echo $v->column2?></td>
<?php if ($v->seq == 3) { ?>
<td> </td>
<?php } else { ?>
<td> EDIT/ADD</td>
<?php }?>
</tr>
Related
I want to print multiple data from the database in one table, however, it shows one row.
Model code is below. Please look at my codes and give some suggestion on how I can solve this issue.
Model CODE
function show_list()
{
$query=$this->db->get('student');
return $query;
}
Here is my controller code below:
Controller Code
<?php
class Showlist extends CI_Controller {
function index()
{
//$this->load->view('showlist');
$this->load->model('main_model');
$data['no']=$this->main_model->show_list();
//return the data in view
$this->load->view('showlist', $data);
}
}
?>
Here is my view code below:
View Code
<style>
table
{
border-collapse: collapse;
}
table, th, td
{
border: 1px solid black;
}
</style>
<body>
<html>
<center>
<table>
<tr>
<th>NAME </th>
<th>Student ID</th>
<th>Address</th>
<th>Fathers Name</th>
<th>Mothers Name</th>
<th>Education</th>
</tr>
<?php
if($no->num_rows()> 0){
foreach ($no->result() as $row) {
?>
<tr>
<td><?php echo $row->Name; ?></td>
<td><?php echo $row->StudentID; ?></td>
<td><?php echo $row->Address; ?></td>
<td><?php echo $row->FatherName; ?></td>
<td><?php echo $row->MotherName; ?></td>
<td><?php echo $row->Eduation; ?></td>
<!-- <td>EDIT</td>
<td>Delete</td>-->
</tr>
<?php
}
?>
<tr>
<td><center><a href='add_new_student.php'>ADD NEW STUDENT</a></center></td>
<td><a href='logout.php'>Logout</a></center></td>
</tr>
<?php
}
else
{
echo 'NO data Found';
}
?>
</table>
</center>
</html>
</body>
It only prints one row:
There are more data in my database. I try result() but it is not printing the rows that I want.
THis is my table data 4 rows
result() display only one row:
Add this line in model code:
return $query->result_array();
And change the foreach loop :
foreach ($no as $row) {..}
Change the line to all the rows:
<td><?php echo $row->Name; ?></td> to
<td><?=$row['Name'];?><td>
I am trying to save the multiple images path in database and upload images in root's upload/images[directory]. I have done it and its working. Images name are saved in database and and the image files are being uploaded. I just did save the images name by imploding. Now I need to explode that image in view. How can i do that? I have tried the following code in view and its not working.
<?php foreach ($products as $p): ?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<!-- <td><?php echo $p['picture']; ?> </td> -->
<td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>
<td>
View |
Edit |
Delete
</td>
</tr>
It throws this error:
A PHP Error was encountered
Severity: Notice
Message: Array to string conversion
Filename: views/dashboard.php
Line Number: 47
and this is my line 47:
<td><img src="<?php echo base_url('uploads/images/').explode('|',$p['picture']);?>" /> </td>
Incase there is mistake in my image upload function, here is my code for it:
public function set_product($id=0)
{
#code
// if($this->input->post('userSubmit')){
$picture=array();
$count=count($_FILES['picture']['name']);
//Check whether user upload picture
if(!empty($_FILES['picture']['name'])){
foreach($_FILES as $value)
{
for($s=0; $s<=$count-1; $s++)
{
$_FILES['picture']['name']=$value['name'][$s];
$_FILES['picture']['type'] = $value['type'][$s];
$_FILES['picture']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['picture']['error'] = $value['error'][$s];
$_FILES['picture']['size'] = $value['size'][$s];
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture[] = $uploadData['file_name'];
}
else{
$picture = '';
}
}
}
}//end of first if
else{
$picture = '';
}
$data=array(
'product_name'=>$this->input->post('product_name'),
'produce_description'=>$this->input->post('produce_description'),
'product_price'=>$this->input->post('product_price'),
'picture'=>implode('|',$picture)
);
if ($id==0)
{
return $this->db->insert('products',$data);
}
else {
$this->db->where('id',$id);
return $this->db->update('products',$data);
}
}
And this is my model function for getting data:
public function get_product()
{
#code
$query=$this->db->get('products');
return $query->result_array();
}
Any kind of help are highly appreciated. Thank you.
You should try something like this
<?php foreach ($products as $p): ?>
<?php
// explode images into a variable/array
$images=explode('|',$p['picture']);
?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<!-- <td><?php echo $p['picture']; ?> </td> -->
<td><img src="<?php echo base_url('uploads/images/').$images[0];?>" /> </td>
<td>
View |
Edit |
Delete
</td>
</tr>
Edit
I will give you an example from a code in production at this link
<div class="aa-properties-details-img" style="margin-bottom: 25px;">
<?php
$property[0]['images']=explode(',',$property[0]['img']);
if(count($property[0]['images'])>0){
for($i=0;$i<count($property[0]['images']);$i++)
{ ?>
<img src="<?php echo base_url().'img/'.$property[0]['images'][$i]?>" alt="img">
<?php }
}else{
?>
<img src="<?php echo base_url().'img/no-image.jpg'?>" alt="img">
<?php
}
?>
</div>
Try this:
<?php foreach ($products as $p): ?>
<tr>
<td><?php echo $p['id']; ?></td>
<td><?php echo $p['product_name']; ?></td>
<td><?php echo $p['product_price']; ?></td>
<td><?php echo $p['produce_description']; ?></td>
<td>
<?php $images=explode('|',$p['picture']);
foreach($images as $image) {
?>
<img src="<?php echo base_url('uploads/images/').$image; ?>" width="50" height="50" />
<?php } ?>
</td>
<td>
View |
Edit |
Delete
</td>
</tr>
I'm trying to delete data from my database
Here's my code:
VIEW
<table border="1" class="table">
<tr>
<th>No</th>
<th>Nama Depan</th>
<th>Nama Belakang</th>
<th>TTL</th>
<th>Email</th>
<th>Keterangan</th>
</tr>
<?php
$i = 0;
foreach ($dataMember as $result) { ?>
<tr>
<td><?php echo ($i+1); ?></td>
<td><?php echo $result['namaDepan'];?></td>
<td><?php echo $result['namaBelakang']; ?></td>
<td><?php echo $result['TTL']; ?></td>
<td><?php echo $result['email']; ?></td>
<td>
<button>Delete</button>
</td>
</tr>
<?php $i++; } ?>
</table>
MODEL
public function Hapusdata($id){
$this->db->where('email', $id);
$this->db->delete('daftar');
}
Controller
public function hapusMember()
{
$this->load->model('Member');
$this->load->helper('url');
$id = $this->uri->segment(3);
$this->Member->Hapusdata($id);
redirect (site_url('Belajarberhadiah/halaman_admin'));
}
and the problem is i get
Severity: Notice
Message: Trying to get property of non-object
Filename: views/halaman_dMember.php
Line Number: 105
What should i do?
Please write href for delete tag as below:
delete
Also be sure that the column[email] you have used for where condition in controller is the correct one. Because column name is "email" in where condition and you are passing an integer value.
I'm trying to display data from query but I can't show it as it should.
My tables in database are: ordersheader and orderitems. It's one to many relationship between them. One ordersheader have many orderitems. They are joined ON idOrder. One idOrder in ordersheader corresponds to many rows in orderitems.
I have to show all orders for current date and to show all orderitems for each idOrder. It should look like this for all orders:
http://prntscr.com/7lwoan
First row is order - it has to be displayed all orderitems for this order and when you click Details it has to be shown each orderitem on separate row.
Now, as I used code below, each orderitem is shown on separate row. How to make it looks like the above link? Thanks!
My model is:
function getOrders(){
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d ');
$this->db->select('ordersheader.*,customer.name,orderitems.*');
$this->db->from('ordersheader');
$this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$query = $this->db->get();
return $query->result();
}
My view is:
<?php
if($orderlist){
foreach($orderlist as $row) {
?> <tr class="overview">
<td><?php echo $row->idOrder; ?></td>
<td class="text-center"><img src="<?= base_url();?>/assets/images/tick.png"></td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php echo $row->quantity; ?></td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: 60%;">
60%
</div>
</div>
</td>
<td>18:00</td>
<td><button type="button" class="btn btn-link btn-xs view-full-order">Full</button></td>
</tr>
<tr class="full hide">
<td></td>
<td></td>
<td></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php echo $row->quantity; ?></td>
<td>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand; ?>%;">
<?php echo $rand; ?>%
</div>
</div>
</td>
<td>14:00</td>
<td></td>
</tr>
<?php } } ?>
</tbody></table>
Edited: I made it with group_concat and explode. But now, problem is that for each idOrder I should calculate average rand (this is the value of progress bar) of all orderitems. This should be for each order - avegare sum of orderitems. Now, it's not calculated correct.
It now looks like that:
http://prntscr.com/7mu0k3
This row where is Billa should be average of the next 2 rows.
My view is:
<tbody class="client">
<?php
if ($orderlist) {
foreach ($orderlist as $row) {
$i = 0;
?> <tr class="overview">
<td><?php echo $row->idOrder; ?></td>
<td class="text-center">
<?php
$s = $row->eggSize;
$egg_size_array = explode(",", $row->eggSize);
$quantity_array = explode(",", $row->quantity);
if (!empty($egg_size_array))
foreach ($egg_size_array as $key => $value) {
$rand[$key] = rand(1, 100);
$random_hour[$key] = rand(01, 24);
}
?>
</td>
<td><?php echo $row->name; ?></td>
<td><?php echo $row->eggSize; ?></td>
<td><?php
$s = $row->quantity;
$s = explode(',', $s);
echo array_sum($s);
print_r($rand); echo "<br>";print_r(array_sum($rand)); echo "<br>"; print_r(count($egg_size_array));
?></td>
<td>
<div class="progress">
<div class="progress-bar" role="progressbar" style="width: <?php
echo array_sum($rand)/ count($egg_size_array); ?>%;">
<?php echo array_sum($rand)/ count($egg_size_array); echo "%"; ?>
</div>
</div>
</td>
<td><?php echo max($random_hour); echo " ч."; ?></td>
<td><button type="button" class="btn btn-link btn-xs view-full-order">
<?php echo lang('details'); ?> </button></td>
</tr>
<?php
if (!empty($egg_size_array))
foreach ($egg_size_array as $key => $value) {
// $rand = rand(1, 100);
//$random_hour = rand(01, 24);
?>
<tr class="full hide">
<td></td>
<td></td>
<td></td>
<td>
<?php echo $value; ?></td>
<td><?php echo $quantity_array[$key]; ?></td>
<td>
<div class="progress">
<div class="progress-bar progress-bar-info" role="progressbar" style="width: <?php echo $rand[$key]; ?>%;">
<?php echo $rand[$key]; ?>%
</div>
</div>
</td>
<td><?php echo $random_hour[$key] . ' ч.' ?></td>
<td></td>
</tr>
<?php
}
?><?php
}
}
?>
</tbody></table>
My model is:
function getOrders(){
$date = new DateTime("now");
$curr_date = $date->format('Y-m-d');
$this->db->select('orderitems.eggSize as size');
$this->db->select('ordersheader.*,customer.name,GROUP_CONCAT(orderitems.itemNumber) as itemNumber');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.quantity ) as quantity ');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.unitPrice) as unitPrice');
$this->db->select('ordersheader.*,customer.name,ordersheader.*,customer.name,GROUP_CONCAT(orderitems.eggSize ) as eggSize');
$this->db->from('ordersheader');
$this->db->join('orderitems', 'orderitems.idOrder = ordersheader.idOrder');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$this->db->group_by('orderitems.idOrder');
$query = $this->db->get();
return $query->result();
}
You should create a function to return order children.
function GetItems($idOrder)
{
return $this->db->get_where("orderitems", "idOrder" => $idOrder)->array_result();
}
After that, you will remove the JOIN with the orderitems table. And then you should get the array_result of your query and iterate with it by foreach and attach the children in your collection like this:
$curr_date = $date->format('Y-m-d ');
$this->db->select('ordersheader.*,customer.name');
$this->db->from('ordersheader');
$this->db->join('customer', 'customer.idCustomer = ordersheader.idCustomer');
$this->db->where('DATE(orderDueDate)', $curr_date);
$query = $this->db->get()->array_result();
$index = 0;
foreach($query as $item)
{
$query[$index]["children"] = GetItems($item->idOrder);
$index++;
}
I've created a mockup in a fiddle to show you an example: http://codepad.org/LfjQJQCP
I have a page to add items to a table that is rendered with renderpartial via ajax. Every time the user adds an item, I render the table with all the items. I also have an ajaxlink to delete the item the user chooses. The problem is that I can only delete the last record of the table, if I click on other ajaxlink, nothing happens.
here is the view file to search and add items
<div class="row">
<?php $this->widget('zii.widgets.jui.CJuiAutoComplete', array(
'name' => 'test_autocomplete',
'source'=>$this->createUrl('insumos/ver'),
'value' => "",
'options' => array(
'minChars'=>1,
'autoFill'=>false,
'focus'=> 'js:function( event, ui ) {
$( "#test_autocomplete" ).val( ui.item.label );
return false;
}',
'select'=>'js:function( event, ui ) {
$( "#Pedidos_codigoinsumo").val(ui.item.id);
$( "#Pedidos_codigo").val(ui.item.codigo);
$( "#Pedidos_nombre").val(ui.item.label);
$( "#Pedidos_cantidad").focus();
$( "#cantidades").show();
return false;
}',
),
'htmlOptions'=>array( 'autocomplete'=>'off'),
)); ?>
</div>
<div id="cantidades">
<div class="row">
<?php echo $form->labelEx($model,'codigo'); ?>
<?php echo $form->textField($model,'codigo',array('size'=>60,'maxlength'=>300)); ?>
<?php echo $form->hiddenField($model,'codigoinsumo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'nombre'); ?>
<?php echo $form->textField($model,'nombre',array('size'=>60,'maxlength'=>300)); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'cantidad'); ?>
<?php echo $form->textField($model,'cantidad',array('size'=>60,'maxlength'=>300)); ?>
<?php echo $form->error($model,'codigoinsumo'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'observaciones'); ?>
<?php echo $form->textArea($model,'observaciones',array('rows'=>6, 'cols'=>50)); ?>
</div>
<?php echo CHtml::ajaxButton('Agregar', array('pedidos/adicion'), array('type'=>'POST','data'=>'js:$("#pedidoalmacen-form").serialize()','update'=>'#req_res02'));
echo CHtml::ajaxButton('Cancelar Pedido', array('pedidos/cancelar'), array('update'=>'#req_res02'));
?>
<?php $this->endWidget(); ?>
<div id="req_res02"></div>
the view that has the table is:
if($ver==1)
{
?>
<table>
<tr>
<td align="center">Borrar</td>
<td align="center">Código</td>
<td align="center">Insumo</td>
<td align="center">Cantidad</td>
<td align="center">Observaciones</td>
</tr>
<?php
$codigo= Yii::app()->session['pedido-codigo'];
$contado=count($codigo);
$i=0;
while($i<$contado)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$codigo[$i]['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $codigo[$i]['contador']; ?></td>
<td align="left"><?php echo $codigo[$i]['insumo']; ?></td>
<td align="left"><?php echo $codigo[$i]['cantidad']; ?></td>
<td align="left"><?php echo $codigo[$i]['observaciones']; ?></td>
</tr>
<?php
$i++;
}
?>
</table>
<div class="row buttons">
<?php echo CHtml::submitButton('Agregar'); ?>
</div>
<?php
}
?>
and the controller is
public function actionAdicion()
{
$model=new Pedidos;
if(isset($_POST['Pedidos']))
{
$model->attributes=$_POST['Pedidos'];
if(isset(Yii::app()->session['pedido-codigo']))
{
$codigo= Yii::app()->session['pedido-codigo'];
$contador=count($codigo)+1;
$auxiliar=array('contador'=>$contador,'id'=>$model->codigoinsumo,'codigo'=>$model->codigo,'insumo'=>$model->nombre,'cantidad'=>$model->cantidad,'observaciones'=>$model->observaciones);
}
else
{
$auxiliar=array('contador'=>1,'id'=>$model->codigoinsumo,'codigo'=>$model->codigo,'insumo'=>$model->nombre,'cantidad'=>$model->cantidad,'observaciones'=>$model->observaciones);
}
$codigo[]=$auxiliar;
Yii::app()->session['pedido-codigo']=$codigo;
$this->renderPartial('insumosporpedir',array('ver'=>'1'),FALSE,TRUE);
//Yii::app()->end();
//echo $_POST['data1'];//CHtml::encode(print_r("hola", true));
}
}
public function actionCancelar()
{
unset(Yii::app()->session['pedido-codigo']);
$this->renderPartial('insumosporpedir',array('ver'=>'0'),FALSE,TRUE);
}
public function actionQuitar()
{
if(isset($_POST['dato']))
{
$codigo= Yii::app()->session['pedido-codigo'];
$compara=$_POST['dato'];
foreach ($codigo as $subkey => $subarray)
{
if($subarray['contador']==$compara)
{
unset($codigo[$subkey]);
//echo $codigo[$subkey];
}
}
$ver=1;
if(count($codigo)>0)
{
Yii::app()->session['pedido-codigo']=$codigo;
}
else
{
unset(Yii::app()->session['pedido-codigo']);
$ver=0;
}
//$this->layout='';
$this->renderPartial('insumosporpedir',array('ver'=>$ver),false,true);
//Yii::app()->end();
}
thank you!
I found the problem. the problem wasnt the unset method, it was in the view where I showed the values in the array. insted of using
while($i<$contado)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$codigo[$i]['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $codigo[$i]['contador']; ?></td>
<td align="left"><?php echo $codigo[$i]['insumo']; ?></td>
<td align="left"><?php echo $codigo[$i]['cantidad']; ?></td>
<td align="left"><?php echo $codigo[$i]['observaciones']; ?></td>
</tr>
<?php
$i++;
}
I replaced it with
foreach ($codigo as $key => $value)
{
?>
<tr>
<td align="left"><?php echo CHtml::ajaxLink('Quitar', Yii::app()->createUrl('pedidos/quitar'),array('update'=>'#req_res02','type'=>'POST','data'=>array('dato'=>$value['contador'])),array('id'=>'quitar-'. uniqid())); ?></td>
<td align="left"><?php echo $value['codigo']; ?></td>
<td align="left"><?php echo $value['insumo']; ?></td>
<td align="left"><?php echo $value['cantidad']; ?></td>
<td align="left"><?php echo $value['observaciones']; ?></td>
</tr>
<?php
}
the error occurred because I lost the continuity in the keys everytime I delete a middle array