Get dynamic content from database to controller in Codeigniter - php

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!

Related

I want to get max id value from table in codeigniter

in model file i write this code:
public function maxcode()
{
$this->db->select (max(ClassID)+1 as ClassID)-> from ('class');
$query=$this->db->get();
if($query->num_rows>0)
{
$row = $query->row_array();
return $row;
}
echo $row['ClassID'];
}
in controller function i write.....this code
public function maxcode()
{
$this->insert->maxcode();
}
Now I understand what your problem is.
You need to transfer this value to the template. This is done in the page controller
Example
$data['ClassID'] = $this->insert->maxcode();
$this->view('tenplate Name', $data);
And used in template
<?php echo $ClassID; ?>

Search through name instead of id in code igniter..?

I am a new to php, have this code in controller:
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectquery($id);
$this->load->view('aboutpageview', $data);
}
and this in model:
public function selectquery($id){
$query =$this->db->get_where('phone_mobileinfos',array('id'=>$id));
// return $query->row_array();
return $query;
}
this in view:
{
<p>Mobile_name:<?php echo $h['mobile_name'];?></p>
<p>Price:<?php echo $h['price'];?></p>
}
Just want to search by name instead of id any suggestions..?
In model:
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
use in controller:
$data['by_name'] = $this->aboutmodel->selectquery($id);
in view:
<p>Mobile_name:<?php echo $by_name['mobile_name'];?></p>
<p>Price:<?php echo $by_name['price'];?></p>
So, If I get you correctly and Adding to j.Litvak's code, you want to search the db for a name rather than an id? If so you would need to pass the controller method the name variable i.e. http://localhost/controller_name/method/search_parameter, in your case localhost/controller/selectquery/samsung
Your controller will be
public function selectquery($id){
$this->load->database();
$this->load->model('aboutmodel');
$data['h'] = $this->aboutmodel->selectByName($id);
$this->load->view('aboutpageview', $data);
}
And Model
public function selectByName($name){
$query =$this->db->get_where('phone_mobileinfos',array('name'=>$name));
return $query->result_array();
}
And View
foreach($h as $key => $value){ The code here is obviously in php tags but I cant show them
Mobile: echo $value['mobile_name'];
Price: echo $value['price'];
}
So you create a loop through the result, iterating each result found.
CAVEAT: you will need to test for a NULL result or you will get an error and be careful passing strings through the URL, codeigniter has sql injection protection but I always like to be safe

Codeigniter display relational data directly in view from model

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.

Why I can't retrieve all data given an ID? php-codeigniter

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');)

Code Igniter Count Function

What I want to do is I want to count the total records from the table called "songs_tbl" from my database. So I wrote this function in controller.
private function getHeaderInfo()
{
$total_songs = $songs->count('distinct songs_tbl.song_id');
$this->mysmarty->assign('total_songs',$total_songs);
}
I got this error
Fatal error: Call to a member function count() on a non-object in
Any suggestion ? Thank you.
With Regards,
I think you are looking for:
$this->db->count_all('songs_tbl');
or if you want the distinct in there you will need to do something like this:
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
As there is/was? an issue with using count_all_results() function and DISTINCT
EDIT
I have never used smarty but based on the code in the question I imagine something like this might work, please correct me if I am wrong:
private function getHeaderInfo()
{
$total_songs = get_all_songs();// This function should be called through a model
$this->mysmarty->assign('total_songs',$total_songs);
}
function get_all_songs(){ //THIS SHOULD BE IN A MODEL
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
Edit 2
My suggested layout would be something along these lines (UNTESTED) using CodeIgniter WITHOUT smarty:
Model Song.php
class Song extends CI_Model {
//Constructor and other functions
function count_all_songs(){
$this->db->select('song_id');
$this->db->distinct();
$this->db->from('songs_tbl');
$query = $this->db->get();
return $query->num_rows();
}
}
Controller Songs.php
class Song extends CI_Controller {
//Constructor and other functions
function index(){ //This could be any page
$this->load->model('Song'); //Could be in constructor
$total_songs = $this->Song->count_all_songs();
$this->load->view('songs_index.html', array('total_songs' => $total_songs));
}
}
View songs_index.html
<html><head></head><body>
Total Songs: <?php echo $total_songs ?>
</body></html>
You could query the table and request a count from the table itself, like this:
$result = mysql_query(SELECT count(*) FROM songs_tbl);
Try this
echo $this->db->count_all('songs_tbl');
It permits you to determine the number of rows in a particular table.
you can use this
$query = $this->db->get('distinct');
if($query->num_rows())
{
return $query->num_rows();
}else{
return 0;
}

Categories