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

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>

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.

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

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

setting up categories in view in 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 ...
}

Resources