codeigniter error in foreach passing variable and query - php

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

Related

i want to fetch data from db get by name not by is id by clicking the hyper link

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

Codeigniter select query is not working

I am trying to fetch data from database using CI. But my code is not working.
Showing error :
A PHP Error was encountered Severity: Notice
Message: Undefined variable: h
Filename: confrence/load_Confrence_passes.php
Here is my controller code:
public function load_Confrence_passes()
{
//load the model
$this->load->model('confrence_model');
//load the method of model
$data['h']=$this->confrence_model->confrencepasses();
$this->load->view('mheader');
$this->load->view('confrence/load_Confrence_passes');
$this->load->view('mfooter');
}
And here is my Model code:
public function confrencepasses()
{
$this->db->select('*');
$this->db->where('confrence_cat','Confrence passes');
$query = $this->db->get('confrence');
return $query;
}
And here is my view code:
<div class="row">
<?php
foreach($h->result() as $row)
{
?>
<div class="col-sm-12 semminar-pass reg">
<div class="col-sm-8 border">
<span class="col-sm-12 training-title"><?php echo $row->confrence_title ?></span>
<span class="col-sm-12 training-extra-info"><?php echo $row->confrence_description ?></span>
<div class="training-bar col-sm-9"></div>
<div style="clear:both;"></div>
<span class="col-sm-12 training-price">
<strong><?php echo $row->confrence_price ?> $</strong>, Taxes included</span>
</div>
<div class="col-sm-4 last-block">
<div class="col-sm-6 btn-oa" data-relation="5">
<span class="border-left border"></span>
<span class="border-right border"></span>
<i class="jbt-icon icon-oa"></i>
Learning Objectives
</div>
<div class="col-sm-6">
<a id="btn-atsc-5" class="btn-add-to-shopping-cart " href="javascript:void(0)"
onclick="addTrainingToShoppingCart(5);">
<i class="glyphicon glyphicon-shopping-cart"></i>
<span class="text">Add to cart</span>
</a>
</div>
</div>
<?php
}
?>
This is the first time I am working on CI.
you need to pass $data array to views then you can hold the array in views.
Here is my controller code:
public function load_Confrence_passes()
{
//load the model
$this->load->model('confrence_model');
//load the method of model
$data['h']=$this->confrence_model->confrencepasses()->result();
$this->load->view('mheader');
$this->load->view('confrence/load_Confrence_passes',$data);
$this->load->view('mfooter');
}
In Views
<?php
foreach($h as $row)
{
echo $row->confrence_title;
}
?>
Data is passed from the controller to the view.
Parse data like $this->load->view('content', $data);
public function load_Confrence_passes()
{
//load the model
$this->load->model('confrence_model');
//load the method of model
$data['h']=$this->confrence_model->confrencepasses();
$this->load->view('content', $data);
$this->load->view('mheader');
$this->load->view('confrence/load_Confrence_passes');
$this->load->view('mfooter');
}
Changes in Controller
public function load_Confrence_passes()
{
//load the model
$this->load->model('confrence_model');
//load the method of model
$data['h']=$this->confrence_model->confrencepasses();
$this->load->view('mheader', $data);
$this->load->view('confrence/load_Confrence_passes');
$this->load->view('mfooter');
}
Changes in Model
public function confrencepasses()
{
$this->db->select('*');
$this->db->from('confrence');
$this->db->where('confrence_cat','Confrence passes');
$query = $this->db->get();
if ($query->num_rows() > 0) {
return $query->result_array();
} else {
return array();
}
}
Changes in View
<div class="row">
<?php
foreach($h as $row)
{
?>
<div class="col-sm-12 semminar-pass reg">
<div class="col-sm-8 border">
<span class="col-sm-12 training-title"><?php echo $row['confrence_title']; ?></span>
<span class="col-sm-12 training-extra-info"><?php echo $row['confrence_description']; ?></span>
<div class="training-bar col-sm-9"></div>
<div style="clear:both;"></div>
<span class="col-sm-12 training-price">
<strong><?php echo $row['confrence_price']; ?> $</strong>, Taxes included</span>
</div>
<div class="col-sm-4 last-block">
<div class="col-sm-6 btn-oa" data-relation="5">
<span class="border-left border"></span>
<span class="border-right border"></span>
<i class="jbt-icon icon-oa"></i>
Learning Objectives
</div>
<div class="col-sm-6">
<a id="btn-atsc-5" class="btn-add-to-shopping-cart " href="javascript:void(0)"
onclick="addTrainingToShoppingCart(5);">
<i class="glyphicon glyphicon-shopping-cart"></i>
<span class="text">Add to cart</span>
</a>
</div>
</div>
<?php
}
?>
This may help you..thanks!
Make this way and you'll have certain variable available in all views/partials:
public function load_Confrence_passes()
{
//load the model
$this->load->model('confrence_model');
//load the method of model
$data['h']=$this->confrence_model->confrencepasses();
$this->load->vars($data);// https://www.codeigniter.com/user_guide/libraries/loader.html#CI_Loader::vars
$this->load->view('mheader');
$this->load->view('confrence/load_Confrence_passes');
$this->load->view('mfooter');
}

how to pass Array of object to view ? MVC ..in Codeigniter

