I tried adding a cart in my application, but how you can while in order to add a new cart page does not reload, but the cart increases.
my controllers
function add_cart($code) {
$produk = $this->Model->get_id($code);
$data = array(
'id' => $produk->code
'qty' => 1,
'name' => $produk->name );
$this->cart->insert($data);
}
Model
function get_id($code) {
$query = $this->db->select('*')
->from('mytable')
->where('code', $code)
->get();
if ($query->num_rows() > 0) {
foreach ($query->result() as $data) {
$hasil[] = $data;
}
return $hasil;
}
}
View
<?php foreach($product as $row) ?>
<div class="col-sm-4">
<div class="col-item">
<div class="info">
<div class="row">
<div class="price col-md-12">
<h5> <?php echo $row->name_product ;?></h5>
</div>
</div>
<div class="separator clear-left">
<p class="btn-add">
class="hidden-sm" href="<?php echo site_url('home/add_cart/'.$row->code);?>">Add</a></p>
</div>
</div>
</div>
</div>
<?php endforeach ?>
Use ajax to replace the href to request the add_chart operation, and get the response to refresh the page content without reload the page.
Related
i am new in code igniter i am trying to fetch the data of all jobs of same location in my view
i write some code but when i try to get the data its gives me single data i want to fetch all data of same location
here is my controler
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}
and model is
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->row();
}else {
return false;
}
}
and my view is
<?php foreach ($data as $key => $data): ?>
<div class="job-box">
<div class="company-logo">
</div>
<div class="row">
<div class="col-lg-12">
<div class="alert alert-success alert-2" role="alert">
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
Recently Posted Jobs in <?php echo $data->location?>
Citi</div>
<div class="row">
<div class="job-box">
<div class="description">
<div class="float-left">
<h5 class="title"><?php echo $data->job_title?></h5>
<div class="candidate-listing-footer">
<ul>
<li><i class="flaticon-pin"> </i><?php echo $data->location?></li>
<li><i class="flaticon-money"> </i><?php echo $data->salary?></li>
<li><i class="flaticon-work"> </i><?php echo $data->timing?></li>
<!--<li><i class="flaticon-honor"> </i><?php echo $jobs->qualification?></li>
<li><i class="flaticon-notepad"> </i><?php echo $jobs->experience?></li>-->
</ul>
<h6>Deadline:<?php echo $data->deadline?></h6>
</div>
</div>
<div class="div-right">
Apply Now
<!--<i class="flaticon-heart favourite"></i>-->
</div>
</div>
</div>
</div>
</div><!--end row-->
</div>
</div>
<?php endforeach ?>
</div>
i want to show multi record not single by clicking the hyper link
Many Thanks in advance
Junaid Amin
Please update your Controller method, Model method and View as follows:
Controller::Method:
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , $data);
}
Model::Method:
public function getBlogBylocation($location){
$this->db->select('*');
$this->db->like('location', $location);
$query = $this->db->get('jobs');
if($query->num_rows() > 0){
return $query->result();
} else {
return false;
}
}
View:
<?php foreach ($jobs as $row): ?>
//Your staff goes here (i.e. $row->name_of_properties)
<?php endforeach ?>
pass name instead of id in you a tag and make route for that inside config/routes file
$route['location/(:any)'] = 'controller_name/function_name/$1';
public function location($location){
$data['jobs'] = $this->edituser_model->getBlogBylocation($location);
$this->load->view('locwise' , ['data'=>$data]);
}
I'm trying to create a search form in codeigniter. This is my controller and model,
Shop.php [ Controller ]
public function s(){
$q = $this->input->get('q');
if( $q == null ){
$this->index();
}
$data['products'] = $this->product_model->search_products( $q );
$this->load->view('shop', $data);
}
Product_model.php
public function search_products( $keyword ){
$this->db->select('product.id, product.product_name, product.product_description, product.product_image1,
brand.brand_name, category.category_name, type.type_name, stock.selling_price,
(SELECT COALESCE(ROUND(SUM(rate)/( COUNT(rate)*5 )*100), 0) FROM rating WHERE rating.product_id = product.id)AS rating,
(SELECT COALESCE(COUNT(rating.id), 0) FROM rating WHERE rating.product_id = product.id)AS rate_count');
$this->db->from('product');
$this->db->join('stock','stock.product_id = product.id');
$this->db->join('brand', 'brand.id = product.brand_id');
$this->db->join('category', 'category.id = product.category_id');
$this->db->join('type', 'type.id = product.type_id');
$this->db->join('color', 'color.id = product.color_id');
$this->db->where('MATCH(product.product_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(brand.brand_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(category.category_name) AGAINST("'.$keyword.'")');
$this->db->or_where('MATCH(type.type_name) AGAINST("'.$keyword.'")');
$this->db->where('stock.qty >', 0);
$this->db->group_by('product.id');
$query = $this->db->get();
return $query->result_array();
}
But return value in null. So I tried calling search_product( $keyword ) function in my view and it worked. but when I print $data['product'] variable and its empty.
What is the problem here? Thank you.
Edit
The search result shows here.
<div class="row product-list-item">
<?php
// This is just used for check the query
$p = $this->product_model->search_products( 'mesh' );
echo '<br>';
print_r($products);
if( $products != null ){
foreach ( $products as $product ){
?>
<!-- item.2 -->
<div class="product-item-element col-sm-6 col-md-6 col-lg-4">
<!--Product Item-->
<div class="product-item">
<div class="product-item-inner">
<div class="product-img-wrap">
<img src="<?php echo base_url($product['product_image1']);?>" alt="">
</div>
<div class="product-button">
<i class="fa fa-eye"></i>
</div>
</div>
<div class="product-detail">
<p class="product-title"><a href="<?php echo site_url('shop/view/'.$product['id'])?>">
<?php echo $product['product_name']; ?>
</a></p>
<div class="product-rating">
<div class="star-rating" itemprop="reviewRating" itemscope="" itemtype="" title="Rated 4 out of 5">
<span style="width: <?php echo $product['rating'];?>%"></span>
</div>
<a href="#" class="product-rating-count">
<span class="count"><?php echo $product['rate_count'];?></span> Reviews
</a>
</div>
<p class="product-description">
<?php echo $product['product_description']; ?>
</p>
<h5 class="item-price"><?php echo $product['selling_price']; ?></h5>
</div>
</div>
<!-- End Product Item-->
</div>
<?php
}
}else{
?>
<div class="text-center no-items">
<h4>No items</h4>
</div>
<?php } ?>
</div>
I want to make an output product detail but the output shows only one productID and other productID can not output!
Is there a mistake on my controller, model, and view ??
View
<?php foreach ($categories as $key=>$value) { ?>
<div class="col-sm-3">
<h5><?php echo $categories[$key]->categoriesName; ?></h5>
<?php if(count($categories[$key]->subs)>0) { ?>
<?php foreach ($categories[$key]->subs as $k=>$v) { ?>
<ul>
<li><?php echo $v->productName; ?> </li>
</ul>
<?php } ?>
<?php } ?>
</div>
<?php } ?>
<div class="row" id="productMain">
<?php if(count($details)>0){ ?>
<div class="col-sm-4">
<div id="mainImage"><img src="<?php echo base_url('upload/'.$details->photo);?>"
alt="" class="img-responsive">div>
</div>
<div class="col-sm-4">
<div class="box">
<h1 class="text-center"><?php echo $details->productName;?></h1>
<p class="price">
<?php echo $details->price;?>
</p>
</div>
</div>
<?php } ?>
</div>
Controller
public function detail($product_id=''){
$data=array('title' =>'Pasar Online | Detail Produk',
'username' => $this->session->userdata('username'),
'categories' => $this->categories_model->get_categories(),
'details' => $this->categories_model->get_details($product_id=''),
'isi' =>'member/detail');
$this->load->view('template_home/wrapper',$data);
}
Model
public function get_details($product_id)
{
$this->db->where("productID",$product_id);
$query = $this->db->query("SELECT * FROM product");
$row = $query->row();
return $row;
}
print_r($details)
stdClass Object ( [productID] => P0001 [categoriesID] => 1 [productName] => AAA ) 1
Where is my mistake? Thank you
In your model use
For object output $row = $query->result();
For array output $row = $query->result_array();
So change the function to
public function get_details($product_id) { $this->db->where("productID",$product_id); $query = $this->db->query("SELECT * FROM product"); $row = $query->result(); return $row; }
I want to make a menu list based on roles, but when I want to display them to my home view it doesnt show up the list.. how can I fix it? the problem is that the menu list is not showing with separates files.
controller
public function getModules($id_module){
if($this->session->userdata('log')){
$data = $this->session->userdata('log');
$menu = array();
$seccions = $this->module->get_rows();
foreach ($seccions as $index => $seccion){
$modules = $this->module->query("SELECT CONCAT('".$seccion['id']."',storelte_modulo.id) AS id,CONCAT('".base_url('assets/img/sidebar')."','/',storelte_modulo.icon) as icon, storelte_modulo.modulo AS value,storelte_modulo.seccion_id,CONCAT('".base_url()."',storelte_modulo.url) AS url FROM storelte_modulo INNER JOIN storelte_modulo_perfil ON storelte_modulo_perfil.modulo_id = storelte_modulo.id WHERE seccion_id = $seccion[id] AND storelte_modulo_perfil.perfiles_id = $data[id] AND storelte_modulo_perfil.STATUS = 1");
$seccions[$index]['data']= $modules;
if (!count($seccions[$index]['data']))
unset($seccions[$index]);
}
foreach ($seccions as $item)
array_push($menu,$item);
$this->data['fields'] = $menu;
$this->load->view('modules_view',$this->data);
}
}
model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
return $this->db->get()->result_array();
}
public function query($query){
return $this->db->query($query)->result_array();
}
view
home.php
<section class="content">
<!-- Small boxes (Stat box) -->
<div class="row">
<h3 class="text-center">Welcome to storeLTE, click a module below to get started!</h3>
<div class="home_module_list">
<div class="module_item">
<?php $this->load->view('modules_view'); ?>
</div>
</div>
</div>
<!-- Main row -->
<div class="row">
<!-- Left col -->
<section class="col-lg-12 connectedSortable">
</section>
</div>
</section>
modules
<?php $this->load->view('header'); ?>
<div class="module_item">
<?php foreach ($fields as $session) : ?>
<?php foreach ($session['data'] as $itemData) : ?>
<div class="module_item" title="<?= $itemData['value'] ?>">
<img src="<?= $itemData['icon'] ?>"/>
<?= $itemData['value']?>
</div>
<?php endforeach ?>
<?php endforeach ?>
</div>
try this at your model
public function get_rows(){
$this->db->select('id,seccion');
$this->db->from('storelte_seccion');
$this->db->get();
return $query->result();
}
I am trying to populate a dropdown form with an array of events from a database table.
I am getting all kinds of errors:
undefined $user_events
etc.
I know it is the way I am passing the array from the controller I think.
Model:
public function dropdown_add_event($id, $Id)
{
$sql = "SELECT *
FROM table_eventcards
WHERE table_eventcards.id = ?
AND table_eventcards.Id = ?";
$query = $this->db->query($sql, array($id, $Id));
return $query->result_array();
}
Controller:
$this->data['user_events'] = $this->model_location->dropdown_add_event(); // Retrieve an array of user events
View:
<?php
$user_events = '';
if($user_events){
foreach($user_events as $events): {
?>
<div class="row">
<div class="col-md-12">
<h3>Add Event To Location</h3>
<div class="row">
<div class="row">
<div class="row">
<div id="myselect" class="col-md-12">
<p></p>
<div class="form-group col-xs-5 col-lg-3">
<?php
$attributes = 'input type="hidden" name="myselect" value="events[]"';
echo form_dropdown('myselect', $events, '',$attributes);
?>
<button id="grab1" type="button" class="btn btn-default">Clear</button>
<hr/>
</div>
</div>
</div>
</div>
</div>
<?php
}
endforeach;
}
?>
Updated:
$Id is already defined a few lines above:
$data['Id'] = $this->session->userdata['logged_data']['member_id'];
In your controller, it seems you forgot to gave out parameters:
$this->data['user_events'] = $this->model_location->dropdown_add_event();
// ^^ $id, $fkUserId
No parameters are passed/found dropdown_add_event();
Just pass the appropriate variables that you have on your controller:
$fkUserId = $data['fkUserId'];
$this->data['user_events'] = $this->model_location->dropdown_add_event($id, $fkUserId);
$this->load->view('view_name', $this->data);