i have an problem. I have two tables:
1 called 'Offers'
1 called 'Networks'
In the 'Offers' table i have an field called 'NetworkId' and in my table 'Networks' i have an list of all networks with fild 'Id' and 'Name'.
I have an method in my Model to get all the rows in table 'Offers'. What i want know is how can i get the value of field 'Name' located in table 'Networks' using the 'NetworkID' that i grab with my method in my model.
I need create an new method ? create function ? idk what to do.
this is my controller atm:
public function index()
{
// Get List of the Offers
$this->load->model('offers_model');
$data['results_offers'] = $this->offers_model->list('all');
$this->load->view('offers_home', $data);
}
and this is my model code:
function list($id){
if($id != "all")
{
$query = $this->db->get_where('offers', array('offerid' => $id));
}
else
{
$query = $this->db->get('offers');
}
return $query->result();
}
Thanks for help me!
Try this
function getAllOffers(){
$query = $this->db->select('a.fieldname, b.name')->from('offers as a')->join('networks as b','a.networkid = b.id')->get();
return $query->result_array();
}
Further Information check Active Record
Related
I have 3 tables :legalpursuit , guarantors, guarantors_legalpursuit. and I have Many To Many Relationships in my project.
I save legalpursuit_id in legalpursuit / guarantors_id in guarantors / and both of them in guarantors_legalpursuit.
I am using the laravel 8.
how can I show every post's tags ? how can I select data from tag_post table?
I use it as follows. but it does not get the data.
it's my Legalpursuit model:
public function guarantorses()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(Guarantors::class, 'guarantors_legalpursuit', 'legalpursuit_id', 'guarantors_id');
}
it's my Guarantor model:
public function legalpursuits()
{
//return $this->belongsToMany(RelatedModel, pivot_table_name, foreign_key_of_current_model_in_pivot_table, foreign_key_of_other_model_in_pivot_table);
return $this->belongsToMany(LegalPursuit::class,'guarantors_legalpursuit','guarantors_id', 'legalpursuit_id');
}
and it's my controller :
public function edit(LegalPursuit $legalPursuit, $id)
{
$user = Auth::user();
$data = LegalPursuit::find($id);
$guarantors = $data->guarantorses()->orderBy('name')->get();
$lawyers = $data->lawyers()->orderBy('name')->get();
dd($guarantors);
$filesPursuit = LegalPursuit::find($id)->files;
$getLawyersLeft = DB::table('users')->where('user_type', '2')
->where('active', '1')
->where('deleted', '0')
->take('5')
->get();
return view('pages.legalpursuit.edit', compact('data', 'getLawyersLeft', 'filesPursuit', 'user'));
}
no table name is different and it actually follows exactly;
protected $table = 'guarantors';
The fields of the table are as follows;
table name; guarantors
field: ID
field: name
pivot tablo name and details;
table name; guarantors_legalpursuit
id;
guarantors_id
legalpursuit_id
I can also do the adding process successfully as follows.
$guarantorArray = $request->input('GUARANTORS');
//$GuarantorsID = Guarantors::find($guarantorArray);
$data->guarantorses()->attach($guarantorArray);
but I can't pull relational data. it does not give an error. but although there is data in the table, the array feels empty.
I have two join tables; parent and student. I have to update both tables from one button click. For that, I want to write a function "get data by id". I managed to write that code only for one table.
How do you write the following code if I want to get data from two tables? if p_id (parent id) is the foreign key?
Model
function get_by_id($id)
{
$this->db->from('student');
$this->db->where('p_id',$id);
$query = $this->db->get();
return $query->row();
}
Controller
public function ajax_edit($id)
{
$data = $this->Model_Action->get_by_id($id);
echo json_encode($data);
}
Hi I think you are looking for this. I use a sample from your code:
function get_by_id($id)
{
$this->db->from('student');
$this->db->join('table_b', 'student.p_id=table_b.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
Actually you can find more here
function get_by_id($id)
{
$this->db->select('*')
$this->db->from('student');
$this->db->join('parent','student.p_id=parent.p_id');
$this->db->where('student.p_id',$id);
$query = $this->db->get();
return $query->row();
}
I want to get the sum of the column with specific id from one table and get the details from another table with that id with mysql query and codeigniter.
I have used this code:
function total_commision($ids)
{
$values=array();
for($o=0;$o<count($ids);$o++) {
$this->db->select('SUM(commision) AS total_commision', FALSE);
$this->db->where('hierarchy_users_id',$ids[$o]);
$query = $this->db->get('commision');
$value=$query->result()[0];
array_push($values, $value);
}
return $values;
}
I want to get the details of each member from the another table with that id in database using codeigniter query .
you can try the following
function total_commision($ids)
{
$query = $this->db
->select('SUM(commision) AS total_commision, admin_credentials.*', FALSE)
->from("commision")
->join("admin_credentials", "admin_credentials.id = commision.hierarchy_users_id", "left")
->where_in('commision.hierarchy_users_id', $ids)
->group_by('commision.hierarchy_users_id')
->get();
return $query->result();
}
I just want to ask if you can select a certain table in the database? There is this code in my
model:
public function select_tables(){
$tables = $this->db->list_tables();
return $tables;
}
and in my controller:
public function maintenance(){
$this->data['title'] = "Inventory Maintenance";
$this->load->vars($this->data);
$this->load->view('homeview');
$selecttable['tablename'] = $this->inventory_model->select_tables();
$this->load->view('maintenance_view', $selecttable);
$this->load->view('footer_view');
}
Here is the printscreen in my view:
There is the list of my tables in the database, what I want is I can only show limited tables, for example I just want to show "tbladditional, tblemployees, tblinventorytype". Is there a syntax where I can select a certain table? An equivalent to this syntax
"Select * from 'tablename' where 'tablename' = 'something'"
It's so confusing so please I really need your help. Thank you so much!!
public function select_tables(){
$tables = $this->db->list_tables();
$req_tables = array("tble1", "table2"); //pass your table names as Array
$tables = array_intersect($tables, $req_tables);
return $tables;
}
This is the programmatic way I can come with. As CI doesn't have any method to retrieve specific table names.
you can put a mixture of Codeigniter and the query result
For example:
$table_list = $this->db->list_tables();
foreach($table_list as $tl)
{
if(strpos(strtolower($tl),'tbladditional')==true ||
strpos(strtolower($tl),'tblemployees')==true ||
strpos(strtolower($tl),'tblinventorytype')==true)
{
$query = "Select * from $tl";
}
}
i have a page that shows all student profiles with their results. all the user info i putted in the table "user"
and then i have another table "results" that shows all the students their scores from diffrent courses. the thing is i don't know how to write the query or controller function to link the student with their corresponding results. I need some help here thanks
Controller
function students()
{
$data = array();
$this->load->model('kdg_model');
$query = $this->kdg_model->get_students();
$query2 = $this->kdg_model->get_resultStudent();
if ($query)
{
$data['user'] = $query;
$data['results'] = $query2;
}
$this->load->view('students_view',$data);
}
Model
get_students get all rows from the database table user.
get_resultstudent gets all the rows from results. tried to combine them but it just gives me all the same rows back on every profile.
function get_students(){
$this->db->where('admin', 0);
$query = $this->db->get('user');
return $query->result();
}
function get_resultStudent(){
$this->db->select('*');
$this->db->from('results');
$this->db->join('user', 'user.id_user = results.FK_student');
$query = $this->db->get();
return $query->result();
}
I'm not sure what your database schema looks like but from what I can work out I've got this. Okay so in your controller you want to build your array of students. Let do this by calling two model functions. The first model function we are going to call we get us all of the students in the 'user' table. The model will pass back an array to the controller which we can loop through to get the individual students. On each iteration of the loop we can pass the 'id_user' to another function in our model to get the results for that student.
//This is where we will store the students and their results
$aData['aStudentResults'] = array();
//Load the model we need
$this->load->model('kdg_model');
//Get an array of students
$aStudents = $this->kdg_model->get_students();
//Now we have an array of student id's let loop through these and for each student
//let get their results
foreach($aStudents as $student)
{
//Add the student to the array
//The student id (id_user) is the key
$aData['aStudentResults'][$student->id_user] = $this->kdg_model->get_resultStudent($student->id_user);
}
//Pass out array of students to the view
$this->load->view('students_view', $aData);
These are the model functions we are calling in the controller
//Get all the students that are not admins
function get_students(){
$this->select('id_user')
-from('user')
->where('admin', 0);
$query = $this->db->get();
return $query->result();
}
//Use the id_user we were passed by the controller to get the results
function get_resultStudent($id_user){
$this->db->select('*');
$this->db->from('results');
$this->db->where('FK_student', $id_user);
$query = $this->db->get();
return $query->result_array();
}
At this point we now have all the data we need. We just need to pass it from the controlle to the view which we did by passing the view $aData. In our view we can access what we passed to it like so
<? foreach($aStudentResults as $studentId => $aResults): ?>
<p>Student id: <?= $studentId ?></p>
<?foreach($aResults as $aResult): ?>
//Do something with results
<? endforeach; ?>
<? endforeach; ?>
This hasn't been tested so there may be some syntax errors but hopefully you should get an idea of what I am trying to do and what you need to do.
It's important for you to understand how MVC works. Practice selected data from your database in your models, and then passing that data to your views via the controller. You'll quickly get the hang of it.
If there is anything you don't understand in this answer please leave a comment and let me know.