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
Related
I am trying to retrieve the data on my wishlist table, for a particular user, so far it only retrieves the first data on the table, just returning one array instead of the three in the table with same user id
public function getWishlistByUserId($id){
$wishlists = Wishlist::where('userId', $id)->get();
foreach($wishlists as $wishlist){
$products = Product::where('id', $wishlist->productId)->get();
return $products;
}
}
It happens because the foreach loop returns a value during the first iteration. Place your return statement outside the loop. Also you could improve your performence by making use of relationships.
An example could be:
// Product.php
public function wishlists()
{
return $this->hasMany(Wishlist::class);
}
// Your method
public function getWishlistByUserId($id)
{
return Product::whereHas('wishlists', function ($query) use ($id) {
$query->where('userId', $id);
});
}
Ideally this is n+1 situation
So i will suggest to use laravel relationship like:
in your whishlist model
public function product(){
return $this->hasMany(Product::class,'productId','id');
}
get data with relationship
public function getWishlistByUserId($id){
$wishlists = Wishlist::with('product')->where('userId', $id)->get();
}
I was finally able to get it working this way, i just pushed the result into an array, and then returned it outside the loop, thanks everyone for your help
public function getWishlistByUserId($id){
$wishlists = Wishlist::where('userId', $id)->get();
$wishlist = [];
foreach($wishlists as $wish){
$product = Product::where('id', $wish->productId)->get();
array_push($wishlist, $product);
}
return $wishlist;
}
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
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'm having a little trouble in retrieving data in multiple tables using codeigniter.
This is the code i'm using to retrieve data in my model which is working well.
function retrieve_experience($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
function retrieve_education($alumni_id)
{
$this->db->select('*');
$this->db->from('education');
$this->db->where('alumni_id',$alumni_id);
$query = $this->db->get();
return $query;
}
Now i tried using a simplified code but fails to display the data. here is the code in my model
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience');
$this->db->join('education','education.alumni_id=experience.alumni_id');
$this->db->where('experience.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
In my controller, i used this code to retrieving data in my model
function display()
{
$alumni_id = $this->session->userdata('alumni_id');
$data['all_data'] = $this->Alumni_model->retrieve_all_data($alumni_id);
$data['main_content'] = 'alumni_home';
$this->load->view('includes/template', $data);
}
and for the display i used this code
foreach($all_data as $results)
{
/** data from experience table **/
$results['company_name'];
$results['company_address'];
/** data from education table **/
$results['school_name'];
$results['field_of_study'];
}
I cant display anything at all. Please help
Try the below code,
I believe you'll want something like this:
function retrieve_all_data($alumni_id)
{
$this->db->select("e.*,edu.*");
$this->db->from("experience e");
$this->db->join("education edu", "edu.alumni_id = e.alumni_id",'left');
$this->db->where('e.alumni_id',$alumni_id);
$this->db->group_by('e.exp_id');
$query = $this->db->get();
return $query->result_array();
}
Hope below mentioned function should return data that you expected.
function retrieve_all_data($alumni_id)
{
$this->db->select('*');
$this->db->from('experience ex');
$this->db->join('education ed','ed.alumni_id=ex.alumni_id');
$this->db->where('ex.alumni_id',$alumni_id);
$query=$this->db->get();
return $query->result_array();
}
I want to insert multiple rows in the database on one click if row is checked using multiple checkbox
Here is my code=>
1)Controller: guard.php
(Here I take a array of list of student id's and pass them one by one to the another function get_leave_data which take id of a student and return more information about the student from another table leave_application).
public function students_out(){
$request=$this->input->post('approve_leave_status');
if($request=="OUT"){
$check_list_out[]=$_POST['check_list_out'];
if(!empty($check_list_out)){
foreach ($check_list_out as $check_list_id) {
$student_data=$this->get_leave_data($check_list_id);
$this->insert_student_outside($student_data);
}
$this->load->helper('url');
$this->load->view('view_guard');
}
}else{
$this->load->helper('url');
$this->load->view('view_guard');
}
} `
public function get_leave_data($id){
$this->load->model('model_guard');
$data=$this->model_guard->get_data($id);
return $data;
}
public function insert_student_outside($std_data){
$this->load->model('model_guard');
$data=$this->model_guard->insert_student_out($std_data);
return $data;
}
2)Model: model_guard.php
(The functions get_data() and get_data2() return more informations about student and function insert_student_out() insert the student to the student_outside table)
public function get_data($id){
$this->db->select('leave_id,leave_from_roll_no,leave_student_name,leave_going_to,leave_from_date,leave_till_date,leave_hostel_no,leave_status');
$this->db->from('leave_application');
$this->db->where('leave_id',$id[0]);
$query=$this->db->get();
$data1=$query->result();
$data2=$this->get_data2($data1);
$final_array=array_merge($data1,$data2);
return $final_array;
}
public function get_data2($array){
foreach ($array as $key) {
$roll_no=$key->leave_from_roll_no;
}
$this->db->select('student_year,student_semester,student_parent_email');
$this->db->from('students');
$this->db->where('student_roll_no',$roll_no);
$query=$this->db->get();
$data=$query->result();
return $data;
}
public function insert_student_out($std_data){
$roll_no=$std_data[0]->leave_from_roll_no;
$id=$std_data[0]->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$std_data[0]->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed
);
if($this->db->insert('students_outside',$array)){
return true;
}else{
false;
}
}
you are trying to insert batch_array using codeignator
the right syntax to using batch_array is:-
$array=array(
'coloum_name'=>$id,
'coloum_name'=>$roll_no,
'coloum_name'=>$date_out,
'coloum_name'=>$inside_date,
'coloum_name'=>$date_allowed
);
if($this->db->insert_batch('students_outside',$array)){
return true;
}else{
false;
}
you can read manual insert_batch here
ok, i am assuming that in $std_data you have more than one record.then in your model's insert_student_out() function try this-
public function insert_student_out($std_data){
foreach($std_data as $row){
$roll_no=$row->leave_from_roll_no;
$id=$row->leave_id;
$date_out=date('Y-m-d');
$inside_date=NULL;
$date_allowed=$row->leave_till_date;
$array=array(
'outside_id'=>$id,
'outside_roll_no'=>$roll_no,
'outside_date_out'=>$date_out,
'outside_date_in'=>$inside_date,
'outside_date_allowed'=>$date_allowed);
$this->db->insert('students_outside',$array);
}
}