remove duplicate values in foreach - php

Hello Developers I have to print a report in my application. I am using codeigniter. I have a table like this in my database.
Report Detail
---------------------------------------------------------------------------
ID Test_ID Description Description_Group Test_Name
---------------------------------------------------------------------------
1 117 value 1 group 1 Test 1
2 117 value 2 group 1 Test 1
3 4 another 1 group 2 Test 2
4 4 another 2 group 2 Test 2
5 4 another 3 group 2 Test 2
I want to print like this
desired print format
-----------------------------------------------
Description
-----------------------------------------------
**Test 1**
group 1
value 1
value 2
**Test 2**
group 2
another 1
another 2
another 3
Here is my Model Codel
public function print_report($Patient_ID, $ID)
{
$query = $this->db->query('select a.ID, a.Description, a.Description_Group, a.Normal_Range, a.Measure_Unit,
b.Result_Value
from tbltestdefault a
inner join tblreportdetail b on a.ID = b.Test_Default_ID
where b.Patient_ID = '.$Patient_ID.' and b.Patient_Test_ID = '.$ID.'');
return $query->result_array();
}
Controller
public function print_report($ID, $Patient_ID, $Test_ID)
{
$data['patient'] = $this->Report_Model->getPatientinfo($Patient_ID);
$data['testname'] = $this->Report_Model->gettestnameonly($Test_ID);
$data['tests'] = $this->Report_Model->print_report($Patient_ID, $ID);
$this->load->view('report_view', $data);
}
and a view to display data
<table class="table table-bordered">
<thead>
<tr>
<th> <h4>Description</h4>
</th>
<th> <h4>Result</h4>
</th>
<th> <h4>Units</h4>
</th>
<th> <h4>Normal Value</h4>
</th>
</tr>
</thead>
<tbody>
<?php foreach($tests as $t): ?>
<tr>
<td>
<?php echo $t['Description_Group']; ?>
<?php echo $t['Description']; ?>
</td>
<td><?php echo $t['Result_Value']; ?></td>
<td><?php echo $t['Measure_Unit']; ?></td>
<td><?php echo $t['Normal_Range']; ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>

man, i think you are taking this site for granted.. i already provided you the main logic.you just need to expand it depending on your additional needs.. btw here's the expanded logic
<?php
$get_title = '';
$get_subtitle = '';
foreach($tests as $t)
{
//if start of new test name
if($get_title != $t['Test_Name'])
{
echo $t['Test_Name'];
//if start of new desc group
if($get_subtitle != $t['Description_Group'])
{
echo $t['Description_Group'];
echo $t['description'];
$get_subtitle = $t['Description_Group'];
}
//if same desc with previous
elseif($get_subtitle == $t['Description_Group'])
{
echo $t['description'];
}
$get_title = $t['Test_Name'];
}
//if same title with previous
elseif($get_title == $t['Test_Name'])
{
//if start of new desc group
if($get_subtitle != $t['Description_Group'])
{
echo $t['Description_Group'];
echo $t['description'];
$get_subtitle = $t['Description_Group'];
}
//if same desc with previous
elseif($get_subtitle == $t['Description_Group'])
{
echo $t['description'];
}
}
}
add this at the last part of your sql query
ORDER BY Test_Name ASC, Description_Group ASC, Description ASC
not sure if this would work, because i haven't tried it.. hit me with some comments if something went wrong..cheers

Related

MySQL Data in the PHP Array

I have a table as follows that is used to store details of employees:
+---------+---------+------------------+
| of_code | name | employment_status |
+---------+---------+------------------+
| 1 | Jhon | 1 |
| 2 | Ram | 1 |
| 3 | Edward | 3 |
| 4 | William | 2 |
+---------+---------+------------------+
This is one of the options in my Codeigniter project.
Desired output
I want to show the employment_status in separate columns in the view as per the value in it. If employment_status = 1, it should be in the first column. If employment_status = 2, it should be in the second column. If employment_status = 3, it should be in the the column and so on.
Controller (Relevant Part)
$where = NULL;
$this->data['officerList'] = $this->Officer_model->officerSummary($where);
Model
function officerSummary($where){
$q = $this->db->query("SELECT
of_code, name, employment_status as cnt
from tbl_officer $where
");
if ($q->num_rows() > 0) {
return $q->result();
}
}
View (Relevant Part)
<?php
$officerLists = [];
foreach ($officerList as $row) {
if (!in_array($row->cnt, $officerLists)) {
$officerLists[] = $row->cnt;
}
}
?>
<div class="table-responsive" id="datatable">
<table id="ExData" cellpadding="0" cellspacing="0" border="0"
class="table table-bordered table-condensed table-hover table-striped reports-table">
<thead id="th">
<tr class="" style="background-color: #d966ff !important;">
<th>#</th>
<th style width="5%" class="text-center"><font size="1"> Code</th>
<th style width="15%" class="text-left"><font size="1">Name</th>
<th class="text-center" colspan="<?= count($officerLists) ?>"><font size="1">Employment Status</th>
</tr>
</thead>
<tbody>
<?php
$count = [];
if(!empty($officerList)) {
foreach ($officerList as $rows) {
$total += $rows->cnt;
$c++;
?>
<tr>
<td><font size="1"><?= $rows->of_code ?></td>
<td><?= $rows->name ?></td>
<?php
foreach ($officerLists as $value) {
if ($rows->cnt == $value) {
if (isset($count[$value]))
$count[$value] = $count[$value] + $rows->of_code;
else
$count[$value] = $rows->of_code;
echo "<td class='text-center'></td>";
} else
echo "<td class='text-center'>-</td>";
}
}
?>
</tbody>
</table>
The function outs three columns correctly, but with no values. Only '-' shows non-relevant places in the column as follows.
What may be going wrong? Can anyone help?
Maybe using a SELECT - CASE structure can be your option
A query in this way
SELECT of_code,
name,
CASE WHEN employment_status = 1 THEN employment_status AS StatusA,
CASE WHEN employment_status = 2 THEN employment_status AS StatusB,
CASE WHEN employment_status = 3 THEN employment_status AS StatusC
FROM yourTbale;
Every CASE will verify if the status is 1 or 2 or 3
Th previous step will generate a new column with a given alias
Because we need the status value as cell value in our result I put it again after the THEN clause
If this work, you only will need to adapt it in CI sintax

PHP - SUM number according to item name

Am new in PHP, I need help in my scripts since I don't get preferred answer.
I have a "bills" table which store information's of sales items. as folows
id | gid | drinks | no_drinks | servicetime | date
1 2 Orange 4 1 2018-08-16
2 2 Orange 2 1 2018-08-16
3 2 Orange 3 1 2018-08-16
Below file (BillsController.php) query gid, servicetime and date, and the query is equal to true.
BillsController.php
$bi = Barz::whereRaw('gid=? and servicetime=? and date=?', array($gid, $t,date('Y-m-d')))->get();
return View::make('bills.show', compact('bi'));
Below is "show.php" file which display the information's from Database. I will just shows you few things in it.
<?php
$foods = array();
foreach($bi as $row){
$foods[] = $row->drinks;
$idadi[] = $row->no_drinks;
$unique = array_keys(array_count_values($foods));
$l = count($unique);
}
$newarr = array();
foreach ($unique as $key => $value) {
array_push($newarr, $value);
}
?>
<table class="table table-bordered" id="gt">
<tr style="background-color: #f5f5f5">
<th>Drink</th>
<th>Qty...sx</th>
<th>Time</th>
<th>#cost</th>
<th>Total#</th>
<th>
<select class="form-control active" id="tserv">
<option value="{{$row->servicetime}}">{{Bill::tm($row->servicetime)}}</option>
<select>
</th>
</tr>
<?php $total = 0; ?>
#for($i=0; $i < $l; $i++)
<tr>
<td>{{$newarr[$i]}}</td>
<td>{{$idadi[$i]}}</td> // Only problem is here
<td>{{Bill::appears($newarr[$i], $foods)}}</td>
<td>{{Bar::where('name', $newarr[$i])->first()->cost}} /=</td>
<td>{{($idadi[$i])*(Bar::where('name', $unique[$i])->first()->cost)}}/= </td>
</tr>
<?php $total = $total + (($idadi[$i])*(Bar::where('name', $newarr[$i])->first()->cost)); ?>
#endfor
<tr style="background-color: #f5f5f5">
<td ></td>
<td ></td>
<td></td>
<td><b>Total</b></td>
<td id="ttl">
{{$total}} /=
</td>
</tr>
When I run above query, I get 4 from ("{{$idadi[$i]}}"), which means it take only first row, and the rest is not selected. I want it to do like this (4+2+3) = 9.
To add columns together;
SELECT col1, col2, col3, (col1+col2+col3) AS Total FROM
To add rows together, use the SUM()
Aggregate below;
SELECT userid, SUM(col1) AS col1_total, SUM(col2) AS col2_total FROM table GROUP BY userid
Try this out :
select SUM(no_drinks) no_drinks from table_name GROUP BY drinks

SQL PHP : How to SELECT and fetch like this?

Hello I'm know how to use basic SQL about select, join, fetch
but don't have idea how fetch like this to my website (see screenshot)
From screenshot It has column room_number too.
<table>
<tr>
<td>room no.</td>
<td>room name</td>
</tr>
<?php
$sql = SELECT * FROM si_room;
$query = $db->query($sql);
while($row = $query->fetch(PDO::FETCH_ASSOC))
{ ?>
<tr>
<td><?php echo ????? ; ?></td>
<td><?php echo ?????; ?></td>
</tr>
<?php } ?>
</table>
UPDATE :
I try with this code
<?php
$sql = "SELECT * FROM si_room";
$querymain = $db->query($sql)->fetchAll(PDO::FETCH_GROUP);
?>
<table>
<tr>
<td>room no.</td>
<td>room_name</td>
</tr>
<?php
foreach ($querymain as $list_id => $list_data)
{
echo "<tr>";
echo "<td> floor ".$list_data[0]['room_floor']."<br>";
foreach ($list_data as $row)
{
echo $row['room_no']."</td>";
echo "<td>".$row['room_name']."</td>";
}
} ?>
</tr>
</table>
but the result is
floor 2
201 | ROOM A
floor 2
202 | ROOM B
floor 3
301 | ROOM E
floor 3
302 | ROOM F
That's not I want.
Luckily you are using PDO which already has this functionality for you, that can do exactly what you want - group the data based on some column.
PDO can group results into the nested arrays, based on the first field selected. So you need to list your list id as the first field in the field list , and then get your rows using fetchAll() with aforementioned fetch mode:
$sql = "select room_floor, room_name, room_no from si_room";
$building = $dbh->query($sql)->fetchAll(PDO::FETCH_GROUP);
and now you get a neat nested array where your rows are grouped by list id!
To make it output neatly you have to use two nested foreach operators
foreach ($building as $floor_no => $rooms)
{
echo $floor_no."\n";
foreach ($rooms as $row)
{
echo $row['room_name']."\n";
}
}

How to remove the same name occurrences in mysql left join?

I am using the following query.
SELECT DISTINCT ph.module_head_id,
ph.module_head, pm.module_id,
pm.module_name, pm.module_url
FROM pms_module_header ph
LEFT JOIN pms_module pm
ON ph.module_head_id = pm.module_head_id
Now i am getting the following output
Module_head_id Module_head module_id module_name module_url
1 dashboard Null Null Null
2 Employee 1 Add test.php
2 Employee 2 View test1.php
3 Report 3 Project project.php
3 Report 4 Client client.php
How to remove the same name occurences in module_head
I Want to display the out like these in codeigniter view
dashboard
Employee Add
View
Report Project
Client
I am using array_unique function but its not work.Please help me
Here is the query,
SELECT DISTINCT ph.module_head_id,
ph.module_head, pm.module_id,
pm.module_name, pm.module_url
FROM pms_module_header ph
LEFT JOIN pms_module pm
ON ph.module_head_id = pm.module_head_id
group by ph.module_head_id
Just apply group by on module_head_id, it will work.
EDIT
<table border="1" width="100%">
<tr>
<th>Module head</th>
<th>Module name</th>
</tr>
<?php
$module_head = '';
foreach ($arr as $k => $v) {
echo "<tr>";
echo "<td>";
if ($v['module_head'] == '' || $v['module_head'] != $module_head) {
$module_head = $v['module_head'];
echo $v['module_head'] . "<br/>";
} else {
echo " ";
}
echo "<td>";
echo "<td>" . $v['module_name'] . "</td>";
echo "<tr>";
}
?>
<table>
Manipulate as per your view. It will work.

Split ID's Up In Tables In While Loop

I have the following database table:
AWARD_ID | NOMINEE_ID | VOTER_ID | MULTI_CODE
------------------------------------------------------
5 | 3 | 1 | 9326
5 | 4 | 1 | 9326
5 | 5 | 3 | 8746
I need to display these results in tables grouped by MULTI_CODE, so for example:
So it would look like
<h1>Multi Code: 9326</h1>
<table>
<tr>
<td>Nominee: 3</td><td>Nominee: 4</td>
</tr>
</table>
<h1>Multi Code: 8746</h1>
<table>
<tr>
<td>Nominee: 5</td>
</tr>
</table>
Here is my SQL + PHP so far:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5'");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
print "<h2>$multiCode</h2>";
if ($multi_code != $multiCode) {
print "<table><tr>";
$multi_code = $multiCode;
}
print "<td>Nominee: $nomineeID</td>";";
}
</tr></table>
With this I get this:
8746
9326
Nominee: 3
9326
Nominee: 4 Nominee: 5
Why am I getting the 9326 above the Nominee 3?
First of all, I suggest to order your results by MULTI_CODE to have the correct sorting when looping through the results.
Then, in your loop you always print the headline with the multi code, regardless being different to the previous one.
Please have a look at this code. It isn't tested, so please be aware that there might be errors in it:
$nomineedetails = mysqli_query($con,"SELECT AWARD_ID, NOMINEE_ID, VOTER_ID, MULTI_CODE
FROM b_awards_votes WHERE AWARD_ID = '5' ORDER BY MULTI_CODE ASC");
$multi_code = -1;
while($row = mysqli_fetch_array($nomineedetails))
{
$awardID = $row['AWARD_ID'];
$nomineeID = $row['NOMINEE_ID'];
$voterID = $row['VOTER_ID'];
$multiCode = $row['MULTI_CODE'];
if ($multi_code != $multiCode) {
if($multi_code !== -1) {
print "</table>";
}
print "<h2>$multiCode</h2>";
print "<table>";
$multi_code = $multiCode;
}
print "<tr><td>Nominee: $nomineeID</td></tr>";
}

Categories