I'm not understand how to pass array of object which contain data of table and this is store in one variable when model query return..
so help me...
more time im spend on this error so if any one have idea on this error
Error :
Undefined variable: data Filename: employee/view_Admin_Profile.php
Line Number: 164
at line is <?php echo ($data->sub_dept_name);?>
contoller :
public function update_Admin_Profile($emp_id)
{
$data = $this->Loginm->getEmployeeProfile($emp_id);
$this->load->view('admin/header',$data);
$this->load->view('admin/sidebar',$data);
$this->load->view('employee/update_Admin_Profile',$data);
$this->load->view('admin/footer');
}
view:
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Gendar:</label>
<div class="col-lg-8">
<?php echo ($data->gender);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Department Name:</label>
<div class="col-lg-8">
<?php echo ($data->sub_dept_name);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Role:</label>
<div class="col-lg-8">
<?php echo ($getEmpProfile->role_name);?>
</div>
</div>
</div>
<div class="col-md-12">
<div class="form-group">
<label class="col-lg-4 control-lable">Nationality:</label>
<div class="col-lg-8">
<?php echo ($data->nationality);?>
</div>
</div>
</div>
Model :
public function getEmployeeProfile($emp_id)
{
$this->db->select('*');
$this->db->from('employee e');
$this->db->join('sub_department sd','sd.sub_dept_id = e.sub_dept_id','left');
$this->db->join('department d','d.dept_id = sd.dept_id','left');
$this->db->join('user_role ur','ur.role_id = e.role_id','left');
$this->db->where('e.emp_id',$emp_id);
$query=$this->db->get();
if($query->num_rows() > 0)
{
return $query->row();
}
}
public function update_Admin_Profile($emp_id)
{
//$data = $this->Loginm->getEmployeeProfile($emp_id); Your Code
$data['data'] = $this->Loginm->getEmployeeProfile($emp_id); //Change to this line
$this->load->view('admin/header',$data);
$this->load->view('admin/sidebar',$data);
$this->load->view('employee/update_Admin_Profile',$data);
$this->load->view('admin/footer');
}
and in view you can fetch like the following code:
<?php echo ($data[0]->sub_dept_name);?>
Let's take the example:
You are getting the data from model like,
$data = array('gender' => 'male', 'age' => 22)
Then to access in codeigniter view you need to echo array's key as a variable
example:
<?php echo $gender; ?>

How to pass two different ids of different models from a controller to a view

I am having an issue with passing two ids in blog posts. The first id
displays a single post and the second one counts the number of posts.
Each works well separately but on integrating the two, it selects a
wrong post.
This is my code for the first model that fetches the comments
class News_model extends CI_Model {
function get_comment($id)
{
$this->db->select('comments.*,users.username');
$this->db->from('comments');
$this->db->join('users','users.id = comments.user_id', 'left');
$this->db->where('post_id',$id);
$this->db->order_by('date_added','asc');
$query = $this->db->get();
return $query->result_array();
}
}
This is the model that fetches the posts
class News_model extends CI_Model {
function get_one_news($news_id)
{
$this->db->select('*, news.id as id');
$this->db->from('news');
$this->db->join('users' , 'users.id = news.user_id');
$this->db->where('news.id' , $news_id);
$query = $this->db->get();
return $query->first_row('array');
}
function latestnews() {
$this->db->select("
news.*,users.*,comments.*,COUNT(comments.post_id) AS num_comments");
$this->db->from('news');
$this->db->join('users' , 'users.id = news.user_id');
$this->db->join('comments' , 'comments.post_id = news.id');
$this->db->group_by('comments.post_id');
$this->db->order_by('news.date_added','desc');
$this->db->limit(4);
$query = $this->db->get();
return $query->result_array();
}
}
And here is my controller
function view($id)
{
$data['news'] = $this->news_model->get_one_news($id);
$data["latest_news"] = $this->news_model->latestnews();
$data['comments'] = $this->m_comment->get_comment($id);
$data['content'] = 'single'; // template part
$this->load->view('includes/template',$data);
}
And finally the view file
<div id="sidebar" class="col-md-3 fix-left">
<!---- Start Widget ---->
<div class="widget wid-vid">
<div class="heading">
<h4>Latest News</h4>
</div>
<div class="content">
<div class="tab-content">
<div id="most" class="tab-pane fade in active">
<div class="post wrap-vid">
<?php if(count($latest_news)) {
foreach($latest_news as $l_news){ ?>
<div class="zoom-container">
<div class="zoom-caption">
</div>
<img src="<?php echo base_url('assets/images/'.$l_news['image'])?>"
style="height:80px;width:100px;"/>
</div>
<div class="wrapper">
<h5 class="vid-name"><a href="<?php echo base_url('bulletins/view/'.$l_news['id'])?>"><?php echo
substr(strip_tags($l_news['title']), 0, 15).'..';?></a></h5>
<div class="info">
<h6>By <?php echo $l_news['username']?></h6>
<span><i class="fa fa-calendar"></i><?php echo date( 'F jS, Y' , strtotime($l_news['date_added']))?></span>
<span><i class="fa fa-comment-o">/i><?php echo
$l_news['num_comments'];?></span>
</div>
</div>
<?php }}?>
</div>
</div>
</div>
</div>
</div>
Please check below mentioned solution. You have error in your select cause. When you want data from multiple table in CI and if columnName is same than CI query builder will overwrite value of that column. In this case you need to give alias to columnName. Please check below.
$this->db->select('news.id as newsId,users.id as userId,news.*,users.*,comments.*, COUNT(comments.post_id) as num_comments');
And in your view file you need to change below code.
Find $l_news['id'] and replace with $l_news['newsId']
This will resolve your issue. Let me know if it not works for you.

Add cart without reloading

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.

Categories