UPDATED THE SOLUTION::: IT IS WORKING NOW
I have created a controller with join query. Now I want to include controller to actionCreate function and show data into text filed in form.
Controller
public function actionCreate()
{
$model=new Grndetail;
if (isset($_POST['Grndetail'])) {
$model->attributes=$_POST['Grndetail'];
if ($model->save())
$this->redirect(array('view','id'=>$model->id));
}
$grndata = $this->GrnData1();
$model->im_grnnumber = $grndata['im_grnnumber'];
$this->render('create',array('model'=>$model, 'grndata'=>$grndata,));
}
public function GrnData1()
{
$sql = Yii::app()->db->createCommand()
->select('t.im_grnnumber, t.im_purordnum, r.pp_purordnum, r.cm_code, r.pp_quantity, r.pp_unit, r.pp_unitqty, r.pp_purchasrate, p.cm_description, p.cm_code')
->from('im_grnheader t')
->join('pp_purchaseorddt r', 't.im_purordnum = r.pp_purordnum')
->join('cm_productmaster p', 'p.cm_code = r.cm_code')
//->where('id=:id', array(':id'=>$id))
->order('im_grnnumber DESC')
->queryRow();
return $sql;
}
form filed
<?php echo $form->textField($model,'im_grnnumber'); ?>
Is anything I am missing. How can I view the data as value in form when I am creating something.
Thanks in advanced.
UPDATED THE SOLUTION::: IT IS WORKING NOW
You should change your program as
public function GrnData1(){
$sql = Yii::app()->db->createCommand()->setFetchMode(PDO::FETCH_OBJ) // change it
->select('t.im_grnnumber, t.im_purordnum, r.pp_purordnum, r.cm_code, r.pp_quantity, r.pp_unit, r.pp_unitqty, r.pp_purchasrate, p.cm_description, p.cm_code')
->from('im_grnheader t')
->join('pp_purchaseorddt r', 't.im_purordnum = r.pp_purordnum')
->join('cm_productmaster p', 'p.cm_code = r.cm_code')
->order('im_grnnumber DESC')
->queryRow();
Return $sql; // write here return statement
}
Now you can access your data like this
echo $form->textField($model,'im_grnnumber', array('value'=>$grndata->im_grnnumber));
Hope, It will help you.
Thanks
Var $grndata is an array, not a model. So the correct way to call its values should be this:
<?php echo $form->textField($model,'im_grnnumber', array('value'=>$grndata['im_grnnumber']) ); ?>
Or something like that, I'm not sure of the format of the array returned by queryRow(). Maybe print_r($grndata) to see the style of the array.
change actionGrnData1() to GrnData1()
You should complete your textField like this
<?php echo $form->textField($model,'im_grnnumber',
array('value'=>$grndata->im_grnnumber
)); ?>
Related
I am facing problem to join these tables and also know how to make controller code to view data in view file with text boxes.
public function get_order_return_info()
{
$this->db->select('tbl_order_details.*', false);
$this->db->select('tbl_order_details.order_details_id', false);
$this->db->select('tbl_order.order_id', false);
$this->db->select('tbl_product.product_id', false);
$this->db->select('tbl_inventory.product_quantity', false);
$this->db->from('tbl_order_details');
$this->db->join('tbl_order', 'tbl_order_details.order_id = tbl_order.order_id ', 'left');
$this->db->join('tbl_product', 'tbl_order_details.product_code = tbl_product.product_id ', 'left');
$this->db->join('tbl_inventory', 'tbl_product.product_id = tbl_inventory.product_id ', 'left');
$query_result = $this->db->get();
$result = $query_result->result();
return $result;
}
You can use MVC to solve this
//Controller
function get_data() {
$data['list'] = $this->your_model->get_data();
$this->load->view('your_view/location',$data);
}
//Model
function get_data() {
$sql = "your_query";
$list = $this->db->query($sql)->result_array();
return $list;
}
//View
foreach($list as $data) {
echo $data['your_selected_field']; // it could be table,text field,or just text
}
API Reference : https://www.codeigniter.com/user_guide/database/results.html#result-arrays
Hope this helps
First u can create OrderModel to work with order table. Then create function get_order_data.
//Order Model
public function get_orders() {
// your db query to get orders
return $result;
}
Then u can load model in controller using $this->load->model('OrderModel'); Best place is __construct to load model
And call your get_orders() function
// Your controller
// Create data array to store front data
$data = [];
$data['orders'] = $this->OrderModel->get_orders();
After getting $orders pass it to view using following codes
$this->load->view('path/to/view', $data);
Finally u can call your order data in view using $orders
I hope this will help u
You need to set alias to the table to function it properly like this.
$this->db->select('a.*,a.order_details_id, b.order_id ', false);
$this->db->from('tbl_order_details as a');
$this->db->join('tbl_order as b', 'a.order_id = b.order_id ', 'left');
If you want select 1 row only you need to use this function.
$this->db->get('tbl_order_details')->row_array();
or if you want all result row you need to use this.
$this->db->get('tbl_order_details')->result_array();
On your controller you should add this lines to pass your result on your view page.
public function index()
{
$data['orderDetails'] = $this->your_model->get_order_return_info();
$this->load->view('pages/yourview/index',$data);
}
And finally to set your order details to the text boxes. You need to echo it inside the text boxes like this.
<input type="Text" name="orderID" value="<?php echo $orderDetails['order_id'];?>" >
If you use result_array() you need to use foreach loop to echo it.
I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
controller:
public function trainer($id)
{
$user_record = $this->db->query("select * from usr_data where usr_id=$id")->result();
$data['title'] = 'trainer Dashboard';
$data['user_record'] = null;
$data['active_courses'] = [];
$data['inprogress'] = [];
if(count($user_record)) {
$user_record = $user_record[0];
$data['title'] = ucwords($user_record->firstname).' Dashboard';
$data['user_record'] = $user_record;
$active_courses = $this->base_model->getTrainercourseAll($id);
$data['active_courses'] = $active_courses;
$inprogress = $this->base_model->getstaffinprogress($id);
$data['inprogress'] = $inprogress;
}
$this->load->view('trainer-dashboard', $data);
}
model:
public function getstaffinprogress($user_id) {
$result=$this->executeSelectQuery("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
return $result;
}
view:
<h3>Avg inprogress:<?php echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";?></h3>
I want to display the column percentage which is coming from database.above code is in the controller, model and view.i thought my controller code is wrong.
Anyone help me to get rid of this error. I want to display a value in view in CodeIgniter but I am getting several errors like trying to get the property on non-object. I think my code is correct but I am getting errors. Below is my code.
Try this in your view file,
if(isset($inprogress)){
echo $inprogress->percentage;
}
Then your code look like this,
<h3>Avg inprogress:<?php if(isset($inprogress)){ echo "<span style='color:#ff00ff;font-family:verdana;'>".$inprogress->percentage."</span>";}?></h3>
Then call the controller function. I think inprogress is not set at the first time.
If it doesn't work, try to var_dump($inprogress) in controller and check value and type.
And try this code in your model. Query also seems not correct
public function getstaffinprogress($user_id) {
$this->db->select_avg('ut_lp_marks.percentage');
$this->db->where('ut_lp_marks.obj_id', $user_id);
$this->db->where('object_data.type', 'crs');
$this->db->where('ut_lp_marks.status', 1);
$this->db->join('object_data', 'object_data.obj_id = ut_lp_marks.obj_id');
$query = $this->db->get('ut_lp_marks');
return $query->result_array();
}
I assume that your db is ut_lp_marks. Then var_dump array and check data is correct first. Then access array element.
public function getstaffinprogress($user_id) {
$result = array();
$query=$this->db->query("select AVG(m.percentage) from object_data o, ut_lp_marks m where o.obj_id=m.obj_id and o.type='crs' and m.status=1 ");
foreach($query->result() as $row){
$result = $row;
}
return $result;
}
Also check $inprogress->percentage exists before print in view.
I have to table article and table comment. In the home page I want to see how many comment for earch article.
Model
function show_latest_article($start,$display,$cate)
{
$query= $this->db->get_where('news_news', array('News_Cate_ID'=>$cate),$start,$display);
if($cate==0)
{
$query= $this->db->get_where('news_news',$start,$display);
}
return $query->result();
}
function count_comment($id)
{
$query = $this->db->get_where('comment',array('comment_article_id'=>$id) );
return $query->num_rows();
}
Controller
function index() {
$this->load->model('article');
$data=array('article'=>$this->article->show_latest_article(0,8,1),
'sidebar'=>$this->article->side_bar(9),
'count_comment'=> $this->article->count_comment($id),
);
$this->load->view('page/index',$data);
}
In the view I have this
foreach($article as $art)
{
echo $art->title."<br>";
$id= $art->id;
// I want to echo number of comment here.
// Or I want to call function count_comment($id)
}
$this->load->model('article');
$data['article'] = $this->article->show_latest_article(0, 8, 1);
$data['sidebar'] => $this->article->side_bar(9);
$data['count_comment'] => $this->article->count_comment($id);
$this->load->view('page/index', $data);
And it should work.
It's not so clear, where the $id variable come from, but I suggest to join the comments table to your article. It's not a best practice to make queries within loops.
In your model:
public function show_latest_article($ids = array())
{
return $this->db
->select('articles.*, COUNT(comments.id) as comment_count')
->where_in('articles.id', $ids) // You can skip this
->join('comments', 'comments.article_id = articles.id', 'left')
->group_by('articles.id')
->get('articles')
->result();
}
In your controller:
public function index()
{
$data['articles'] = $this->article->show_latest_article(array(0, 8, 1));
$this->load->view('page/index', $data);
}
Just change the field name according to database columns. Also you can skip the where_in condition. (I'm not sure what the three number stands for).
Then in your view, you can simply access the field:
$art->comment_count
EDIT: According to your comment:
$this->db
->select('a.*, (SELECT COUNT(*) FROM comment c WHERE c.comment_article_id = a.News_News_ID ) as counta')
->get('news_news')
->result();
I am writing this answer as you provided code. It could be more simpler if you give more database details. Try below code
function index() {
$this->load->model('article');
$articles = $this->article->show_latest_article(0,8,1);
foreach($articles as $article)
{
$article->comment_count = $this->article->count_comment($article->id);
$all_article[] = $article;
}
$data=array('article'=>$all_article,
'sidebar'=>$this->article->side_bar(9)
);
$this->load->view('page/index',$data);
}
On view
foreach($article as $art)
{
echo $art->title."<br>";
$id= $art->id;
echo $art->comment_count;
}
Controller :
function index() {
$this->load->model('article');
$articles = $this->article->show_latest_article(0,8,1);
$count_comments = Array();
for($i=0;$i<count($articles);$i++){
$count_comments[$i] = $this->article->count_comment($articles->$id);
}
$count_comments =
$data=array('article'=>$articles,
'sidebar'=>$this->article->side_bar(9),
'count_comment'=> $count_comments);
$this->load->view('page/index',$data);
}
In the View :
$i=0;
foreach($article as $art)
{
echo $art->title."<br>";
$id= $art->id;
echo $count_comment[$i];
$i++;
}
Data is passed from the controller to the view by way of an array or an object in the second parameter of the view loading function. Here is an example using an array:
$data = array(
'title' => 'My Title',
'heading' => 'My Heading',
'message' => 'My Message'
);
$this->load->view('blogview', $data);
Also below is the example for object
$data = new Someclass();
$this->load->view('blogview', $data);
PLEASE NOTE: Note: If you use an object, the class variables will be turned into array elements.
You can find out more from below ref URL
Ref: https://ellislab.com/codeigniter/user-guide/general/views.html
First of all in your controller you get count_comment only for specific article ID and not for all articles so you cant do foreach to display comment count of each article.
You need to setup you model better and in model function show_latest_article to use JOIN comments table and do count comment in query.
I will help you with query if you provide me more info about database table of article and comments.
SELECT
article.*,
COUNT(comment.id),0) AS numberOfCommments
FROM article
LEFT JOIN comment
ON comment.article_id = article.id
GROUP BY article.id
Good day all. I'm new to codeigniter and I'm trying to pass a variable from one view page to another. But I get error and after a lot of tries still couldn't figure it out.
This is the Model code:
function get_post($postID){
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->result_array();
}
function update_post($postID, $data)
{
$this->where('post_id', $postID);
$this->db->update('khanposts', $data);
}
This is the Controller code:
function editpost($postID)
{
$data['success']=0;
if($_POST){
$data_post=array(
'fullname'=>$_POST['fullname'],
'dob'=>$_POST['dob'],
'blood'=>$_POST['blood'],
'village'=>$_POST['village'],
'occupation'=>$_POST['occupation'],
'company'=>$_POST['company'],
'email'=>$_POST['email'],
'contact'=>$_POST['contact'],
'password'=>$_POST['pass'],
'marry'=>$_POST['marry']);
$this->khanpost->update_post($postID, $data);
$data['success']=1;
}
$data['post']=$this->khanpost->get_post($postID);
$this->load->view('edit_post', $data);
}
This is the code of View page which passes the value to edit_post view page:
foreach ($posts as $row){
<tr><td><i>Edit</i></td></tr>';
}
This is code of edit_post view page where it must get the value of $row['post_id']:
echo form_open(base_url().'khanposts/editpost/'.$row['post_id']);
echo '<b>Full Name: </b>';
$data_form=array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value'=>$row['fullname'] );
echo form_input($data_form);
How do I assign the passsed variable($row['post_id']) in form_open()? Any solution will be really helpful. Tnx.
As far as I understand the problem;
Pass PostID to view in editpost function:
function editpost($postID)
{
...
$data['postID'] = $postID;
}
Get it in view page like:
echo form_open(base_url('khanposts/editpost/'.$postID));
You should load form helper:
$this->load->helper('form');
And your data_form input:
$data_form = array('name'=>'fullname', 'size'=>30, 'id'=>'fullname', 'class'=>'inputstyle', 'value' => $post['fullname']);
You should use row_array instead of result_array, because of you get single post.
function get_post($postID)
{
$this->db->select()->from('khanposts')->where(array('post_id'=>$postID))->order_by('fullname', 'desc');
$query=$this->db->get();
return $query->row_array();
}
//model
function shop_dropdown()
{
$this->db->select('shop');
$this->db->from('shop');
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name'];
}
return $data;
}
controller//
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop['select_options'] = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
view//
<?php
echo form_dropdown('shop', $select_options);
?>
this is not not working.please help me creating a drop downlist from database.if you can write a new code.
thanks in advance
Modify like this
function shop_dropdown()
{
$data = array();
$this->load->model('shop_model');
$shop = $this->shop_model->shop_dropdown();
$this->load->view('shop/product_view', $shop);
}
and in your view
echo form_dropdown('shop', $shop->option);//option is an value taking form database
that's it.accept answer if it useful for you
I am not sure if you have autoload form helper, if you didn't, you can't use the form_dropdown function unless you load it in the controller. I don't see you load form helper anywhere.
http://codeigniter.com/user_guide/helpers/form_helper.html
you are selecting 'shop' column in your model.
I think your model should be like this
function shop_dropdown()
{
$this->db->select('id,name'); //column names you want to select, can be optional if you want to select all columns.
$this->db->from('shop'); //table name, required
//$this->db->where('category_online', 1);
$query = $this->db->get();
foreach($query->result_array() as $row)
{
$data[$row['id']]=$row['name']; //make sure 'id' and 'name' ,columns are present in table
}
return $data;
}
And I hope you have edited application/config/databse.php