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.
}
Related
I'm having issues passing data from my controller to my view. I have a contenteditable div that I am saving the contents of to my database; I want to then display that in the view.
My controller
{
function __construct(){
parent::__construct();
$this->load->database();
$this->load->model('view_data');
$data = array();
}
public function index(){
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data);
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
}
My model
<?php
class View_data extends CI_Model{
function __construct(){
parent::__construct();
}
public function loadData(){
$this->db->where('contactMe');
$query = $this->db->get('content');
return $query->result_array();
}
}
My view
<div class="w-100" ondblclick="makeEditable(this)" onblur="makeReadOnly(this)" contenteditable="true" id="contactMe">
<?php
foreach($contactMeData as $contact){
echo $contact;
}
?>
</div>
<button id="editBtn" type="submit" value="">Save changes
</button>
Every time I reload the page it replaces the data already in the database and I just get a blank div. I'm unable to view the data that was in the database to begin with.
try to add parameter on where
$this->db->where('contactMe',$parameter);
try to add field name on your view
echo $contact['field_name'];
This might be because every time you open the URL it updates the table, so you need a condition to check if POST request was made or not and then update the column
public function index(){
if($this->input->post()){ // check for post request
$data['contactMe'] = $this->input->post('data');
$this->db->update('content', $data); // try writing db queries in model
}
$results = $this->view_data->loadData();
$data['contactMeData'] = $results;
$this->load->view('pages/home', $data);
}
See, if it helps you.
I'm new in codeigniter, I'm having a challenge with num_rows on view page
Here's the sample of the code - Updated
Model
public function __construct()
{
parent::__construct();
}
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function __construct()
{
parent::__construct();
$this->load->helper('url');
$this->load->model('Referrals_form_model', 'referral'); /* LOADING MODEL * Referral_form_model as referral */
}
public function index()
{
$this->load->helper('url');
$this->load->view('referrals_form_view');
}
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
}
View
<span><?php
echo $query->num_rows();
?></span>
when i run the code, it tell me undefined variable "query"
Please help.
Thank you
Please change your code as below.
MODEL:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller
public function referral_in()
{
$data['total_referred_in'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
View :
<span>
<?php
echo $total_referred_in;
?>
</span>
As you can see in Controller line :
$this->load->view("referral_form_view", $data);
$data here is the data array you are passing to your view. In your view you can access data in $data variable by using $data variable's KEY as php variable(In our case its "total_referred_in").
You aren't returning anything from your function, you're just echoing. Try this:
public function total_referred_in()
{
$query = $this->db->query('SELECT * FROM daily_out_patient');
return $query->num_rows();
}
Controller:
public function referral_in()
{
$data['num_rows'] = $this->load->referral->total_referred_in(); //get referral data
$this->load->view("referral_form_view", $data);
}
And then in your view:
<span><?=$num_rows?></span>
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);
}
I am building my comment module and trying to get the active records to display on view. Seems simple however I have a lot going on and using a separate model and controller then what the view is being generated from.
So I have a profile page ( where I'm trying to get the results to display )
Controller:
public function profile()
{
$this->load->helper('url'); //Include this line
$this->load->helper('date');
$this->load->library('session');
$this->load->library('form_validation');
$session_id = $this->session->userdata('id'); //Ensure that this session is valid
//TRYING TO MAKE A VARIABLE WITHT THE $_GET VALUE
$user_get = $this->uri->segment(3); // Modify this line
// LOAD THE CORRECT USER
$this->load->model('account_model');
$user = $this->account_model->user($user_get); //(suggestion) you want to pass the id here to filter your record
$data['user'] = $user;
$data['session_id'] = $session_id;
if($user_get != $user['id'] || !$user_get)
{
$data['main_content'] = 'account/notfound';
$this->load->view('includes/templates/profile_template', $data);
}
else
{
if($user_get == $session_id) //Modify this line also
{
$data['profile_icon'] = 'edit';
}
else
{
$data['profile_icon'] = 'profile';
}
$sharpie = $this->sharpie($user);
$data['sharpie'] = $sharpie;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/profile_template', $data);
}
}
Now I have a new controller for my comments I'm trying to display:
public function airwave() {
$this->load->helper('date');
$this->load->library('session');
$airwave_get = $this->uri->segment(3);
$this->load->model('community_model');
$airwave = $this->coummunity_model->airwave($airwave_get);
$data['airwave'] = $airwave;
$data['main_content'] = 'account/profile';
$this->load->view('includes/templates/main_page_template', $data);
}
with this model:
public function airwave($id=null)
{
if(is_null($id)) {
$session = $this->session->userdata('is_logged_in');
$commenter_id = $this->session->userdata('id');
}else{
$commenter_id = intval($id);
}
$query = $this->db->query("SELECT * FROM airwaves_comments WHERE from_id=$commenter_id");
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
//above returns the single row you need to the controller as an array.
//to the $data['user'] variable.
}
}
and trying to display it on this view ( which is generated by the profile function )
<div class="profile_airwave_comment_text">
<?php echo $airwave['comment'];?>
</div>
I just can't seem to get it to pass the array variable I've created properly to that view?
thanks in advance.
The first glaring thing I see wrong is in you model's airwave function:
if($query->num_rows()==1)
{
$data = $query->result_array();
return $data[0];
You are assuming that there will only be one result in your query, so if there are more or less than one, your $data array will bever be set.
Also, even then you are only returning the first element in the array. I am not seeing anywhere in your question that you want to return only one.
Finally, in your view, if you now return the full array, and not only one element, you will have to do a foreach statement.
EDIT: please see suggested solution.
In your model, don't worry about the check for the amount of data in the result array. This will come later. Simply do:
return $query->result_array();
Now, keep your controller as is, but adapt your view:
<div class="profile_airwave_comment_text">
<?php
if (isset($airwave) && is_array($airwave) && !empty($airwave))
{
foreach ($airwave as $wave)
{
echo $wave['comment'];
}
}
else
{
echo 'No comments found...';
}
?>
</div>
This should work, and if not, at least let you know that no comments were fond, and thus check your database.
HTH!
If your using HMVC, simply call the module from inside the profile view, ie:
echo Modules::run('comments/profileComments', $user->id);
Else:
You would need to create a new method in the loader class, to load in the controller seperate from routing.
If your really after a quick fix until you come up with a better alternative, you could(depending if both controllers live in the same directory) just include the 'comments' controller in your 'profile' controller and try something like this:
{Cons: CI Pagination wont work!}
application/controllers/profile.php
include 'Comments' . EXT;
class Profile extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function index( $user_id ){
$commentsPartialView = call_user_func_array(array(new Comments(), "getProfileComments"), array($user_id));
return $this->load->view($this->template, array(
'view' => 'profiles/_index',
'comments' => $commentsPartialView
));
}
}
-
application/controllers/Comments.php
class Comments extends CI_Controller{
public function __construct ()
{
parent::__construct () ;
}
public function getProfileComments( $user_id ){
$user_comments = ''; //pull from DB
//set third param to TRUE for partial view, without main template
return $this->load->view('comments/show', array(
'user_comments' => $user_comments
), TRUE);
}
}
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.