Pass all array elements to view from controller in codeigniter - php

I want to pass an array to a view from a controller. I tried to use the code below. I know it is wrong, but cannot think of what to use. find() function gets all rows from table, and then i want to pass those rows as array to the view. How can i do so?
<?php
class Blog extends CI_Controller{
public function __construct(){
parent::__construct();
$this->load->model('blog_model');
}
public function index(){
$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
}
public function create(){
$this->blog_model->create();
}
public function delete(){
}
}
?>

$data = $this->blog_model->find(); //which gets all entries from table
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
Should be:
$data = array('myvar' => $this->blog_model->find());
$this->load->view('template/header');
$this->load->view('template/content', $data);
$this->load->view('template/footer');
Then access it in your view with:
$myvar
Please see this for a thorough explanation.

Related

Codeigniter Fetch data

I'm trying to fetch de data from my database and show it in my view.
But I don't seem to get it right. I worked a bit with codeIgniter and I know you can display your data in the view in a way that goes something like this:
{data}{dataname}{/data}
And this would show you every record of the dataname.
This is my model:
public function __construct()
{
$this->load->database();
}
public function Get_Pedras()
{
$query=$this->db->query("SELECT * FROM pedras;");
$row = $query->row_array();
return $row;
}
And this is my controller
public function __construct()
{
parent::__construct();
$this->load->model('Pedras_Model');
$this->load->helper('url_helper');
}
public function index (){
$this->load->view('Main/Formulario_Email');
}
public function GetStone(){
$this->load->library('parser');
$name = $this->Pedras_Model->Get_Pedras();
$data = array(
'title' => 'IdPedra',
'heading' => 'NomePedra'
);
$this->load->view('Main/Formulario_Email', $data);
}
And at least my view:
<div class="col-sm-6 col-sm-offset-1">
{data}
{title}
{/data}
</div>
I think I might be missing something realy important, but I only worked a little bit with CI, and some time ago, could you be helping me out pls? Thanks in advance!
In your Model don't use row_array because you are querying all the available data from your table. use result_array instead.
Like this...
public function Get_Pedras(){
$query=$this->db->query("SELECT * FROM pedras;");
return $query->result_array();
}
Try this one in your Controller...
public function GetStone(){
$this->load->library('parser');
$data['pedras'] = $this->Pedras_Model->Get_Pedras();
$this->load->view('Main/Formulario_Email', $data); //pass data here!
}
then in your view, in order to display the data do this...
foreach($pedras as $p){
echo $p['data']; //rename data with you query_name.
}

How to send data from controller to model with CodeIgniter through functions

I want to pass data from Controller to model.But I am unable to fetch it at the model side in CI.Kindly help me out.
Here is my controller function:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
$data['h'] = $this->Chronicles_model->show_seminar();
//return the data in view
$this->load->view('chronicles', $chronicle_num);
}
And here is my model:
public function show_seminar($chronicle_num) {
echo $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
Its because your not passing any value to your model.
CONTROLLER
function show_chronicles($chronicle_num)
{
$this->load->database();
$this->load->model('Chronicles_model');
$data['h']=$this->Chronicles_model->show_seminar($chronicle_num);
$this->load->view('chronicles', $chronicle_num);
}
and you need to return the result() of your query
MODEL
public function show_seminar($chronicle_num = NULL)
{
return $this->db
->get_where('chronicles', array('chronicles_no' => $chronicle_num))
->result();
}
Controller:
function show_chronicles($chronicle_num) {
$this->load->database();
//load the model
$this->load->model('Chronicles_model');
//load the method of model
// pass it to model
$data['chronicle_num'] = $this->Chronicles_model->show_seminar($chronicle_num);
//return the data in view
$this->load->view('chronicles', $data);
}
And here is my model:
public function show_seminar($chronicle_num) {
return $chronicle_num;
//$this->db->select('*');
//$this->db->where('chronicles_no',$chronicle_num);
//$query1 = $this->db->get('chronicles');
//return $query1;
}
You just forgot to pass $chronicle_num as a parameter when you called $this->Chronicles_model->show_seminar();.
So the right way is $this->Chronicles_model->show_seminar($chronicle_num);
You can add as many parameters as you wish to the functions. Example:
public function show_seminar($chronicle_num, $param2, $param3) {
//Login here
}
Just remember to pass the parameters every time you call the function.

I can't get data and post it in my html

model
public function getPost(){
$this->db->select('post_title', 'post_auth', 'post_content');
$this->db->from('post');
$query = $this->db->get();
return $query->result_array();
}
controller
public function index(){
$this->load->model('swmodel');
$data['posts'] = $this->swmodel->getPost();
$this->load->view('dashBoard', $data);
}
How can I transfer data in my view?
Honestly I can't say you why, but for some reason in CodeIgniter 3 you have to store the result or return of your function in a variable and then that variable assign it within the array based value you pass to your view as $this->load->view('dashBoard', $data);.
You need to check does exist a view called dashBoard? and where is it?
So you can try with something like:
public function index() {
$this->load->model('posts_model');
// Get your posts, store them in $posts.
$posts = $this->posts_model->getPost();
// Assign your $posts to your $data.
$data['posts'] = $posts;
// Pass your $data when you load the view.
$this->load->view('dashBoard', $data);
}
And then in your view you can easily access to the passed data as $posts.
# dashBoard.php
<?php var_dump($posts); ?>

How to pass Codeigniter controller array to view and use foreach function?

I can't access codeigniter controller array values in the view. where did i miss.
mymodel
function get_data(){
$this->db->select('prod_id, content, details');
$query = $this->db->get('tblesample');
return $query->result();
}
controller
public function index(){
$this->load->model('mymodel');
$user_info = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $user_info);
}
data_view
<div class="col-md-9">
<?php
foreach ($user_info as $info) {
echo $info->content;
}
?>
</div>
In controller change the code.
public function index(){
$this->load->model('mymodel');
$data['user_info'] = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $data);
}
You be better off using associative array to pass on to the view:
public function index(){
$this->load->model('mymodel');
$data = [
'user_info' => $this->mymodel->get_data(),
];
$this->load->view('inc/header_view');
$this->load->view('data_view', $data);
}
And reference it like $user_info in your view.
You are not passing the array correctly to view . Pass your data like this
public function index(){
$this->load->model('mymodel');
$user_info['user_info'] = $this->mymodel->get_data();
$this->load->view('inc/header_view');
$this->load->view('data_view', $user_info);
}

How to get and display database table value in codeigniter error was coming

I am a new codeigniter developer and have encountered an error. I have stored some value in database and want to display it in view page. Help me please.
Controller
function get_user_value() {
$data['query'] = $this->user_m->user_data_set($query);
$this->load->view('user_settings', $data); }
Model
public function user_details()
{
// $query = $this->db->select('*')->from('user')->get();
$query = $this->db->get('user');
return $query->$result();
}
Try this:
Note call first the model to your controller
add first this function _construct()
function __construct() {
parent::__construct();
$this->load->model("model_file_name", "model_name_alias");
//Example $this->load->model("user_model","user");
//the "user_model" is the file name in your model folder
}
function get_user_value() {
$data['query'] = $this->model_name_alias->user_details();
$this->load->view('user_settings', $data);
}

Categories