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; ?>
Related
I was working with an array which I have to list in a view page. so I iterated the array using a foreach loop. but it's not displaying any data and throwing an error like
trying to get property of non-object
I have tried the normal way but didn't get it
<div class="scrollbar1 table-content">
<?php
foreach($items_list as $row)
{
?>
<div class="table-row">
<div class="row list_row_style color_black common_rows">
<div class="col-md-12 no_padding">
<a href="add_ledger_sub_type?ledger_name=<?php echo $row->ledger_name; ?
>">
<div class="row">
<div class="col-md-2 list_common_text_style table-data">
<?php echo $i; ?></div>
<div class="col-md-3 list_common_text_style table-data" >
<?php if(!empty($row->sub_ledger_name)) echo $row->sub_ledger_name; ?>
</div>
<div class="col-md-3 list_common_text_style table-data" >
<?php if(!empty($row->ledger_name)) echo $row->ledger_name; ?>
</div>
<div class="col-md-4 list_common_text_style table-data" >
<?php if(!empty($row->description)) echo $row->description; ?>
</div>
</div></a>
</div>
</div>
</div>
<?php
$i++;
}
?>
</div>
My expected result is this code lists all items in '$items_list' array. But it's not
Please help me.
can you put $row["ledger_name"] instead of $row->ledger_name
Yes, I got the answer. It was a small mistake. I took the items to an array in database_models and again stored to another array in 'controller'.
Currently trying to figure out how to update existing SQL data in cake php. I have my input fields being automatically filled with preexisting data but whenever I hit save to change existing data it does nothing. NO error or warning message.
Here is my Controller:
public function settings()
{
$user = $this->UserAuth->getUser();
$userid = $this->UserAuth->getUserId();
//$userEntity = $this->Push->find()->where(['user_id = ' => $userid])->order('time')->toArray($this->request->data, ['validate'=>false]);
//$query = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
$userEntity = $this->Push->find('all', ['conditions' => ['Push.user_id' => $userid]])->last();
if($this->request->is('patch', 'post', 'put')) {
$errors = $userEntity->errors();
if($this->request->is('ajax')) {
if(empty($errors)) {
$response = ['error'=>0, 'message'=>'success'];
} else {
$response = ['error'=>1, 'message'=>'failure'];
$response['data']['push'] = $errors;
}
echo json_encode($response);exit;
} else {
if(empty($errors)) {
if(!empty($this->request->data($userEntity['iosapikey']))) {
$userEntity['iosapikey'] = $this->request->data['Push']['iosapikey'];
}
if(!empty($this->request->data['Push']['androidapikey'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['androidapikey'];
}
if(!empty($this->request->data['Push']['restapikey'])) {
$userEntity['restapikey'] = $this->request->data['Push']['restapikey'];
}
if(!empty($this->request->data['Push']['segments'])) {
$userEntity['iosapikey'] = $this->request->data['Push']['segments'];
}
if(!empty($this->request->data['Push']['tags'])) {
$userEntity['androidapikey'] = $this->request->data['Push']['tags'];
}
if(!empty($this->request->data['Push']['deeplinks'])) {
$userEntity['restapikey'] = $this->request->data['Push']['deeplinks'];
}
if($this->Push->save($userEntity, ['validate'=>false])) {
$this->Flash->success(__('Push settings have been updated successfully'));
//$this->redirect(['action'=>'index', 'page'=>$page]);
} else {
$this->Flash->error(__('Unable to update push settings, please try again'));
}
}
}
} else {
if(!empty($userEntity['iosapikey'])) {
$userEntity['iosapikey'] = $userEntity['iosapikey'];
}
if(!empty($userEntity['androidapikey'])) {
$userEntity['androidapikey'] = $userEntity['androidapikey'];
}
if(!empty($userEntity['restapikey'])) {
$userEntity['restapikey'] = $userEntity['restapikey'];
}
if(!empty($userEntity['segments'])) {
$userEntity['segments'] = $userEntity['segments'];
}
if(!empty($userEntity['tags'])) {
$userEntity['tags'] = $userEntity['tags'];
}
if(!empty($userEntity['deeplinks'])) {
$userEntity['deeplinks'] = $userEntity['deeplinks'];
}
}
$this->set(compact('userEntity'));
$this->set('_serialize', ['push']);
}
This is my template:
<div class="panel-body">
<?php echo $this->element('Usermgmt.ajax_validation', ['formId'=>'editPushForm', 'submitButtonId'=>'editPushSubmitBtn']); ?>
<?php echo $this->Form->create($userEntity, ['id'=>'editPushForm', 'class'=>'form-horizontal', 'type'=>'text']);?>
<div class="form-group col-md-9 col-sm-10">
<label>iOS API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.iosapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Android API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.androidapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>REST API Key:</label>
<div class="">
<?php echo $this->Form->input('Push.restapikey',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Segments:</label>
<div class="">
<?php echo $this->Form->input('Push.segments',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Tags:</label>
<div class="">
<?php echo $this->Form->input('Push.tags',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group col-md-9 col-sm-10">
<label>Deeplinks:</label>
<div class="">
<?php echo $this->Form->input('Push.deeplinks',['type'=>'text', 'label'=>false, 'div'=>false, 'class'=>'form-control']);?>
</div>
</div>
<div class="form-group">
<div class="col-md-offset-3 col-sm-offset-2 col-sm-3">
<?php echo $this->Form->Submit(__('Save'), ['div'=>false, 'class'=>'btn btn-primary pushbutton', 'id'=>'editPushSubmitBtn']); ?>
</div>
</div>
<?php echo $this->Form->end();?>
I'm sure I have a small error somewhere I just haven't spotted yet, but having put many hours into this with no positive result I thought it was time to find some help.
Cake PHP find function returns an object so i would request you to use a statement like $userEntity->iosapikey = $this->request->data['Push']['iosapikey']; to assign values and also enable debuging in cakephp https://book.cakephp.org/3.0/en/orm/retrieving-data-and-resultsets.html#debugging-queries-and-resultsets
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');
}
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);
I have been developing an application in Yii framework. At this point, I fall in an issue that is, I have an Order form where I select a registrant (all registrants come from database) from a dropdown, an item (all product items come from database) from dropdown and a input textbox where I type the quantity. There is a "ajaxSubmitButton" button to send all these values to the controller "actionCart" using Ajax. After receiving all values in the controller, I want to put all values in session variable. When I add another new item, the session values are being replaced what I want to hold all newly added items into the session variable. In this circumstance, what should I do. Please help me. I am giving my code snippets below:
in form:
<div class="form">
<?php
$form=$this->beginWidget('CActiveForm', array(
'id'=>'order-form',
'enableAjaxValidation'=>false,
'htmlOptions'=>array('class'=>'form-horizontal' , 'enctype'=>'multipart/form-data', ),
)); ?>
<div class="alert alert-info" xmlns="http://www.w3.org/1999/html">
<p class="note">Fields with <strong><span class="required">*</span></strong> are required.</p>
</div>
<?php echo $form->errorSummary($model); ?>
<div class="form-group">
<?php echo $form->labelEx($model,'registration_id', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Registration::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'registration_id',$data,array('class' => 'form-control chzn-select','prompt'=>'Select a Registrant'));
?>
</div>
<?php echo $form->error($model,'registration_id'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'item', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-8">
<?php
$data = CHtml::listData(Products::model()->findAll(),'id', 'name');
echo $form->dropDownList($model,'item',$data, array('class'=>'form-control chzn-select' , 'id'=>'item', 'prompt'=>'Select an Item')); ?>
<?php
?>
</div>
<?php echo $form->error($model,'item'); ?>
</div>
<div class="form-group">
<?php echo $form->labelEx($model,'quantity', array('class' => 'control-label col-lg-4')); ?>
<div class="col-lg-2">
<?php
echo $form->textField($model,'quantity',array('class' => 'form-control','size'=>60,'maxlength'=>11));
?>
</div>
<?php echo $form->error($model,'quantity'); ?>
</div>
<div class="form-group">
<div class="col-lg-8 pull-right">
<?php
echo CHtml::ajaxSubmitButton('Add to Cart',Yii::app()->createUrl('admin/order/cart'),
array(
'type'=>'POST',
'update'=>'#cartResult',
),
array('class'=>'btn btn-primary btn-sm',));
?>
</div>
</div>
<div class="form-group">
<div id="cartResult" class="col-lg-12">
</div>
</div>
<?php $this->endWidget(); ?>
</div>
In controller:
public function actionCart()
{
if(isset($_POST["Order"])){
$item = $_POST["Order"];
$registration_id = $item["registration_id"];
$productId = $item["item"];
$quantity = $item["quantity"];
$quantity = $item["quantity"]=='' ? 1 : $item["quantity"];
$productInfo = Products::model()->findByPk(array('id'=>$productId));
$totalPrice = $productInfo->price * $quantity;
$session = Yii::app()->session;
$session['cart'] = array("product_id" => "$productId" , "product_name" => "$productInfo->name", "quantity" => "$quantity","price" => "$productInfo->price");
}
}