codeigniter invalid argument supplied for foreach() on join clause - codeigniter

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();
}

Related

Can't make carousel work in php: CodeIgniter

I have added a carousel in a container but it remains static showing the first pic. Though the carousel buttons are visible but not active. The code is:
<div class="col-xl-8 col-lg-8 col-md-7 mb-5 mb-md-0">
<div id="" class="pt-0 pt-md-4 pt-lg-5">
<div id="carouselExampleIndicators" class="carousel slide" data-ride="carousel">
<ol class="carousel-indicators">
<?php if (!empty($event)) {
foreach ($event as $key => $value) {
?>
<li data-target="#carouselExampleIndicators" data-slide-to="<?= $key ?>" <?php if ($key == 0) { ?> class="active" <?php } ?>></li>
<?php }
} ?>
</ol>
<div class="carousel-inner">
<?php if (!empty($event)) {
foreach ($event as $key => $value) {
$date = date('d', strtotime($value['datetime']));
$month = date('F', strtotime($value['datetime']));
$year = date('Y', strtotime($value['datetime']));
$seconds = strtotime($value['datetime']) - time();
$days = floor($seconds / 86400);
$seconds %= 86400;
$hours = floor($seconds / 3600);
$seconds %= 3600;
$minutes = floor($seconds / 60);
$seconds %= 60;
$cdate = date("Y-m-d H:i:s", strtotime($value['datetime']));
if ($cdate > date("Y-m-d H:i:s")) {
?>
<div class="carousel-item <?php if ($key == 0) { ?>active<?php } ?>">
<div class="row">
<div class="col-md-12 col-lg-6 mb-4 mb-md-0">
<div class="d-flex align-items-center justify-content-between w-100">
<!--<div class="images-craus">-->
<!-- <img src="<?= base_url() ?>/images/upcomingevents/<?= $value['image1'] ?>">-->
<!--</div>-->
<div class="dates">
<h3 class="months"><?= $month ?> <?= $date ?><br><?= $year ?></h3>
<h3 class="team"><?= $cdate ?> <br><?= $value['team1'] ?> vs <?= $value['team2'] ?></h3>
</div>
<!--<div class="images-craus">-->
<!-- <img src="<?= base_url() ?>/images/upcomingevents/<?= $value['image2'] ?>">-->
<!--</div>-->
</div>
</div>
<div class="col-md-12 col-lg-6 mb-4 mb-md-0">
<div class="details1">
<div class="pt-0 pt-md-4 pb-3">
<h5 class="text-white text-center">Next Match<br><?= $value['team1'] ?> vs <?= $value['team2'] ?></h5>
</div>
<div class="button-20 d-flex">
<button type="button" class="btn btn-danger"><?= $days ?> day</button>
<button type="button" class="btn btn-danger"><?= $hours ?> hr</button>
<button type="button" class="btn btn-danger"><?= $minutes ?>min</button>
</div>
</div>
</div>
</div>
</div>
<?php }
}
} ?>
</div>
I tried looking at bootstrap feature but couldn't really work it out. Mind you this carousel also have a table list which shows on the right side of the container. I am not sharing it code as table is working fine.

how to add show data on modal for individual id?

I want when user click on "more detail href". they should get this detail on modal the box.
<ul class="list-tickets">
<?php foreach ($searchFlights as $searchFlight) {
//One Way Logic
$stopInfo_O = "";
$stopCount_O = 0;
$AirlineLogo_O = $this->search_model->getAirlinelogo($searchFlight->airline_idO);
$finalDepartdate_O = $searchFlight->departure_dateO;
$DepartAirpot_O = $this->search_model->getAirportCode($searchFlight->depart_airportidO);
$totalFlightTime_O = $searchFlight->total_flighttimeO;
$finalArrivaldate_O = $searchFlight->arrival_dateO;
$totalBaggage_O = $searchFlight->baggageO;
$stopOvertime1 = $searchFlight->stopover1_staydurationO;
$className0 = $searchFlight->classNameO;
$ArrivalAirpot_O = $this->search_model->getAirportCode($searchFlight->arrival_airportidO);
$flightPrice = $searchFlight->flightPrice;
if($searchFlight->operatedby_airlineidO == "0") {
$operatedAirlinename = "";
} else {
$operatedAirlinename = $this->search_model->getAirlinename($searchFlight->operatedby_airlineidO);
}
//Stops Handling Oneway
if(empty($searchFlight->stopover1_airportidO) && empty($searchFlight->stopover2_airportidO)){ $stopInfo_O = "Direct"; }
if($searchFlight->stopover1_airportidO != 0){ $stopCount_O++; }
if($searchFlight->stopover2_airportidO != 0){ $stopCount_O++; }
if($stopCount_O>0){
$stopInfo_O .= $stopCount_O." Stop ";
//Stop oneway 1
$stopInfo_O .= $this->search_model->getAirportCode($searchFlight->stopover1_airportidO);
if($stopCount_O == 2) {
//Stop One Way 2
$stopInfo_O .= ", ".$this->search_model->getAirportCode($searchFlight->stopover2_airportidO);
}
}
?>
<li class="list-item">
<div class="list-item-inner">
<div class="list-item-main">
<!-- Oneway Section -->
<div class="list-item-top">
<div class="list-item-logo">
<img src="<?= base_url(); ?>uploads/<?php echo $AirlineLogo_O; ?>" alt="">
</div>
<div class="list-item-content">
<div class="list-item-content-left">
<!-- Final Departure Dates-->
<div class="text-bold text-base">
<?php echo $finalDepartdate_O; ?>
</div>
<!-- Departure Airport -->
<span class="small reveal-block">
<?php echo $DepartAirpot_O; ?>
</span>
</div>
<div class="list-item-content-line-wrapper small">
<!-- Total Flight Time -->
<div class="list-item-content-line-top">
<?php echo $totalFlightTime_O; ?>
</div>
<div class="list-item-content-line"></div>
<!-- Stops info -->
<div class="list-item-content-line-bottom text-primary">
<p title="<?php echo $stopOvertime1; ?>"><?php echo $stopInfo_O; ?> </p>
</div>
</div>
<div class="list-item-content-right">
<!-- Final Arrival date -->
<div class="text-bold text-base">
<?php echo $finalArrivaldate_O; ?>
</div>
<!-- Arrival Airport -->
<span class="small reveal-block">
<?php echo $ArrivalAirpot_O; ?>
</span>
</div>
</div>
</div>
</div>
<div class="list-item-footer ticket-holder-dt">
<h5 class="text-bold list-item-price"><?php echo COMMON_CurrencySymbol." ".$flightPrice;?></h5>
<span class="ticket-include"></span>
<span class="ticket-type">One way</span>
<a class="btn btn-info btn-xs btn-no-shadow" href="<?= base_url(); ?>searchflights/bookFlight?id=<?php echo $searchFlight->id; ?>">
Book Now
</a>
<br>
<a href="">
More detail <!-- I want when user click on it. they should get this detail on modal the box -->
</a>
<br>
<?php echo $className0; ?> Class
</div>
</div>
<div class="small text-gray-light list-item-subtitle">Operated by <?php echo $operatedAirlinename; ?></div>
<div class="tripbaggage">Baggage: <?php echo $totalBaggage_O; ?></div>
<?php } ?>
You can achieve this by implementing separate modal for each id inside for loop or
by implementing Ajax request to show the detail information of the corresponding id.

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