setting up categories in view in codeigniter - codeigniter

I am building a online restaurant project through which user can order items. I have a sql table with id,item_name,item_image,item_price and category.
I want to display all items in my view category wise.
My controller function is :-
public function order()
{
if($this->session->userdata('is_logged_in'))
{
$data['i']=$this->model_db->item_details();
$this->load->view('order',$data);
}
else {
redirect('main/login');
}
}
My model function is
public function item_details()
{
$query = $this->db->query("SELECT * FROM items");
return $query->result();
}
My view code is
<div class="container-fluid">
<div class="row">
<div class="col-lg-2 col-xs-12 side-nav"></div>
<div class="col-lg-10 main">
<?php
foreach ($i as $item) {
# code...
?>
<div class="col-xs-12 col-lg-4 <?php echo $item->category ?>">
<div class="col-xs-12 col-lg-8 item">
<img src="<?php echo $item->img?>" width="200px" height="150px">
<p><?php echo $item->item_name?></p>
<p><?php echo $item->category?></p>
<p><?php echo $item->price?></p>
<p>Place Order
</form>
</div></div>
<?php
}
?>
</div>
</div>
</div>
</body
</html>
I want to display all results above in view category wise.

There are several possible solutions. None of them is codeigniter specific.
Here's some untested code that should do what you want:
public function item_details()
{
$items = array();
$categories = $this->db->query("SELECT DISTINCT(category) FROM items")->result();
foreach($categories as $category)
{
$items[$data] = $this->db->query('SELECT * FROM items WHERE category = ?', $category)->result();
}
return $items;
}
And you display it with the following in your view:
foreach ($i as $category => $item) {
// display it ...
}

Related

Get related products for a product by category

