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
Related
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
I have an array of data and want to display it on a table, but the columns that appear in the table according to the number of array data, how to make 12 column array default but the number remains on the database. suppose there are 2 data appear in the table but there are a number of columns 12...?
Models
function get_id($id) {
$this->db->where('my_id', $id);
$get_data = $this->db->get('mytable');
if ($get_data->num_rows() > 0) {
foreach ($get_data->result() as $data) {
$my_result[] = $data;
}
return $my_result;
}
}
Controllers
public function myControllers() {
$id = $this->uri->segment(4);
$data=array('detail' => $this->mymodels->get_id($id));
$this->load->view('layout/wrapper', $data);
}
Views
<table>
<thead>
<tr>
<td>No</td>
<td>ID</td>
<td>Name</td>
<td>Class</td>
</tr>
</thead>
<tbody>
<?php
$no = 1;
foreach ($detail as $row) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td>'.$row->id.'</td>';
echo '<td>'.$row->name.'</td>';
echo '<td>'.$row->class.'</td>';
echo '</tr>';
$no++;
}
?>
</tbody>
</table>
I expected as this example
NO | ID | NAME | ClASS |
____|____ |______|_______|
1 | 001 | Paul | x |
2 | | | |
3 | | | |
4 | | | |
to 12
If I'm understanding your question, you want to have a default of 12 rows in your table, even if only 2 rows are filled with data.
What about adding a while loop after your foreach loop that continues incrementing your counter and adding rows until your counter reaches 12?
while ($no <= 12) {
echo '<tr>';
echo '<td>'.$no.'</td>';
echo '<td></td>';
echo '<td></td>';
echo '<td></td>';
echo '</tr>';
$no++;
}
my Database table field look like this
id - name - school - subjects - marks
1 - Adam - Highschool - Geography,physics,math - 60,50,40
2- - Jhone - elementry - Math,Language,Geography - 90,20,10
the php file should convert this values into html table using boot strap
my expected reslut to be like this
Hello Jone! this is your results
Math : 90
Language : 20
Geography : 10
I tried different methods like convert the result into array but it did work with me will.
one of them are
<?php
$shop = array( array("title"=>"rose", "price"=>1.25 , "number"=>15),
array("title"=>"daisy", "price"=>0.75 , "number"=>25),
array("title"=>"orchid", "price"=>1.15 , "number"=>7)
);
?>
<?php if (count($shop) > 0): ?>
<table>
<thead>
<tr>
<th><?php echo implode('</th><th>', array_keys(current($shop))); ?></th>
</tr>
</thead>
<tbody>
<?php foreach ($shop as $row): array_map('htmlentities', $row); ?>
<tr>
<td><?php echo implode('</td><td>', $row); ?></td>
</tr>
<?php endforeach; ?>
</tbody>
</table>
<?php endif; ?>
any advice
thanks
It will be great to normalize your data and to avoid any comma separated lists in your database. However you can achieve this very easily using explode():
Input:
mysql> select * from marks;
+----+------+-------------+-------------------------+----------+
| id | name | school | subjects | marks |
+----+------+-------------+-------------------------+----------+
| 1 | Adam | High School | Geography,physics,math | 60,50,40 |
| 2 | Jone | Elementary | Math,Language,Geography | 90,20,10 |
+----+------+-------------+-------------------------+----------+
2 rows in set
PHP code:
<?php
$con = mysqli_connect('localhost','root','','test') or die(mysqli_error($con));
$query = "SELECT * FROM marks";
$result = mysqli_query($con,$query) or die(mysqli_error($con));
if(mysqli_num_rows($result) > 0){
while($row = mysqli_fetch_assoc($result)){
$subject = explode(',',$row['subjects']);
$marks = explode(',',$row['marks']);
echo 'Hello '.$row['name'].'! This is your results:';
echo '<table>';
foreach($subject as $k=>$s){
echo '<tr>
<td>'.$s.'</td>
<td>'.$marks[$k].'</td>
</tr>';
}
echo '</table>';
}
}
?>
Output is table for each user:
Hello Adam! This is your results:
Geography 60
physics 50
math 40
Hello Jone! This is your results:
Math 90
Language 20
Geography 10
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>";
}
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