data While Cakephp - while-loop

I'm developing an application with cakephp.
I need to separate the results find('all') for group.
for example my table has the fields: Name; Group.
I want to make a select group by using criteria.
My Controller:
public function print(){
$this->loadModel('Nomevento');
$total = $this->Nomevento->find('count');
$i=1;
while($i<=$total) {
$this->set('grupo', $this->Lista->find('all', array('conditions' => array('Lista.nomevento_id' => $i))));
$i++;
}
}
How do I collect these data in view?
My view:
<?php foreach ($grupo as $grupos): ?>
<table>
<tr>
<td colspan="5" class="tablelinha"><?php echo $grupos['Nomevento']['nome']; ?></td>
</tr>
<tr>
<td class="tablelinha"><?php echo $grupos['Lista']['data']; ?></td>
<td class="tablelinha"><?php echo $grupos['Lista']['dia']; ?></td>
<td class="tablelinha"><?php echo $grupos['Lista']['hora']; ?></td>
<td class="tablelinha"><?php echo $grupos['Igreja']['nome']; ?></td>
<td class="tablelinha"><?php echo $grupos['Responsavel']['responsavel']; ?></td>
<tr><td colspan="5" class="tablelinha"><?php echo $grupos['Lista']['obs']; ?></td></tr>
</tr>
</table><p></p>
The while is working but is displaying only the last result.
My Query:
SELECT COUNT(*) AS `count` FROM `lista_batismo`.`nomeventos` AS `Nomevento` WHERE 1 = 1 SELECT `Lista`.`id`, `Lista`.`data`, `Lista`.`dia`, `Lista`.`hora`, `Lista`.`obs`, `Lista`.`igreja_id`, `Lista`.`responsavel_id`, `Lista`.`nomevento_id`, `Igreja`.`id`, `Igreja`.`codigo_ccb`, `Igreja`.`nome`, `Responsavel`.`id`, `Responsavel`.`responsavel`, `Responsavel`.`comum`, `Nomevento`.`id`, `Nomevento`.`nome`, `Nomevento`.`prioridade` FROM `lista_batismo`.`listas` AS `Lista` LEFT JOIN `lista_batismo`.`igrejas` AS `Igreja` ON (`Lista`.`igreja_id` = `Igreja`.`id`) LEFT JOIN `lista_batismo`.`responsavels` AS `Responsavel` ON (`Lista`.`responsavel_id` = `Responsavel`.`id`) LEFT JOIN `lista_batismo`.`nomeventos` AS `Nomevento` ON (`Lista`.`nomevento_id` = `Nomevento`.`id`) WHERE `Lista`.`nomevento_id` = 1 SELECT `Lista`.`id`, `Lista`.`data`, `Lista`.`dia`, `Lista`.`hora`, `Lista`.`obs`, `Lista`.`igreja_id`, `Lista`.`responsavel_id`, `Lista`.`nomevento_id`, `Igreja`.`id`, `Igreja`.`codigo_ccb`, `Igreja`.`nome`, `Responsavel`.`id`, `Responsavel`.`responsavel`, `Responsavel`.`comum`, `Nomevento`.`id`, `Nomevento`.`nome`, `Nomevento`.`prioridade` FROM `lista_batismo`.`listas` AS `Lista` LEFT JOIN `lista_batismo`.`igrejas` AS `Igreja` ON (`Lista`.`igreja_id` = `Igreja`.`id`) LEFT JOIN `lista_batismo`.`responsavels` AS `Responsavel` ON (`Lista`.`responsavel_id` = `Responsavel`.`id`) LEFT JOIN `lista_batismo`.`nomeventos` AS `Nomevento` ON (`Lista`.`nomevento_id` = `Nomevento`.`id`) WHERE `Lista`.`nomevento_id` = 2 SELECT `Lista`.`id`, `Lista`.`data`, `Lista`.`dia`, `Lista`.`hora`, `Lista`.`obs`, `Lista`.`igreja_id`, `Lista`.`responsavel_id`, `Lista`.`nomevento_id`, `Igreja`.`id`, `Igreja`.`codigo_ccb`, `Igreja`.`nome`, `Responsavel`.`id`, `Responsavel`.`responsavel`, `Responsavel`.`comum`, `Nomevento`.`id`, `Nomevento`.`nome`, `Nomevento`.`prioridade` FROM `lista_batismo`.`listas` AS `Lista` LEFT JOIN `lista_batismo`.`igrejas` AS `Igreja` ON (`Lista`.`igreja_id` = `Igreja`.`id`) LEFT JOIN `lista_batismo`.`responsavels` AS `Responsavel` ON (`Lista`.`responsavel_id` = `Responsavel`.`id`) LEFT JOIN `lista_batismo`.`nomeventos` AS `Nomevento` ON (`Lista`.`nomevento_id` = `Nomevento`.`id`) WHERE `Lista`.`nomevento_id` = 3 SELECT `Lista`.`id`, `Lista`.`data`, `Lista`.`dia`, `Lista`.`hora`, `Lista`.`obs`, `Lista`.`igreja_id`, `Lista`.`responsavel_id`, `Lista`.`nomevento_id`, `Igreja`.`id`, `Igreja`.`codigo_ccb`, `Igreja`.`nome`, `Responsavel`.`id`, `Responsavel`.`responsavel`, `Responsavel`.`comum`, `Nomevento`.`id`, `Nomevento`.`nome`, `Nomevento`.`prioridade` FROM `lista_batismo`.`listas` AS `Lista` LEFT JOIN `lista_batismo`.`igrejas` AS `Igreja` ON (`Lista`.`igreja_id` = `Igreja`.`id`) LEFT JOIN `lista_batismo`.`responsavels` AS `Responsavel` ON (`Lista`.`responsavel_id` = `Responsavel`.`id`) LEFT JOIN `lista_batismo`.`nomeventos` AS `Nomevento` ON (`Lista`.`nomevento_id` = `Nomevento`.`id`) WHERE `Lista`.`nomevento_id` = 4
Thanks for your attention;

