CODEIGNITER, Reference data from 2 tables to show in view file - codeigniter

Trying to cross reference data from 2 tables and only show the data from 1 of the tables...
If a "district" in table 001 matches a "district" in table 002 then I'd like to echo the corresponding "coordinator" from table 002
TABLE 001
| month_of_report | district | org |
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
| January | 004 | Dallas |
| February | 029 | Dallas |
| March | 047 | Dallas |
TABLE 002
| coordinator | district |
- - - - - - - - - - - - - - -
| Jamie | 004 |
| Susie | 047 |
| Jimmy | 029 |
MODEL
public function reg_co()
{
$this->db->where('report_month', $report_month);
$query = $this->db->get('non_clinical_total_tests');
$this->db->select('*');
$this->db->from('coordinator');
$this->db->join('non_clinical_total_tests', 'non_clinical_total_tests.district = coordinator.district');
if($query->num_rows() > 0)
{
return $query->row();
}
}
CONTROLLER
$data['reg_co'] = $this->Page_model->reg_co();
VIEW FILE
<?php if($reg_co) : ?>
<strong>DISTRICT: </strong><?php echo $reg_co->district; ?><br>
DESIRED RESULT
If the district from the 2 tables match then I should be able to echo the coordinator for that district.

________________________________________________________________________________
//This code is for single record from database
//Model function
public function reg_co()
{
$this->db->select('non_clinical_total_tests.*');
$this->db->from('non_clinical_total_tests');
$this->db->join('coordinator', 'coordinator.district = non_clinical_total_tests.district');
$this->db->where('non_clinical_total_tests.report_month', $report_month);
$this->db->limit(1);
if($this->db->num_rows() > 0)
{
return $this->db->get()->row();
}
return false;
}
//View file
<?php if($reg_co) : ?>
<strong>DISTRICT: </strong><?php echo $reg_co->district; ?><br>
_____________________________________________________________________________________
//This code is for all records from database
//Model function
public function reg_co()
{
$this->db->select('non_clinical_total_tests.*');
$this->db->from('non_clinical_total_tests');
$this->db->join('coordinator', 'coordinator.district = non_clinical_total_tests.district');
$this->db->where('non_clinical_total_tests.report_month', $report_month);
if($this->db->num_rows() > 0)
{
return $this->db->get()->result_array();
}
return false;
}
//View file
<?php if($reg_co) : ?>
<?php foreach($reg_co as $reg) { ?>
<strong>DISTRICT: </strong><?php echo $reg['district']; ?><br>
<?php } ?>

Related

How to convert into array in this condition?

In Student model, i have made attribute like this
protected $appends = ['present'];
public function getPresentAttribute(){
return $this->getTotalDays()-$this->getAbsentDays();
}
My Database looke like this
| id | student_name | created_at |
*----*----------------------*-------------------*--
| 1 | Adam | 2020/11/10 |
| 2 | Annie | 2020/11/10 |
|3 | Paul | 2020/11/10 |
if i do like this in controller then
$students=Student::get();
foreach($students as $stu){
echo "<pre>"; print_4($stu->student_name,$stu->present); die;
}
My result will be like this
Adam -> 32
Annie ->34
Paul->33
But i need to convert like this
['Students', 'Present Day',],
['Adam' , 32],
['Annie' , 34],
['Paul' , 33],
So in controller, I tried like this
public function attendancePerformance(){
$students=Student::get();
$data=[];
$student_data=[];
$arrayHeader = ["Students","Present Day"];
array_push($data,$arrayHeader);
foreach ($students as $stu) {
array_push($student_data, $stu->student_name,$stu->present);
}
array_push($data,$student_data)
}
But it not working.How should i do it ?

How to show filed in distinct codeigniter

Hello guys I would like to distinct field product_code and I would like to show all field
Table Transaction :
---------------------------------------------------
| id | product_code | price | datetime |
---------------------------------------------------
| 1 | 001 |20 | 2018-18-12 09:09:09 |
| 2 | 002 |30 | 2018-18-12 08:09:09 |
| 3 | 001 |20 | 2018-18-12 08:08:08 |
---------------------------------------------------
This is my model :
$this->db->distinct('product_code');
$this->db->select('id','product_code','price','datetime');
$this->db->from($this->table);
return $this->db->get()->result_array();
You can try this solution for you problem :
$this->db->distinct();
$this->db->select('id','product_code','price','datetime');
$this->db->from($this->table);
return $this->db->get()->result_array();
Than
echo $query->num_rows();
OR
$this->db->select('DISTINCT(product_code), ');
$this->db->from($this->table);
$query=$this->db->get();
print_r($this->db->get()->result_array());
echo $query->num_rows();exit;
I hope it will help you.

Yii2 AR left join with where and relations

