Can not take value from view on codeigniter - php

I've been working on this codeigniter php app for my class and I have a view where you can select a quantity of a coin and the selected quantity comes back as a null.
my controller:
public function add(){
$this->form_validation->set_rules('quantity', 'Quantity', 'required');
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$inventorycoin = $this->uri->segment('3');
$inventoryquantity = $this->input->post('quantity');
$this->coin_model->add_coin($inventorycoin, $inventoryquantity);
//$this->session->set_flashdata('post_deleted', 'Post has been deleted');
redirect('coins');
}
my model:
public function add_coin($inventorycoin, $inventoryquantity){
$data = array (
'inventory_user' => $this->session->userdata('user_id'),
'inventory_quantity' => $inventoryquantity,
'inventory_coin' => $inventorycoin
);
return $this->db->insert('inventory', $data);
}
my view:
<?php echo form_open_multipart('coins/add', 'id="coins"'); ?>
<table width="100%">
<?php foreach($coins as $coin) : ?>
<tr>
<td><img class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
<td><?php echo $coin['coin_name']; ?></td>
<td><?php echo $coin['coin_country']; ?></td>
<td><?php echo $coin['coin_weight']; ?></td>
<td><input type="number" min="0" name="quantity" placeholder="Qt" style="width: 40px"></td>
<td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>
</tr>
<?php endforeach; ?>
</table>
</form>
What could be the problem?

You don't use submit button. So this part won't work.
$inventoryquantity = $this->input->post('quantity');
You are looping the input .So try to change your form to this.
<?php echo form_open_multipart('coins/add/', 'id="coins"'); ?>
<table width="100%">
<?php foreach($coins as $coin) : ?>
<tr>
<input name="inventorycoin[]" type="hidden" value = "<?=$coin['coin_id'];?>" >
<td><img class="post-thumbnail" style="width: 200px; height: 200px" src="<?php echo site_url(); ?>assets/images/coins/<?php echo $coin['coin_image']; ?>"></td>
<td><?php echo $coin['coin_name']; ?></td>
<td><?php echo $coin['coin_country']; ?></td>
<td><?php echo $coin['coin_weight']; ?></td>
<td><input type="number" min="0" name="quantity[]" placeholder="Qt" style="width: 40px"></td>
<td>--</td>
</tr>
<?php endforeach; ?>
</table>
<button type="submit"> Add </button>
</form>
and your function to this
public function add(){
$this->form_validation->set_rules('quantity', 'Quantity', 'required');
if(!$this->session->userdata('logged_in')){
redirect('users/login');
}
$inventorycoin = $this->input->post('inventorycoin');
$inventoryquantity = $this->input->post('quantity');
echo "<pre>";print_r($inventorycoin);
echo "<pre>";print_r($inventoryquantity); // you need to loop this because it is an array
redirect('coins');
}

You can change the below line in the view:
<td><p><a class="btn btn-primary" href="<?php echo base_url(); ?>coins/add/<?php echo $coin['coin_id']; ?>">Add</a></p></td>
Into this and try.
<td><p><a type="submit" class="btn btn-primary" href="<?php echo base_url('coins/add/'.$coin['coin_id']); ?>">Add</a></p></td>

Related

codeigniter model function result array with WHERE clause filtering