It looks your are counting records so it would return only INT.
I wouldn't use $this->loadModel('Nomevento'); You should define properly relations between your models (Nomevento and Lista).

Related

Am having difficulty displaying data in a table using SQL Aggregate Function SUM()

I tried to use the SQL Aggregate Function SUM () and it successfully but i can't seem to be able to display this in a table
Below is my php code i tried it with
`$sql = " SELECT product_name, sum(unit_price_before_discount)
FROM transaction_sell_lines
GROUP BY product_name";
<!-- TABLE CONSTRUCTION -->
<table>
<tr>
<th>Product Name</th>
<th>Quantity</th>
<th>Unit Price</th>
<th>Current Value</th>
</tr>
<!-- PHP CODE TO FETCH DATA FROM ROWS -->
<?php
// LOOP TILL END OF DATA
while($rows=$result->fetch_assoc())
{
?>
<tr>
<!-- FETCHING DATA FROM EACH
ROW OF EVERY COLUMN -->
<td><?php echo $rows['product_name'];?></td>
<td><?php echo $rows['quantity'];?></td>
<td><?php echo $rows['unit_price_before_discount'];?></td>
<td><?php echo ($rows['unit_price_before_discount'])*$rows['quantity'];?></td>
</tr>
<?php
}
?>
</table>`
This is the result i get
And this is my database structure
What i want to do is to add all the values in quantity that has the same name together and display in a table

Start Order by data from table that is found in another table

My question may be not easy to explain but I am going to do my best.
I have 3 tables :
medicine
patient
patient_medicine
the third table contains 3 columns :
auto increment (id)
medicine_id
patient_id
I make a query that display all data in medicine table and if there is a patient id found in patient_medicine table I check it and those that are not found are not checked.
A- controller
public function showPatientMedicine()
{
$data['patient_id']=$this->input->post('patient_id');
$data['medicine'] = $this->model_admin->medicine();
$this->load->view('admin/show-Patient-Medicine',$data);
}
B- VIEW
<table>
<thead>
<tr>
<th>List</th><th></th>
</tr>
</thead>
<tbody>
<?php
foreach($medicine as $row){
$patient_med=$this->db->select('medicine_id')
->where('patient_id',$patient_id)
->where('medicine_id',$row->id)
->get('patient_medicine')->row('medicine_id');
if($row->id==$patient_med){
$checked="checked";
} else {
$checked="";
}
?>
<tr>
<td>
</td>
<td>
<input type='checkbox' value="<?=$row->id?>" <?=$checked?> />
</td>
</tr>
<?php
}
?>
</tbody>
</table>
THIS QUERY SHOWS ME ALL MEDICINES AND CHECK THE MEDICINES THAT ARE FOUND IN PATIENT_MEDICINE TABLE
MY QUESTION IS :
HOW CAN I SHOW ALL MEDICINES THAT ARE CHECKED IN THE FIRST POSITION OR HOW TO ORDER BY CHECKED CHECKBOX ?
THANK YOU IN ADVANCE.
Try this,
SELECT * FROM `medicine` as a left join (select medicine_id,patient_id from patient_medicine where patient_id = 2) as b on a.id = b.medicine_id
Put the above query in your controller, it will automatically sort the result based on patient id, if patient details found. it will return null for all other details in rows except medicine details.
So you can just
<?php foreach($medicine as $row){ ?>
<input type='checkbox' value="<?=$row->id?>" <?php echo !is_null($row->patient_id)?'checked':''; ?> />
<?php } ?>

How to join three table in laravel eloquent?

