Missing argument in codeigniter - 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();

Related

laravel blade - show only one data from database

I have a table with multiple fields called "skills" . how do I show only one(first) data associated with "skills" .
#foreach ($others as $other )
#if ($other->type == 'skills')
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
#endif
#endforeach
in my controller-
public function index()
{
$sliders = Slider::latest()->with('service')->get();
$clients = OurClient::all();
$galleries = Gallery::all();
$services = Service::all();
$others = Others::all();
return view('frontend.pages.home',compact('services','galleries','clients','others','sliders'));
}
this is showing the same data twice. I want only the first data
I solved it. changed the blade file
<div class="row">
<?php $count = 0; ?>
#foreach ($others as $other )
#if ($other->type == 'skills')
<?php if($count == 1) break; ?>
<div class="col-md-6 p-0">
<img src="{{$other->photoOrVideo}}" class="imgbg-col" alt="imghome">
</div>
<div class="col-md-6 centered">
<div class="detailcontent">
<div class="subheading">{{$other->type}}</div>
<div class="heading">{{$other->titleOrName}}</div>
<div class="textdetail">{{$other->description}}</div>
</div>
</div>
<?php $count++; ?>
#endif
#endforeach
</div>

How can I display images in carousel using Codeigniter

I am using CI_3 and successfully inserted images to database and in directory but I can't get the image to display on carousel. How should I properly get the image to display it on a carousel?
(EDITED)
I have this on my Model:
function carousel(){
$this->db->select('*');
$this->db->where('status = 1');
$query = $this->db->get('image');
return $query->result_array();
}
Controller:
public function index()
{
$data['content'] = $this->Home_model->getRows();
$data['title'] = 'Products List';
// Load the list page view
$this->load->view('template/header');
$this->load->view('template/topBarNav');
$image = new Home_model;
$data['images'] = $image->carousel();
$this->load->view('pages/home', $data);
$this->load->view('template/footer');
}
and display it on view like this:
<div class="carousel-inner">
<?php foreach ($images as $row) : ?>
<div class="carousel-item card">
<div class="parent d-flex justify-content-center">
<img id="carousel_image" src="<?php echo base_url('uploads1/'.$row['upload_path']) ; ?>" class="img-responsive">
<div class="carousel-caption">
<h3><?php echo stripslashes(html_entity_decode($row['description'])); ?></h3>
</div>
</div>
</div>
<?php endforeach; ?>
But it doesn't display any images. I only get the folder path of the image and not the actual image.
Every uploaded image will create a folder according to their id, therefore every image are inside the generated folders inside uploads1 where I set the upload path for the images.
Your carousel is not working because your are set every item active that's the issue in this only first item was active only other are automatically active by your carousel .
You can try with removing active in loop make it first active only using condtions
<div class="carousel-inner">
<?php foreach ($images as $i => $row) : ?>
<div class="carousel-item card">
<div class="parent d-flex justify-content-center">
<img src="<?php echo base_url().'tourism/uploads1/'.$row['upload_path'] ?>" class="img-responsive">
<div class="carousel-caption">
<h3><?php echo stripslashes(html_entity_decode($row['description'])); ?></h3>
</div>
</div>
</div>
<?php endforeach; ?>

POST data from input within loop

I have a lot of products from a database. I display them with a loop. When a user clicks "more", I want to display its details. The problem is, for example: I have four products A, B, C, and D. When a user clicks A's details, it displays D's details or randomly. How can I fix this? This is my code:
<form action="details" method="post" enctype="multipart/form-data">
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img class="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
<button type="submit" value="Submit" name="more" class="btn btn-primary ">More</button>
</div>
</div>
</div>
#endforeach
</form>
route
Route::post("details",'UserController#detail');
controller
public function detail(Request $req)
{
$productname = $req->input('imagename');
return $productname;
}
There is no need of using a form to accomplish what you want.
#foreach($images as $image)
<div class="card" >
<div class="card-inside1" >
<div class="center">
<img clas="card-img-top img-fluid" src="<?php echo asset('images').'/'.$image->image1 ?>" alt="Card image" >
</div>
<div class="card-body">
<input type="text" name="imagename" value="{{$image->product_name}}"></input>
More
</div>
</div>
</div>
#endforeach
Route:
Route::get("details/{imagename}",'UserController#detail');
Controller:
public function detail(Request $req)
{
$productname = $req->imagename;
return $productname;
}
UPDATE
The reason why is taking the last product is because you are sending all products through the form. and it takes the last one that sends, not the one you clicked.
view
#foreach($images as $image)
{{ $product->name }}
#endforeach
web.php
Route::get("details/{id}",'ProductController#show')->name('product.show');
ProductController.php
public function show($id)
{
$product = Product::find($id);
return $product
}

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() on join clause

i try all of away that i know and suggestion on internet.
but all time return false.
please help me...
thanks
my error message is:
Severity: Warning
Message: Invalid argument supplied for foreach()
Filename: views/index.php
Line Number: 10
model.php
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
if($query->num_rows() != 0)
{
$result = $query->result_array();
return $result;
}
else
{
return false;
}
}
controllers.php
$data['banner'] = $this->Fornt_model->getBanner();
showbanner.php
<section class="banner">
<div class="container">
<div class="row">
<div class="col-sm-12">
<div id="myCarousel" class="carousel slide" data-ride="carousel">
<!-- Wrapper for slides -->
<div class="carousel-inner" role="listbox">
<?php $item = 1; foreach($banner as $latest_poster): $item_class = ($item == 1) ? 'item active' : 'item'; ?>
<div class="<?php echo $item_class; ?>">
<img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail">
<div class="carousel-caption caption">
<a href="<?php echo site_url('category/bollywood/'.$latest_poster['slug']); ?>" class="box">
<b class="h3"><?php echo $latest_poster['name']; ?></b>
<small class="p">Exclusive AT*<?php echo substr($latest_poster['modify'], 0, 10); ?></small>
</a>
</div>
</div>
<?php $item++; endforeach; ?>
</div>
</div>
</div>
</div>
<div class="clearfix" style="padding: 10px 0;"></div>
<div class="row hidden-xs">
<?php $itemto = 0; foreach($banner as $latest_poster): $item_class_active = ($itemto == 0) ? 'active' : ''; ?>
<div class="col-sm-2 pointer" data-target="#myCarousel" data-slide-to="<?php echo $itemto; ?>"><img src="<?php echo base_url('images/poster/'.$latest_poster['poster']); ?>" class="img-responsive img-thumbnail" /></div>
<?php $itemto++; endforeach; ?>
</div>
</div>
I think something wrong with your query.. try
if($query->num_rows() != 0)
{
$result = $query->result_array();
print_r($result);
}
make sure your array data is available..
If there is no rows, front_model->getBanner() will return the result as false. Due to this you are getting an error. Use following code
public function getBanner()
{
$this->db->select('album.id, album.cat_id , album.poster, album.slug, album.modify, category.id, category.name, category.slug');
$this->db->from('album,category');
$this->db->where('album.cat_id', 'category.id');
$query = $this->db->get();
return $query->result_array();
}

Resources