I'm trying to show products related to a category. But it is showing all products which are also related to other categories.
Blade:
<div class="row">
#foreach($categories as $category)
#foreach ($category->products as $product)
<div class="col-sm-12 col-md-6 col-lg-4 p-b-50">
<!-- Block2 -->
<div class="block2">
<div class="block2-img wrap-pic-w of-hidden pos-relative block2-labelnew">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $product->product_image }}"
class="img-thumbnail" style="width: 270px; height: 360px;" />
<div class="block2-overlay trans-0-4">
</div>
</div>
<div class="block2-txt p-t-20">
<a href="product-detail.html" class="block2-name dis-block s-text3 p-b-5">
{{ $product->product_name }}
</a>
<span class="block2-price m-text6 p-r-5">
$75.00
</span>
</div>
</div>
</div>
#endforeach
#endforeach
</div>
Controller:
public function products(Request $request, Product $product)
{
$categories = Category::with('products')->distinct()->get();
return view('product.listing', compact('product', 'categories'));
}
Route:
Route::get('/product/{id}','Admin\ProductController#products')->name('product.products');
Edit your controller:
public function products(Request $request, Product $product)
{
$categories = $product->categories;
return view('product.listing', compact('product', 'categories'));
}
Also you should have the categories relationship method on Product Model.
public function products(Request $request, Category $category)
{
$products = Product::where('category_id',$category->id)->get();
$categories = Category:all();
return view('product.listing', compact('products','categories'));
}
Your route is
Route::get('/product/{id}','Admin\ProductController#products')->name('product.products');
//{id} it means you're getting category id from route right? so you can directly access it in controller.
Controller
//$id is from route.
public function products($id)
{
$products = Product::with('category')->where('category_id',$id)->get();
return view('product.listing', compact('products'));
}
In your blade file
#foreach($products as $product)
//here your all product which belongs to that categories.
and now if you want to access categories then may do as.
categories :- {{ $product->category->name }} //make sure it belongsTo in product
#endforech
public function products(Request $request)
{
$categories = Category::where('id',$request->id)->with('products')->get();
return view('product.listing', compact('categories'));
}
#foreach($categories as $category)
#foreach ($category as $product)
Assuming a product has one category and you have a category_id column in your products table change products method in your ProductController like below.
public function products(Request $request, Product $product)
{
$relatedProducts = Product::where('category_id', $product->category_id)
->where('id', '!=', $product->id) // ignore current product
->get();
return view('product.listing', compact('product', 'relatedProducts'));
}
Replace id in your route withproduct`. Read More about route model binding.
Route::get('/product/{product}','Admin\ProductController#products')->name('product.products');
Then in your view your you can iterate through relatedProducts
controller:
public function products(Request $request, Category $category)
{
$category->load('products');
return view('product.listing', compact('category'));
}
route:
Route::get('/product/{category}','Admin\ProductController#products')->name('product.products');
blade file:
#foreach ($category->products as $product)
<div class="col-sm-12 col-md-6 col-lg-4 p-b-50">
<!-- Block2 -->
<div class="block2">
<div class="block2-img wrap-pic-w of-hidden pos-relative block2-labelnew">
<img src="{{ URL::to('/') }}/images/backend_images/product_images/{{ $product->product_image }}" class="img-thumbnail" style="width: 270px; height: 360px;" />
</div>
</div>
</div>
#endforeach

How to display Select - where condition value in input type textbox in codeigniter

I have a table Tax contains tax_id (1 & 2) ,Tax_price(12 & 8) & Tax_name (vat & Gst).
I want to display the VAT price in a textbox('txtvat')
SQl Query : SELECT`tax_price` FROM `tbl_taxmaster` WHERE `tax_name` = "VAT"
View
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label>
</div>
<?php foreach($taxvat as $row){ ?>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $row->tax_name;?>" class="form-control">
</div>
<?php } ?>
</div>
Controller
public function displayBillingPage()
{
$this->load->model('CrudModel');
$records['taxvat']=$this->CrudModel->getVatName();
$this->load->view('generatebill',$records);
}
Model
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
$query = $this->db->get();
return $query->result()->row()->tax_price;
}
You can't use row() and result() at the same time
row() is used to get only one row and result() to get more than one
If you are using row() then you can do this row()->column_name. But can't with result()
And if using column name in select() then don't do this row()->column_name
IF you are getting only one row no need to use foreach loop
For one row
Method 1
public function getVatName(){
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
return $this->db->get()->row();
}
Method 2
public function getVatName(){
return $this->db->get_where('tbl_taxmaster', ['tax_name' => 'VAT'])->row()->tax_price;
}
For multiple rows
Method 1
public function getVatName(){
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
return $this->db->get()->result();
}
Method 2
public function getVatName(){
return $this->db->get_where('tbl_taxmaster', ['tax_name' => 'VAT'])->result();
}
view :
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label></div>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $taxvat;?>" class="form-control">
</div>
</div>
Model:
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"VAT");
$query = $this->db->get();
return $query->row('tax_price');
}
**View**
<div class="row form-group">
<div class="col col-md-4"><label class=" form-control-label">VAT</label></div>
<?php foreach($taxvat as $row){ ?>
<div class="col-12 col-md-5">
<input type="text" id="txtVat" name="txtVat" value="<?php echo $row->tax_price;?>" class="form-control">
</div>
<?php } ?>
</div>
**Controller**
public function displayBillingPage()
{
$this->load->model('CrudModel');
$records['taxvat']=$this->CrudModel->getVatName();
$this->load->view('generatebill',$records);
}
**Model**
public function getVatName()
{
$this->db->select('tax_price');
$this->db->from('tbl_taxmaster');
$this->db->where('tax_name',"vat");
$query = $this->db->get();
return $query->result();
}

Missing argument in codeigniter

I am new to the codeigniter framework. When I try to load the parameter in codeigniter its showing an error like Missing argument 1. Can anyone please help solve my issue. Here Is my code.
This is My View Code. In view code i call the parameter in ">Read More
<div class="row m-t-20">
<?php foreach($show as $row) { ?>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<img src="<?php echo base_url()?>uploads/<?php echo $row['img_path']; ?>" alt="" class="img-responsive" />
</div>
<h2 class="title"><?php echo $row['title'];?></h2>
<p><?php echo $row['message'];?> ...</p>
<div class="more-link">Read More</div>
</div>
</div>
<?php }?>
</div>
Controller code
public function viewProjects($id)
{
$data['show']=$this->Selection_Model->fullProjects($id);
$this->load->view('topurl');
$this->load->view('nav');
$this->load->view('fullProjects',$data);
$this->load->view('footer');
}
Model Code
public function fullProjects($id){
$this->db->select('*');
$this->db->where('p_id',$id);
$this->db->from('projects');
$query = $this->db->get();
$result = $query->row_array();
return $result;
}
Final view code
<div class="col-md-7 ab-text">
<?php foreach($show as $row) { ?>
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<p><?php echo $row['message'];?> ...</p>
</div>
</div>
<?php }?>
</div>
</div>
In your view.Not need to use foreach loop.Beacuse in codeigniter row_array result set is used to fetch only first matched row. try like this..
<div class="col-md-7 ab-text">
<div class="col-md-3 col-sm-6 col-xs-12">
<div class="recent-pro-box">
<div class="pro-img">
<p><?php echo $show['message'];?> ...</p>
</div>
</div>
</div>
</div>
OR
If you want to use foreach loop.
In model change
$result = $query->row_array();
TO
$result = $query->result_array();
For more see docs Codeigniter Result Sets
All the code's are fine, except one line in Model. Your model should be
function fullProjects($id){
$this->db->select('*');
$this->db->where('p_id',$id);
$this->db->from('projects');
$query = $this->db->get();
$result = $query->result_array(); # Changed
return $result;
}
If you're accessing array data with this pattern $row['message'];, You must use the array as objective array like this $query->result_array();

Codeigniter : How to detect the first row of a query result

My viewis like below.
<div class="carousel-inner">
<?php
foreach($news_data as $nws){
?>
<div class="item **If this is the first row, then echo active**">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php } ?>
</div>
Model:
function get_news_update()
{
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
return $query->result();
}
Please see the <div class="item. If it is the first row of query result, then it will be item active.
How to do that ?
Modify your view like this:
<div class="carousel-inner">
<?php
foreach($news_data as $key => $nws){
?>
<div class="item <?php echo $key == 0 ? "active":""; ?>">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php } ?>
</div>
Here is your updated code
<div class="carousel-inner">
<?php
$i = 0;
foreach($news_data as $nws){
// it should be $i. not $i%2 right ?
if($i == 0)
{
$class="item active";
}
else
{
$class = "item";
}
?>
<div class="<?php echo $class; ?>">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
<?php $i++; } ?>
</div>
As per my knowledge,You are looking for below code.Please let me know if its not working.
function get_news_update()
{
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
return $query->first_row();
}
You can use row() function, which return always the first result.
Model.php
function get_news_update(){
$this->db->select('news_desc');
$this->db->from('news');
$this->db->where('stat', '1');
$this->db->order_by('id', 'desc');
$query = $this->db->get();
//check if exist
if (isset($query->row())){
return $query->row();
}
}
For more informations you will find here
View.php
<div class="carousel-inner">
<div class="item **If this is the first row, then echo active**">
<div class="col-md-12 news-item" style="padding-left: 0;">
<p><?php echo $nws->news_desc; ?></p>
</div>
</div>
</div>

codeigniter Invalid argument supplied for foreach()

I have a full working code with retrieve insert update and delete data to database.
All is working well, but if I delete some data, it is deleted but when direct to my view page again, then it will return "Invalid argument supplied for foreach()".
When I press again my view page link it is all normal and the data I delete is gone too, this is just happen with delete function.
Code :
Controller :
public function merk(){
//$data["result"] = $this->modelAdminMerk->getMerk();
$data["totalRows"] = $this->modelAdminMerk->getTotalRows();
$config["base_url"] = base_url() . "admin/merk";
$config["per_page"] = 15;
$config["total_rows"] = $data["totalRows"];
$config["uri_segment"] = 3;
$config["num_links"] = 10;
$this->pagination->initialize($config);
if( ! $this->uri->segment(3)){
$page = 0;
}else{
$page = $this->uri->segment(3);
}
$data["results"] = $this->modelAdminMerk->getMerkByPages($config["per_page"], $page);
$data["links"] = $this->pagination->create_links();
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminMerk", $data);
$this->load->view("adminView/adminFooter");
}
public function insertMerk(){
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminInsertMerk");
$this->load->view("adminView/adminFooter");
}
public function validateInsertMerk(){
$this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]");
if($this->form_validation->run()){
$newMerk = array("merk" => $this->input->post("namaMerk"));
$this->modelAdminMerk->insertMerk($newMerk);
$this->merk();
}else{
echo "GAGAL!!!";
}
}
public function editMerk($id){
$data["merk"] = $this->modelAdminMerk->getMerkById($id);
$this->load->view("adminView/adminHeader");
$this->load->view("adminView/adminEditMerk", $data);
$this->load->view("adminView/adminFooter");
}
function validateEditMerk(){
$this->form_validation->set_rules("namaMerk", "Nama Merk", "trim|required|min_length[3]|max_length[30]");
if($this->form_validation->run()){
$newMerk = array("merk" => $this->input->post("namaMerk"));
$this->modelAdminMerk->editMerk($this->input->post("id"), $newMerk);
$this->merk();
}else{
echo "GAGAL";
}
}
public function deleteMerk($id){
$this->modelAdminMerk->deleteMerk($id);
$this->merk();
}
Model :
function getMerk(){
$data = $this->db->query("SELECT * FROM merkbarang");
return $data->result();
}
function getMerkById($id){
$this->db->where("id", $id);
$data = $this->db->get("merkbarang");
return $data->row();
}
function insertMerk($newMerk){
$this->db->insert("merkbarang", $newMerk);
return true;
}
function editMerk($id, $newMerk){
$this->db->where("id", $id);
$this->db->update("merkbarang", $newMerk);
return true;
}
function deleteMerk($id){
$this->db->where("id", $id);
$this->db->delete("merkbarang");
return true;
}
function getTotalRows(){
$rows = $this->db->count_all("merkbarang");
return $rows;
}
function getMerkByPages($limit, $start){
$this->db->limit($limit, $start);
$query = $this->db->get("merkbarang");
if($query->num_rows() > 0){
foreach ($query->result() as $row){
$data[] = $row;
}
return $data;
}else{
return false;
}
}
View :
<div class="container">
<div class="row">
<div class="col-md-3">
<div class="panel panel-danger">
<div class="panel-body">
<ul class="nav nav-pills nav-stacked">
<li role="presentation" class="active">Insert Merk</li>
</ul>
</div>
</div>
</div>
<div class="col-md-9">
<div class="panel panel-default">
<div class="panel-heading">
<strong>Merk</strong>
</div>
<div class="panel-body">
<div class="row" id="tabelBarang">
<div class="col-md-1 colBarang">
<strong>No</strong>
</div>
<div class="col-md-7 colBarang">
<strong>Merk</strong>
</div>
<div class="col-md-4 colBarang">
<strong>Action</strong>
</div>
</div>
<?php
$angka = 0;
foreach ($results as $row){
$id = $row->id;
$merk = $row->merk;
?>
<div class="row" id="tabelBarang">
<div class="col-md-1 colBarang">
<?php echo ++$angka; ?>
</div>
<div class="col-md-7 colBarang">
<?php echo $merk; ?>
</div>
<div class="col-md-2 colBarang">
<button type="button" class="btn btn-info btn-xs" aria-label="Left Align">
Edit
</button>
</div>
<div class="col-md-2 colBarang">
<button type="button" class="btn btn-danger btn-xs" aria-label="Left Align">
Delete
</button>
</div>
</div>
<?php
}
?>
<?php echo $links; ?>
</div>
</div>
</div>
</div>
</div>
The way I see it, there is nothing wrong in your code, that should definitely work, but I would suggest you to try CI's redirect() method in your delete function
public function deleteMerk($id){
$this->modelAdminMerk->deleteMerk($id);
redirect('controllername/merk');
}

Resources