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
Related
I'm trying to get data from my DB using a model, to then be used in the view. I return the query results to my controller and it gives me the undefined variable notice.
I tried first tried performing a select(get) statement in the controller, I then defined the result array as row before defining the specific row to be used, to which I pass on to my view. That threw an error, then I tried the same thing but with a model and a return to the controller:
controller.php
public function Home()
{
$this->load->model('Main');
$this->Main->getresults();
$this->load->view('header', $data);
}
model.php
public function getresults() {
$query = $this->db->get('table');
foreach ($query->result_array() as $row) {
$data = array(
'column' => $row["column"]
);
}
return $data;
}
view.php
<?php echo $column; ?>
I expect the return of $data to the controller for it to be used in the view, yet it still throws a notice of an undefined variable.
In your controller you don't assign and send data to view.
Change your code there:
public function Home()
{
$this->load->model('Main');
$data = $this->Main->getresults();
$this->load->view('header', $data);
}
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; ?>
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');)
My Controller:
public function contact()
{
$data =array();
$this->load->model('contentModel');
$data['reachus']=$this->contentModel->reachusviewModel();
$data['socialnetworks']=$this->contentModel->socialnetworkviewModel();
$data['background1']=$this->contentModel->contactbackgroundviewModel();
$result=$this->load->view('contact',$data);
/*$this->load->view('contact');*/
}
my model:
public function contactbackgroundviewModel()
{
$this->db->select("*");
$this->db->from('contactbackground');
$query = $this->db->get();
return $result=$query->result();
}
My view:
<?php
foreach ($background1 as $row)
{
echo $row->image;
}
?>
My requirement is I have only just one value to be printed. how should I avoid foreach, how the data should be printed in the view?
thanking in advance
Use ->row(). This function returns a single result row
$this->db->select("*");
$this->db->from('contactbackground');
$query = $this->db->get();
return $result=$query->row(); // return only single row
In view
echo $background1->image; // without use of foreach loop
Change the line:
return $result=$query->result();
to
return $result=$query->row_array(); //only the first row will be returned and the return data will be array.
or
return $result=$query->row(); //only the first row will be returned and the return data will be object.
Try this:
use
return $result=$query->first_row();
instead of
return $result=$query->result();
in your model
Online Codeigniter Docs:
https://ellislab.com/codeigniter/user-guide/database/results.html
You can use print_r($array).
This will output all the array
Documentation here
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!