I already have a working function in my model
//GETTING TOTAL PURCHASE
function total_portal_user_sale($user_id)
{
$return = 0;
$transection = $this->db->get('transection')->result_array();
foreach ($transection as $row) {
if ($row['buyer'] == $user_id) {
$return += $row['credit'];
}
}
return $return;
}
i am getting data from my database correctly but i need to filter result by where clause for example this query
$this->db->query("SELECT sum(credit) as total FROM transection
where buyer = $user_id and status like '%paid%'");
this is the same scenario but with data filtering, I don't know how to use my query with active record statement
if someone rewrite my
function total_portal_user_sale($user_id)
with my WHERE clause query i will appreciate it ...
this is how i am rendering data into tables in view file
<div class="panel-body" id="demo_s">
<table id="demo-table" class="table table-striped" data-pagination="true" data-show-refresh="false" data-ignorecol="0,6" data-show-toggle="false" data-show-columns="false" data-search="true" data-striped="true" data-filter-control="true" data-show-export="true" >
<thead>
<tr>
<th style="width:4ex"><input type="checkbox" id="users_idtog"/></th>
<th><?php echo translate('no');?></th>
<th><?php echo translate('image');?></th>
<th><?php echo translate('name');?></th>
<th><?php echo translate('email');?></th>
<th><?php echo translate('phone');?></th>
<th>Total<br>Sale</th>
<th>This<br>Month<br>Sale</th>
<th>Total Debit</th>
<th>Total<br>Profit</th>
<th><?php echo translate('creation');?></th>
<th>Monthly<br>Sale<br>Target</th>
<th>Coupon<br>Code</th>
<th>Coupon<br>Expiry<br>Date</th>
<th class="text-right"><?php echo translate('options');?></th>
</tr>
</thead>
<tbody >
<?php
$i = 0;
foreach($all_users as $row){
$i++;
?>
<tr>
<td><input type="checkbox" class="users_id" name="users_id[]" value="<?php echo $row['user_id']; ?>" /></td>
<td><?php echo $i; ?></td>
<td>
<img class="img-sm img-circle img-border"
<?php if(file_exists('uploads/user_image/user_'.$row['user_id'].'.jpg')){ ?>
src="<?php echo base_url(); ?>uploads/user_image/user_<?php echo $row['user_id']; ?>.jpg"
<?php } else if($row['fb_id'] !== ''){ ?>
src="https://graph.facebook.com/<?php echo $row['fb_id']; ?>/picture?type=large" data-im='fb'
<?php } else if($row['g_id'] !== ''){ ?>
src="<?php echo $row['g_photo']; ?>"
<?php } else { ?>
src="<?php echo base_url(); ?>uploads/user_image/default.png"
<?php } ?> />
</td>
<td><?php echo $row['username']; ?></td>
<td><a href="mailto:<?php echo $row['email']; ?>" target="_self" ><?php echo $row['email']; ?></td>
<td><?php echo $row['phone']; ?></td>
<td class="text-right"><?php echo $this->crud_model->total_purchase($row['user_id']); ?></td>
<td class="text-right">
<?php
$Days = explode("-",date("d-m-Y"));
echo round($this->crud_model->sale_target($Days[0],$row['user_id']),2);
?>
</td>
<td class="text-right"><?php echo $this->crud_model->total_profit($row['user_id']); ?></td>
<?php
$ts = $this->crud_model->total_portal_user_sale($row['user_id']);
$tc = $this->crud_model->total_portal_user_profit($row['user_id']);
$tp = $ts - $tc
?>
<td class="text-right">
<?php echo $ts; ?>
</td>
<td class="text-right"><?php echo date('d M,Y',$row['creation_date']);?></td>
<td><?php echo $row['monthly_sale_target']; ?></td>
<td><?php echo $row['coupon_code']; ?></td>
<td><?php echo $row['expiry_date']; ?></td>
<td class="text-right">
<a class="btn btn-purple btn-xs btn-labeled fa fa-tag" data-toggle="tooltip"
onclick="ajax_modal('add_discount','<?php echo translate('give_target_discount'); ?>','<?php echo translate('adding_discount!'); ?>','add_discount','<?php echo $row['user_id']; ?>')" data-original-title="Edit" data-container="body">
<?php echo translate('give_discount');?>
</a>
<a class="btn btn-success btn-xs btn-labeled fa fa-wrench" data-toggle="tooltip"
onclick="ajax_modal('edit','<?php echo translate('edit_user'); ?>','<?php echo translate('successfully_edited!'); ?>','user_edit','<?php echo $row['user_id']; ?>')"
data-original-title="Edit" data-container="body">
<?php echo translate('edit');?>
</a>
<a onclick="delete_confirm('<?php echo $row['user_id']; ?>','<?php echo translate('really_want_to_delete_this?'); ?>')" class="btn btn-xs btn-danger btn-labeled fa fa-trash" data-toggle="tooltip"
data-original-title="Delete" data-container="body">
<?php echo translate('delete');?>
</a>
</td>
</tr>
<?php
}
?>
</tbody>
</table>
</div>
Hope this will help you :
function total_portal_user_sale($user_id)
{
$return = 0;
$this->db->select('*');
//$this->db->select('sum(credit) as total,buyer');
$this->db->from('transection');
$this->db->where('buyer',$user_id);
$this->db->like('status','paid');
$query = $this->db->get();
if ($query->num_rows() > 0 )
{
foreach ($query->result_array() as $row)
{
if ($row['buyer'] == $user_id)
{
$return += $row['credit'];
}
}
return $return;
}
}
You can also use $this->db->select_max('credit') for the same
For more : https://www.codeigniter.com/user_guide/database/query_builder.html

FOREACH printing all of its element

I just want the table show the category name rather than showing its ID to the user...So I just added $data['category'] object in my Controller
Controller Code:
$Query="group by product_image order by product_id";
$data["products"] = $this->insert->Select('product',$Query);
foreach ($data['products'] as $waste) {
$qry = "where id=".$waste->product_category;
$cat[] = $this->insert->select('category',$qry);
}
$data['category'] = $cat;
//print_r($data["products"]);
$this->load->view('manage/product/list',$data);
View Code:
if(!empty($products)) {
$l=0;
$base_url=$this->config->item('base_url');
$tot = count($products);
foreach($products as $products_info) {
$l++; ?>
<tr>
<td valign="middle"><div class="checkbox check-default">
<!-- <input type="checkbox" value="3" id="checkbox2" />
<label for="checkbox2"></label> !-->
</div></td>
<td valign="middle"><?php echo $products_info->product_id;?></td>
<td valign="middle"><?php echo $products_info->product_code;?></td>
<td valign="middle"><?php echo $products_info->product_name;?>-<?php echo $products_info->prod_weight;?></td>
<td valign="middle"><a target="_new" href="<?php echo $base_url. "product/".$products_info->product_image;?>"><img src="<?php echo $base_url. "product/".$products_info->product_image;?>" width="75" height="30" alt="" /></a></td>
<td valign="middle"><?php echo $products_info->product_amount; ?></td>
<td valign="middle">
<?php foreach($category as $cat) { ?>
<?php echo $cat[0]->category_title; ?>
<?php } ?>
</td>
<td><?php if($products_info->status==1) {?>
<button Placeid="<?php echo $products_info->id;?>" action='1' class="btn btn-mini btn-success status-btn">Approved</button>
<?php } else { ?>
<button Placeid="<?php echo $products_info->id;?>" action='0' class="btn btn-mini btn-danger status-btn">UnApproved</button>
<?php } ?>
</td>
The Output image of this Code =>
but I just want to show the correct category to the corresponding row..So please help to get off from this...Waiting for possible solutionzzz
No need multiple query : simple use INNER JOIN .
select * from product as p inner join category as c on p.product_category=c.id

Getting extra blank spaces while printing table

I am trying to print a table using Datatable Jquery , and it is giving me some extra spaces unnecessary.
<tbody>
<?php foreach($products as $product): ?>
<tr class="data-item show">
<td class="one wide tdalign collapsing">
<div id="divwhlprodmttrcheck" class="ui checkbox">
<input id="cbwhlprodmttrcheck" type="checkbox" name="inventory[]" value="<?php echo $product->productId ?>">
<label style="padding-left:0;"></label>
</div>
</td>
<td id="tdwhlprodmtin<?php echo $product->productId ?>" ><?php echo $product->ProductName ?></td>
<td id="tdwhlprodmtinsku<?php echo $product->productId ?>"><?php echo $product->SKU ?></td>
<td id="tdwhlprodmtincat<?php echo $product->productId ?>"><?php echo $product->Category ?></td>
<td id="tdwhlprodmtinsc<?php echo $product->productId ?>"><?php echo $product->Subcategory ?></td>
<td id="tdwhlprodmtinbr<?php echo $product->productId ?>"><?php echo $product->BrandName ?>
#if($product->BrandDeleted == 1)
<i class="warning red circle icon brand" data-content="Brand has been deleted" data-variation="inverted"></i>
#endif
</td>
<td id="tdwhlprodmtinsp<?php echo $product->productId ?>"><?php echo $product->SupplierName ?>
#if($product->SuppDeleted == 1)
<i class="warning red circle icon supp" data-content="Supplier has been deleted" data-variation="inverted"></i>
#endif
</td>
<td id="tdwhlprodmtinst<?php echo $product->productId ?>"><?php echo $product->AvailableStock ?></td>
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>"><?php echo $product->Price ?></td>
#if($product->Discount == 0)
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>">NA</td>
#else
<td id="tdwhlprodmtinpr<?php echo $product->productId ?>"><?php echo $product->Discount ?>%</td>
#endif
<input type="hidden" id="inhidsuppmain{{$product->productId}}" value="{{$product->SupplierName}}" >
<input type="hidden" id="inhidbrndmain{{$product->productId}}" value="{{$product->BrandName}}">
<input type="hidden" id="tdwhlprodmtprice<?php echo $product->productId?>" value="<?php echo $product->Price ?>" name="price">
<input type="hidden" id="inhiddiscountmain{{$product->productId}}" value="{{$product->Discount}}" >
<input type="hidden" id="inhidIsOnSalemain{{$product->productId}}" value="{{$product->onsale}}" >
<input type="hidden" id="inhidIsOnClearancemain{{$product->productId}}" value="{{$product->onclearance}}" >
<input type="hidden" name="inID" class="inid<?php echo $product->productId ?>" id="tdwhlprodmtinid<?php echo $product->productId ?>" value="<?php echo $product->productId; ?>">
<td class="tdalign collapsing"><i class="link black write icon" data-content="edit" data-variation="inverted" name="<?php echo $product->productId ?>" id="tdwhlprodmt<?php echo $product->productId; ?>" onclick="produps(4); getinValues('<?php echo $product->productId ?>'); gettingMulid('<?php echo $product->productId ?>')"></i>
</td>
</tr>
<?php endforeach; ?>
If I remove the code between <tbody> there is no spaces while code inside gives me spaces as below
I have even tried balancing it with margin-bottom , but no result yet.
Any help is appreciated.

Codeigniter Update cart

I have the problem with update function in CodeIgniter. The cart is not updating and don't understand why. Can you help me to find a solution for this issue?
This is my Update function
public function update($in_cart = null) {
$data = $_POST;
$this->cart->update($data);
//show cart page
redirect('cart','refresh');
}
This is my form inside sidebar.php
<form action="cart/update" method="POST">
<table cellpadding="6" cellspacing="1" style="width:100%" border="0">
<tr>
<th>QTY</th>
<th>Item Description</th>
<th style="text-align:right">Item Price</th>
</tr>
<?php $i = 1; ?>
<?php foreach ($this->cart->contents() as $items) : ?>
<input type="hidden" name="<?php echo $i.'[rowid]'; ?>" value="<?php echo $items['rowid']; ?>" />
<tr>
<td><input type="text" style="color:black; text-align:center;margin-bottom:5px;" name="<?php $i.'[qty]'; ?>" value="<?php echo $items['qty']; ?>" maxlength="3" size="3"></td>
<td><?php echo $items['name']; ?></td>
<td style="text-align:right"><?php echo $this->cart->format_number($items['price']); ?></td>
</tr>
<?php $i++; ?>
<?php endforeach; ?>
<tr>
<td></td>
<td class="right"><strong>Total</strong></td>
<td class="right" style="text-align:right">$<?php echo $this->cart->format_number($this->cart->total()); ?></td>
</tr>
</table>
<br>
<p><button class="btn btn-default" type="submit">Update Cart</button>
<a class="btn btn-default" href="cart">Go to Cart</a></p>
</form>
Try using $this->input->post() to get all form posted data.
https://ellislab.com/codeigniter/user-guide/libraries/input.html

ajax post in search

I make search form and I'm using it as ajax searchform.
I have problems in html(data), when I put the html(data) it will display the loading image and the table too it will double here's my code:
$(document).ready(function(){
$("#loading").hide();
<?php $type_temp = (!empty($type)) ? 'company/' .$type:'company';?>
$("#search_id").click(function(){
$("#loading").show();
$.post("<?php echo url_for('/AdmSys_dev.php/' .$type_temp); ?>", $("#send_search").serialize(), function(data){
$(".result").html(data);
});
});
});
How will I put the result into the page?
heres my html code below
<div id="sf_admin_content">
<h2>Company</h2><br />
<div style="margin-bottom:20px;">
<form id="send_search" action="" method="get">
<input type="text" name="search" placeholder="Search: " value="<?php echo $search; ?>" />
<input id="search_id" type="button" value="Search" />
</form>
</div>
Add
<div id="loading" style="margin-left:186px; margin-top:30px;">
<img alt="" src="/images/preloader_transparent.gif" />
</div>
<table class="location" cellspacing="0" style="width:500px;">
<thead>
<tr>
<th>Id</th>
<th>Name</th>
<th>Description</th>
<th>Photo</th>
<th>Option</th>
</tr>
</thead>
<tbody class="result">
<?php foreach($company as $x => $companies): ?>
<tr id="company<?php echo $companies->getId(); ?>" class="<?php echo ($x&1) ? 'even':'odd'; ?>">
<td align="right"><?php echo $companies->getId(); ?></td>
<td><?php echo $companies->getName(); ?></td>
<td><?php echo $companies->getDescription(); ?></td>
<td><img alt="" src="/uploads/company/<?php echo $companies->getImageFull(); ?>" /></td>
<td align="right">
<a href="<?php echo url_for('company/edit?id=' .$companies->getId() )?>">
<img alt="edit" src="/images/pencil.png" />
</a>
<a href="#delete" title="delete" onclick="return goDelete(<?php echo $companies->getId(); ?>)">
<img alt="delete" src="/images/delete.png" />
</a>
</td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php
$type_temp = (!empty($type))?'company/'.$type:'company';
$data = array(
'maxPerPage' => $company->getmaxPerPage(),
'lastPage' => $company->getlastPage(),
'nbResults' => $company->getnbResults(),
'curPage' => $curPage,
'linkPage' => '/AdmSys.php/company'.$type_temp. '?search'.$search
);
include_partial('event/pager',$data); ?>
</div>
as youve notice i put the class results in the <tbody class="result></tbody>

Categories