How can I display data from model mdl_request inside view using ajax
this is my model:
function get_unit_measurement_by_id($id){
$this->db->from('items', 'item_unit');
$this->db->join('item_unit', 'item_unit.item_unit_id = items.item_unit_id');
$this->db->where('item_unit', $id);
$query = $this->db->get();
return $query->row();
}
this is my view:
<?php
foreach($requests as $request):
echo $this->mdl_request->get_unit_measurement_by_id($request->item_unit);
endforeach;
?>
You can't....In order to display your data straight from that function it needs to be in a controller.
Related
so here is my model codes:
public function GetId()
{
$this->db->select('count(*) as total');
$this->db->from($this->table);
$this->db->where('dibaca', null);
$query = $this->db->get();
return $query->result_array();
}
and this is my code in html:
<?= $DataId['total']; ?>
i already call the function to my controller as DataId,
and i get error that Undefined array key "total"
can you guys tell me what is wrong in it?
Some untested advice:
Your model can be refined to:
public function countNullDibaca(): int
{
return $this->db
->where("dibaca", null)
->count_all_results($this->table);
}
Your controller should call for the model data and pass it to the view.
public function myController(): void
{
$this->load->model('my_model', 'MyModel');
$this->load->view(
'my_view',
['total' => $this->MyModel->countNullDibaca()]
);
}
Finally, your view can access the variable that associates with the first-level key in the passed in array.
<?= $total; ?>
Here is a related post which speaks on passing data from the controller to the view.
Replace the result_array() at the model with
num_rows()
and you can delete ['total'] from your code in html, or like this:
<?= $DataId; ?>
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 trying to retrieve data from datbase given an ID number.
Controller:
Usuario.php
public function seleccionar_por_id_estado(){
$id_estado = $this->input->post('id_estado');
$result = $this->usuario_m->ver_datos_por_id($id_estado);
$data['result'] = $result;
$this->load->view('paginas/administrar_usuarios', $data);
}
My Model
usuario_m.php
public function ver_datos_por_id($id_estado){
$condicion = "estado =" . "'". $id_estado ."'";
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where($condicion);
$query = $this->db->get();
return $query->result();
}
My View
administrar_usuarios.php
<form action="usuario/seleccionar_por_id_estado">
<div class="content well">
<label>Ver por:
<select id="id_estado">
<option>Activo</option>
<option>Inactivo</option>
</select>
<?php
foreach ($result as $u){?>
<?php echo $u->usuario;?> <br>
<?php } ?>
</form>
The error that I'm getting is that the var $result is not defined.
Could you help me, please? Thanks!
In your model.Set condition inside $this->db->where() like this
<?php
public function ver_datos_por_id($id_estado){
$this->db->select('*');
$this->db->from('usuarios');
$this->db->where('estado',$id_estado);//set your condition here
$query = $this->db->get();
return $query->result();
}
OR use $this->db->get_where
$query = $this->db->get_where('usuarios',array('estado'=>$id_estado));
return $query->result();
For more see docs Codeigniter Query Builder
UPDATE
Controller:
public function seleccionar_por_id_estado(){
$id_estado = $this->input->post('id_estado');
$data['result'] = $this->usuario_m->ver_datos_por_id($id_estado);
$this->load->view('paginas/administrar_usuarios', $data);
}
I think issue is in your where condition in model.
use this query
$query = $this->db->get_where('usuarios', array('column name of your table' => $condicion));
First, I'd check to be certain that $id_estado isn't empty.
Another possible problem is that Codeigniter really wants you to capitalize class and file names. I'd try renaming the model to 'Usuario_m.php', name the class in the model Usuario_m, and then load it by calling $this->load->model('Usuario_m); (looks like you forgot to load the model too) and then finally call the model function like this:
$result = $this->Usuario_m->ver_datos_por_id($id_estado);
Update
Your where() parameters are wrong too. Your entire function in the model can be just this:
public function ver_datos_por_id($id_estado){
return $this->db->get_where('usurarios', array('estado' =>$id_estado)->result();
}
The get_where function combines get() and where(), chooses the table with the first parameter (so you don't need $this->db->from('usuarios');)
I have a table in database where I kept keywords/title/description. Now I want to get the data (content) in controller function ::::
how can I get data from database into a function of controller ::::
public function index()
{
$data=array();
$data['title']=" Title "; // need the title dynamic
$data['keywords']="Keywords"; // need the keyword dynamic
$data['keywords']="Description"; // need the description dynamic
}
try this
in controller
public function index()
{
$data=array();
$data['content']=$this->model_name->getContent();
}
in model
function getContent()
{
$data=array();
return $data=$this->db->select('title,keywords,description')->get('table_name')->result_array();
}
This is the general way to fetch data from database. Please let me know if you face any problem.
try this.
$query = $this->db->query("YOUR QUERY");
if ($query->num_rows() > 0)
{
foreach ($query->result() as $row)
{
$data['title'] = $row->title;
$data['Keywords'] = $row->Keywords;
$data['Description']= $row->Description;
}
}
you can use the following variables in your view file
$title, $Keywords, $Description.
Create a model function that will query the database to return your controller the results. You need to learn about CI first. Learn Codeignter first!
In my controller I went from getting the data right there (which worked fine):
$data['query'] = $this->db->query("MY DB QUERY");
$this->load->view('shopci_view', $data);
to grabbing the data from a class model function:
class Shopci_model extends CI_Controller {
function __construct()
{
parent::__construct(); //model constructor
}
//display sale itmes on shopci_view.php
function sale_items()
{
$query = $this->db->query('MY DB QUERY - SAME AS ABOVE');
return $query->result();
}
}
new controller function:
//load model, auto connect to db
$this->load->model('Shopci_model', '', TRUE);
//Display items for sale
$data['query'] = $this->Shopci_model->sale_items();
$this->load->view('shopci_view', $data);
ERROR:
Fatal error: Call to a member function result() on a non-object in
shopci_view
This is the line in the view that worked before I switched to the model (didn't change view):
<?php foreach($query->result() as $row): ?>
Any help is appreciated.
Thank You.
In your model you return the $query as a result() array to the controller.
So you cannot then use 'foreach $query->result()' again - because you have already done that;
This should work
<?php foreach($query as $row): ?>
OR if you want - just change the model from
return $query->result();
to
return $query;
In your model you want to return the data to pass to the controller. So in your model you would have
function sale_items()
{
$query = $this->db->query(SAME AS OLD);
return $query;
}
and then your controller will be the same and your foreach in your view should be the following
<?php foreach ($query as $row): ?>