Book
----------------------------
| id | name | published |
----------------------------
| 1 | book1 | 1 |
| 2 | book2 | 1 |
| 3 | book3 | 0 |
Chapter
----------------------------
| id | book_id | name | published |
----------------------------
| 1 | 1 | chapter1 | 1 |
| 2 | 1 | chapter2 | 0 |
| 2 | 2 | chapter1 | 0 |
| 3 | 3 | chapter1 | 1 |
class Book{
public function getChapter()
{
return $this->hasMany(Chapter::className(), ['kook_id' => 'id']);
} }
class Chapter{
public function getBook()
{
return $this->hasOne(Book::className(), ['id' => 'book_id']);
} }
How can i get published books with published pages using ActiveRecord (i want get book1 with chapter1 and book2 without any chapters)?
smth like Book::find($id)->where(['published' => 1])-> {{{left join with where}}} ->all())
ADDED
And then i wand to use it
echo $book->name;
foreach($book->getChapter() as chapter){
echo chapter->name;
}
Change your Book class relation as
class Book
{
public function getChapter()
{
return $this->hasMany(Chapter::className(), ['book_id' => 'id'])->where(['published' => 1]);
}
}
To get Related Records use with()
Book::find()->with(['chapter'])->where(['id' => $id ,'published' => 1])->all()
To Use it:
//Book Name
echo $book->name;
//For Chapters Name
if($book->chapter){
foreach($book->chapter as $chapter){
echo $chapter->name;
}
}

Validation in laravel 5.1

as would do this in laravel ?
I need to pass this condition laravel
$query = mysql_query("SELECT * FROM citas WHERE
especialidad='".$especialidad."'AND doctor='".$doctor."'AND
fecha ='".$fecha."' AND hora = '".$hora."'" , $con);
if ($existe = mysql_fetch_object($query))
{
}else{}
this is my table in the database:
fecha | hora | especialidad | doctor |
12-07-2015 | 01:17:00 | General | Carlos |
12-07-2015 | 01:35:00 | General | Carlos |
This would be what shipping.
fecha | hora | especialidad | doctor |
12-07-2015 | 01:22:00 | General | Carlos | <--- It is saved by the time it is different
12-07-2015 | 01:35:00 | General | uriel | <--- It is saved by the dcotor it is different
12-07-2015 | 01:35:00 | General | carlos | <--- It is no, because
it already exists
Try this:
$query = DB::table('citas')
->where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}
Or you can try this by Eloquent Model. I assume you have a model named Cita
Add use App\Cita at top of your code after namespace in your controller
$query = Cita::where('especialidad',$especialidad)
->where('doctor',$doctor)
->where('fecha',$fecha)
->where('hora',$hora)
->get();
if($query != null){
// your code will be here
}else{
// you code will be here
}

Laravel 5 - Eloquent JOIN confusion

I have two simple tables, cats and breeds:
mysql> select * from cats;
+----+--------+---------------+----------+---------------------+---------------------+
| id | name | date_of_birth | breed_id | created_at | updated_at |
+----+--------+---------------+----------+---------------------+---------------------+
| 1 | Rita | 2008-07-06 | 1 | 2015-05-09 20:40:49 | 2015-05-09 20:50:20 |
| 2 | Muni | 1992-05-15 | 3 | 2015-05-09 20:50:54 | 2015-05-09 20:50:54 |
| 3 | Hector | 2005-01-23 | 4 | 2015-05-09 21:08:23 | 2015-05-09 21:08:23 |
+----+--------+---------------+----------+---------------------+---------------------+
3 rows in set (0.00 sec)
mysql> select * from breeds;
+----+------------+
| id | name |
+----+------------+
| 1 | Domestic |
| 2 | Persian |
| 3 | Siamese |
| 4 | Abyssinian |
+----+------------+
4 rows in set (0.00 sec)
All I want to do is get a list of the cats that are of a certain breed. In the cats table, breed_id is a FK that points to the PK of the breeds table. The example code in my book which is supposed to return a view with all cats of a specific breed returns no results:
The url I am trying is /cats/breeds/Domestic
The view that is generated uses this logic in the routes.php file:
Route::get('cats/breeds/{name}', function($name)
{
$cats = Furbook\Cat::with('breeds')
->whereName($name)
->get();
dd($cats);
});
When I dd($cats) I get zero results even though I have a cat with a domestic breed_id of 1. What have I done wrong here?
EDIT:
I can get it to work with the following hacky code, which first gets the id from the breeds table by querying against the name field, then queries the cats table with that id:
$breed = Furbook\Breed::whereName($name)->get();
$id = $breed[0]['attributes']['id'];
$cats = Furbook\Cat::whereId($id)->get();
How do I make this into one Eloquent query? I see no examples for this kind of query on the Laravel site.
For more information this is how the models look:
class Breed extends Model {
public $timestamps = false;
public function cats()
{
return $this->hasMany('Furbook\Breed');
}
}
class Cat extends Model {
protected $fillable = [
'name',
'date_of_birth',
'breed_id',
];
public function breed()
{
return $this->belongsTo('Furbook\Breed');
}
}
You're asking the system to give you all cats with a name of Domestic - not all cats of the breed name Domestic.
Assuming your model relationships are in order, you could do e.g.
$cats = Furbook\Breed::whereName($name)->first()->cats;
Also, my Burmese cat just hit the monitor; I think she's upset that she's not in the breeds list.

Resources