I have three tables:
consignee (id,name) , jobs(id,consignee_id), expenses(id,job_id)
In my ExpensesController
public function index(){
$expenses = \App\Expense::all();
return view('expenses.index', compact('expenses'));}
in my index view page i want get all the details in expenses table with consignee name now in my view page
#foreach($expenses as $expense)
<tr>
<td width="5%">{!! $expense->id !!}</td>
<td width="7%">{!! $expense->date !!}</td>
<td width="7%">{!! $expense->job_id !!}</td>
<td width="10%">{!!$expense-> !!}</td></tr>
#endforeach
i dont know how to get consignee name in my index view any one suggest me idea
Instead of:
$expenses = \App\Expense::all();
you can use:
$expenses = \App\Expense::select('expenses.*','consignee.name')
->leftJoin('jobs','jobs.id','=','expenses.job_id')
->leftJoin('consignee','consignee.id','=','jobs.consignee_id')
->get();
This way you make join with 2 other tables and get name of Consignee.

SUM and GROUP BY and Difference of colums

i have a table t1
ag_name ag_start ag_end
a 10 20
c 30 50
a 60 70
c 70 75
i want to have this :
ag_name numberOfCards
a 20
c 25
which means a has (20-10) + (70-60) = 20 Cards.
then please help me with the QUERY ??
select ag_name, sum(ag_end - ag_start)
from the_unknown_table
group by ag_name
Please try:
select
ag_name,
sum(ag_end-ag_start) NoOfCards
From t1
group by ag_name
Select SUM(ag_end - ag_start) as numberofcards, ag_name From table
Group by ag_name
the query works fine when i click on go in DirectAdmin , but when i write the code to show the recordset it shows different result .
my model :
public function getAgents(){
$db =JFactory::getDBO();
$query_Recordset1 = "SELECT *,SUM(ag_end - ag_start) AS ag_num FROM #__basak_agent group by ag_fname";
$db->setQuery($query_Recordset1);
return $db->loadAssoc();
}
and this is my .php code :
</thead> <?php
$count=0;
$result=$this->get('Agents');
foreach( $result as $row_Recordset1 )
{
$count++;
?>
<tr>
<td> <?php echo $count ?></td>
<td> <?php echo $row_Recordset1['ag_fname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_lname'] ; ?></td>
<td> <?php echo $row_Recordset1['ag_num'] ; ?></td>
</tr>
<?php } ?>
</table>
but in code shows :
1 a a a
2 a a a
3 2 2 2
there is no field like this in table , i dont know where it comes from ??!
anybody have suggestion , where i have to look for it ? what is wrong with my code ?

SQL Join in CodeIgniter

I have one file, then for every file there are multiple comments about that file. I was able to successfully join the two tables, comments and files, however when I try to view the page where it displays the file and its comments, the page displays the name of file n times (n corresponds to the number of records of comments of that certain file).
How will I be able to display just once the file name as well as displaying the comments?
<table width="40%" align="center">
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
</table>
<table width="40%" align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666">
<?php foreach($rows as $file):?>
<tr>
<td width="15%"></td>
<td width="45%" id="name">FILESIZE:</td>
<td width="40%" id="txt3"><?php echo $file->size; ?></td>
</tr>
<tr>
<td></td>
<td id="name">DATE UPLOADED:</td>
<td id="txt3"><?php echo $file->dateUploaded;?></td>
</tr>
<?php endforeach;?>
</table>
<!--COMMENTS -->
<table align="center" frame="above" style="margin-top:10px; border-top-width:3px; border-top-color:#666666" width="40%">
<tr>
<td><h3>Comments</h3></td>
</tr>
<tr><?php foreach($rows as $comment):?>
<td><?php echo $comment->comm;?></td>
</tr><?php endforeach;?>
</table>
You can use
GROUP_CONCAT()
in your join.
SELECT person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles
FROM person
LEFT OUTER JOIN person_role ON person.id=person_role.person_id
LEFT OUTER JOIN role ON person_role.role_id=role.id
GROUP BY id
ORDER BY id;
is an example from a tutorial at
http://dev-loki.blogspot.com/2008/01/aggregate-join-results-with-mysql-using.html
can't give much better without seeing your schema.
EDIT: trying anyway.
SELECT files.*, GROUP_CONCAT(comments.comment SEPARATOR ',') as comments_list
FROM files
LEFT JOIN comments on files.id = comments.id
GROUP BY id
ORDER BY id
Adding onto orbit, in codeigniter if your using activerecord it would be:
(Sorry, I couldn't format code in a comment)
$this->db->select('person.id AS id, name, GROUP_CONCAT(role.role SEPARATOR ',') AS roles',false);
$this->db->from('person');
$this->db->join('person_role', 'person.id = person_role.person_id', 'left outer');
$this->db->join('role', 'person_role.role_id = role.id', 'left outer');
$this->db->group_by('id');
$this->db->order_by('id'):
$this->db->get();
well it looks like you're echoing the filename for each row that is returned
your rows are getting returned like
name | size | comment1
name | size | comment2
so just don't do foreach ($rows as $file) that will print out the filename each time, just change
<?php foreach($rows as $file):?>
<tr>
<td width="70%" id="title"><?php echo $file->name; ?></td>
</tr>
<?php endforeach;?>
to
<tr>
<td width="70%" id="title"><?php echo $rows[0]->name; ?></td>
</tr>