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
Related
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
I have a table that consist of records and i want to retrieve rows with minimum price. I try the code below, and i am only able to retrieve only one rows. i need to return all rows with minimum price for the same partno.
<tr>
<th>S/N</th>
<th>Part Number</th>
<th>Description </th>
<th nowrap="nowrap">Recommended Price</th>
<th nowrap="nowrap">Recommended Supplier</th>
</tr>
<?php
$get = mysqli_query($con,"SELECT MIN(price)As price,partno,supplier,description FROM tab_stockqty2 ");
$c=0;
while($rw = mysqli_fetch_array($get)){
$c++;?>
<tr>
<td nowrap="nowrap"><?php echo $c;?></td>
<td nowrap="nowrap"><?php echo $rw['partno'];?></td>
<td nowrap="nowrap"><?php echo $rw['description'];?></td>
<td><?php echo number_format($rw['price'],2);?></td>
<td><?php echo $rw['supplier'];?></td>
</tr>
<?php };?>
See Database Record:
It show return two rows, but it is only retuning only one row for the first set of partno-2070081
To retrieve all records with minimal price, per partno, the query should be something like this:
SELECT s.price,s.partno,s.supplier,s.description FROM tab_stockqty2 s
INNER JOIN (
SELECT MIN(price) as m, partno FROM tab_stockqty2 GROUP BY 2
) m on s.price = m.m and s.partno = m.partno
Demo: http://sqlfiddle.com/#!9/3e089/1
try this ==>
SELECT MIN(price)As price,partno,supplier,description FROM tab_stockqty2 group by partno
The SQL MIN returns only 1 row which is the most minimum.
What you can do is fetch all records and sort them on price based on ascending order.
SELECT price,partno,supplier,description FROM tab_stockqty2 ORDER BY price ASC;
Try this instead.
In this way you will get records based on price from Low to High.
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";
}
}
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
For my current progress I need to create a table like the one below
pid cid eid name value
1 1 4 name ab
2 1 5 amt 2
3 1 4 name cd
4 1 5 amt 4
Instead of creating the table like this
pid cid name amt
1 1 ab 22
2 1 cd 4
Anyhow created table as my wish with the below code
<table width="1204" height="100" border="1">
<tr>
<?php $sqlname="Select * From elements where cat_id='1' order by e_id ";
$resname=mysql_query($sqlname);
while($rowname=mysql_fetch_array($resname)){
?>
<td colspan="2"><?php echo $rowname['lable_name']; ?></td>
</tr>
<tr>
<?php $i=0;
$sqlprolist="select value from products_list where name='".$rowname['lable_name']."' and e_id='".$rowname['e_id']."'";
$resprolist=mysql_query($sqlprolist);
while($rowprolist=mysql_fetch_array($resprolist)){
$i++;
?>
<td><?php echo $rowprolist['value'];?></td>
<?php if($i%8==0){
?>
<tr></tr>
<?php }?>
<?php }?>
</tr>
<?php }?>
</table>
But I don't have any idea to retrieve data from the table for processing.
thanks
as by following martin the table created as like the below table
pid cid eid name value
12 1 4 name abc
1 1 4 name cde
13 1 5 code 12
2 1 5 code 14
how to split up the data
like
name code breeder quality size
abc 12 121 121 22
acfd 34 164 22 22
thanks
It's difficult to help you without seeing database structure. Please share it with us, you might get better answers.
Anyway, I suppose you have two tables, elements and products_list. It looks like you need to lookup the value column in the products_list for every row in the elements table. You can merge these table into one result set using a single SQL query:
SELECT e.p_id, e.cat_id, e.e_id, e.lable_name, p.value
FROM elements e, products_list p
WHERE e.cat_id='1' AND
p.name = e.lable_name AND p.e_id = e.e_id
ORDER BY by e.e_id
Note that the e.p_id is just a guess, you have not shared with us, where the "pid" column value gets from. Also not sure, if you actually need to match the rows using the p.name = e.lable_name. If e_id is primary key, you might do with p.e_id = e.e_id only.
What's the point of cid column? If it is indeed the cat_id column in database, why do you need it in HTML table, if you filter the elements to cat_id=1 anyway? Was that intentional?
You can now take the results of the query and simply output it to a HTML table row by row like:
<table width="1204" height="100" border="1">
<tr>
<th>pid</th>
<th>cid</th>
<th>eid</th>
<th>name</th>
<th>value</th>
</tr>
<?php
$sqlname =
"SELECT e.p_id, e.cat_id, e.e_id, e.lable_name, p.value ".
"FROM elements e, products_list p ".
"WHERE e.cat_id='1' AND ".
"p.name = e.lable_name AND p.e_id = e.e_id".
"ORDER BY e.e_id";
$resname = mysql_query($sqlname);
while ($row = mysql_fetch_array($resname))
{
?>
<tr>
<td><?php echo $row['p_id'];?></td>
<td><?php echo $row['cat_id'];?></td>
<td><?php echo $row['e_id'];?></td>
<td><?php echo htmlspecialchars($row['lable_name']);?></td>
<td><?php echo $row['value'];?></td>
</tr>
<?php
}
?>
</table>
This is just a first step. It's unlikely this is correct answer. But I hope it helps you understand, what are you trying to do and to your question.
This is hardly production-grade code. But I wanted to do as little changes as possible, so you can see, what is the actual change from your code.
Also note that the mysql_ functions are deprecated. See the big red box on PHP documentation page.