PHP: Fill fixed size chart with mysql data - php

I have a table chart. Lets say 5 by 5. I run a loop such as
<table>
<tbody>
<?php for ($i = 0; $i < 5; $i += 5) {
echo "<tr>
<td>one box</td>
<td>one two</td>
<td>one three</td>
<td>one four</td>
<tr>";
}?>
</tbody>
</table>
It creates a table such as
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
Now I have mysql data I load for my purposes and I need it to put the data in respectively so the table looks like
| | | | | |
-------------------------------------
| | | | | |
-------------------------------------
| | Res 1| | | |
-------------------------------------
| | | | Res 3| |
-------------------------------------
| |Res 4 | | | Res 2|
-------------------------------------
How would I do this? I have 50 results and need to fill the results into the correct column and row. I need to do some sort of if(results[0-50]['id'] == rowcolumnid) echo the results for the correct table while doing the for loop.
Edit: Here is my full code.
<table id="schedule">
<tbody>
<?php
$time = mktime(0, 0, 0, 1, 1);
for ($i = 28800; $i < 62200; $i += 1800) { ?>
<tr id="row<?php echo $i; ?>">
<td id="hour">
<?php
printf('%1$s',date('g:i a', $time + $i));
?>
</td>
<td id="sunday"></td>
<td id="monday"></td>
<td id="tuesday"></td>
<td id="thursday"></td>
<td id="friday"></td>
<td id="saturday"></td>
</tr>
<?php } ?>
</tbody>
</table>
My mysql results are in datetime format that I'm going to use to propogate the table.
Results:
ID| EVENT NAME | DATEOFEVENT
1 | event name | 2012-11-20 12:00:00
2 | event name | 2012-11-21 13:30:00
3 | event name | 2012-11-22 13:00:00
4 | event name | 2012-11-23 11:00:00
5 | event name | 2012-11-24 08:00:00
etc.
I can do a strtotime of the dates and a date command to match.

If you first fetch the data (or if it is sorted if you first fetch the first data), then you can just iterate over the data when you match the hour/time that you iterate over to draw the table.
As an example, I've chosen to display only one column that represents 5 hours (1-5) of which some can be matched. Those that are matched, are stored in an array and made available as an iterator (ArrayIterator):
$data = [3,5];
$datas = new ArrayIterator($data);
$datas->rewind();
Matched hours are represented with 1, unmatched ones with 0:
echo "+---+---+\n";
foreach(range(1, 5) as $hour)
{
if ($hasData = ($datas->valid() and $datas->current() === $hour)) {
$datas->next();
}
$hasData = (int) $hasData;
echo "| $hour | $hasData |\n";
echo "+---+---+\n";
};
Output:
+---+---+
| 1 | 0 |
+---+---+
| 2 | 0 |
+---+---+
| 3 | 1 |
+---+---+
| 4 | 0 |
+---+---+
| 5 | 1 |
+---+---+
This works perfectly if the data from the data is available as an iterator (often the case, for mysql_* you need to write you one) and if it is sorted.
Even this is only a single list here, for a table this works actually equally because a table is just a different form of representing the data.

Related

How to do a calculation with a column in database and insert the result Codeigniter

Basically i have to count the monthly meal allowance for each of the employee based on their monthly attendance, which only leave me with just one table, attendance table
The calculation for the meal allowance is pretty simple, i only need to count how many days the employee attended and calculate it with one number (this number should be dynamic or editable)
this is the example of the calculation:
number of attendance * 15.00 (editable number)
here's my data for attendance table
----------------------------------------------------------------------------
| attendance_id | emp_code | emp_name | date | time_in | time_out |
| 1 | ALB | Albert | 2018.01.01 | 07.00 | 18.00 |
| 2 | GlN | GLENN | 2018.01.01 | 07.00 | 18.00 |
| 3 | ALB | Albert | 2018.01.02 | 07.00 | 18.00 |
| 4 | GLN | GLENN | 2018.01.02 | 07.00 | 18.00 |
| 5 | ALB | Albert | 2018.01.04 | 07.00 | 18.00 |
| 6 | GLN | GLENN | 2018.01.04 | 07.00 | 18.00 |
----------------------------------------------------------------------------
So far i already managed to count the monthly recap (by counting how many days they attended), this is my current condition:
------------------------------------------------
| emp_code | emp_name | days_attended |
| ALB | Albert | 3 |
| GLN | GLENN | 3 |
-------------------------------------------------
This is my controller
public function index(){
$this->load->model('allowance_m');
$data['query'] = $this->allowance_m->calculate();
$this->load->vars($data);
$this->load->view('allowance_v',$data);
}
This is my model (allowance_m.php)
public function hitungabsen()
{
$this->db->select('attendance_id,emp_code,emp_name, COUNT(date) as days_attended');
$this->db->from('attendance');
$this->db->group_by('emp_code');
$query = $this->db->get();
return $query->result();
}
This is my view allowance_v.php
<table class="table table-striped table-bordered table-hover">
<thead>
<tr>
<th><center>Employee Code</center></th>
<th><center>Employee Name</center></th>
<th><center>Days Attended</center></th>
</tr>
</thead>
<?php
$no = 1;
foreach($query as $row){
?>
<tr>
<td><?php echo $row->emp_code ?></td>
<td><?php echo $row->emp_name ?></td>
<td><?php echo $row->days_attended ?></td>
</tr>
<?php } ?>
</table>
Now i really need to know how can i calculate the meal allowance with the inputted days attended, and after calculating the result, i need to insert the result into the database so that i can make a pdf report about that. the result should be like this:
------------------------------------------------------------------------
| emp_code | emp_name | days_attended | meal allowance |
| ALB | Albert | 3 |(automaticaly updated) |
| GLN | GLENN | 3 |(automaticaly updated) |
------------------------------------------------------------------------
THANK YOU IN ADVANCE! Really need the answer as soon as possible, it means a lot for me
Don't really understand everything but this should work:
public function calc() {
$meal_allowance = array_column($this->db->select('emp_code')->get('tablename')->result_array(), 'emp_code');
$n = 15;
$results = $this->hitungabsen();
$this->db->trans_start();
foreach ($results as $row) {
$calc = intval($row->days_attended) * $n;
if (in_array($row->emp_code, $meal_allowance)) {
$this->db->update('tablename', array('meal_allowance' => $calc));
} else {
$data = array(
'emp_code' => $row->emp_code,
'emp_name' => $row->emp_name,
'days_attended' => $row->days_attended,
'meal_allowance' => $calc
);
$this->db->insert('tablename', $data);
}
}
$this->db->trans_complete();
return $this->db->trans_status();
}
Updates if a record exists, inserts if it doesn't (assumed separate table with the specs in your last graph)

Number auto increase with other while function (PHP)

I want the queqe id auto increase start from 1
I have an mysql table call t1
mysql table t1 Data as below:
+----------+------------------+-------------+
| ID | Name | Status |
+----------+------------------+-------------+
| 1 | ABBCCC | 1 |
| 2 | BASDASD | 1 |
| 3 | ABBCCC | 1 |
| 4 | ABBCCC | 2 |
+-------------------------------------------+
I loop data in php like this:
$quserCA = DB::query("SELECT * FROM ".DB::table('jnbook_book')." WHERE Name = 'ABBCCC' ORDER BY id DESC LIMIT 20");
$nqCA = mysql_num_rows($quserCA);
while($ruserCA = DB::fetch($quserCA)){
$CAlist[] = $ruserCA;
}
$x = 1;
while($x <= $nqCA) {
//echo "The number is: $x <br>";
$x++;
}
I loop this in my htm like this:
<table>
<tr>
<td>Queqe ID</td><td>ID</td><td>Status</td>
</tr>
<!--{loop $CAlist $value}-->
<tr>
<td>{$x}</td><td>{$value[id]}</td><td>{$value[status]}</td>
</tr>
<!--{/loop}-->
</table>
But after that my table output as below show
+---------------+-------------------+----------------+
| Queqe ID | ID | Status |
+---------------+-------------------+----------------+
| 1 | 1 | 1 |
| 1 | 3 | 1 |
| 1 | 4 | 2 |
+----------------------------------------------------+
Actually what I want the table output as below
(I want the queqe id auto increase start from 1):
+----------+-----------------+-----------------+
| Queqe ID | ID | Status |
+----------+-----------------+-----------------+
| 1 | 1 | 1 |
| 2 | 3 | 1 |
| 3 | 4 | 2 |
+----------------------------------------------+
Thank you.
This should be done something like:
$x = 1;
while($ruserCA = DB::fetch($quserCA)){
// add a field, say `x` with number of a record:
$ruserCA['x'] = $x++;
$CAlist[] = $ruserCA;
}
In a template:
<td>{$value[x]}</td><td>{$value[id]}</td><td>{$value[status]}</td>

How to format data in specific way using html and php

I have a table that contains Aspiring team for particular positions and various vote casted.
The data is below
Teamtable
| no | team | position | votes Cast |
| 1 | A | President | 2 |
| 3 | B | President | 1 |
| 4 | C | Secretary | 2 |
| 6 | D | Secretary | 1 |
I want to be able to get this in an html format using php and mysql just as below
EXPECTED VIEW IN THE HTML AND PHP FORMAT
PRESIDENT
Team | Total Votes | Percentage |
A | 2 | 66.67 |
B | 1 | 33.33 |
SECRETARY
Team | Total Votes | Percentage |
C | 2 | 66.67 |
D | 1 | 33.33 |
This is what i have tried so far
//QUERY
$SQL=SELECT
`team`,
`position`,
`votesCast`
FROM
Teamtable
$Results=$db->query($SQL);
$data=array();
while($row=mysqli_fetch_assoc($Results)){
$team=$row['team'];
$position=$row['position'];
$totalVote=$row['votesCast'];
$data[$position][]=$position;
$data1[$position][]=$team;
$data2[$position][]=$totalVote;
}
foreach($data as $position =>$electionResults){
$teams=$data1[$position];
$totalVotes=$data2[$position];
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "
<table>
<tr>
<td>$teas</td>
<td>$votes</td>
</tr>
</table>";
}
}
I have to tried up to this point, any help is appreciated.
This could be very helpful for you
while($row = mysqli_fetch_assoc($result)) {
$data[$row['position']]['total'][]=$row['votes'];
$data[$row['position']][]=$row;
}
foreach($data as $k=>$v){
echo '<p>'.$k.'</p>';
echo '<table border="1">';
$total_votes=array_sum($v['total']);
foreach($v as $kk=>$vv){
if($kk!=='total'){
$percentage=round($vv['votes']/$total_votes*100,2);
echo '<tr><td>'.$vv['tean'].'</td><td>'.$vv['votes'].'</td><td>'.$percentage.'%</td></tr>';
}
}
echo '</table>';
}
You wan't to have the table on the outside of the foreach.
echo "<table>";
foreach($teams as $re => $teas){
$votes=$totalVotes[$re];
echo "<tr>
<td>$teas</td>
<td>$votes</td>
</tr>";
}
echo "</table>";

display table group by category using php

my database design is like this
-- Table Reason
--------------------------
| reasonid | reasonname |
--------------------------
| 1 | reason1 |
| 2 | reason2 |
--------------------------
-- Table Student
----------------------------
| studentid | studentname |
----------------------------
| 1 | John |
| 2 | Jane |
| 3 | Hulk |
----------------------------
-- Table form
-----------------------------------
| formid | studentid | reasonid |
-----------------------------------
| 1 | 1 | 1 |
| 2 | 2 | 2 |
| 3 | 3 | 1 |
-----------------------------------
I want to show data table like :
reason1
| 1 | John |
| 2 | Hulk |
reason2
| 1 | Jane |
I have tried below code but the result is not groupBy reason
<?php $i =1;
while($rowfet = mysql_fetch_array($myselect)){ ?>
<h2><?php echo $rowfet['ReasonName']; ?></h2>
<table>
<thead>
<tr class="active">
<th>No.</th>
<th>StudentName</th>
</tr>
</thead>
<tbody>
<tr>
<td align="center"><?php echo $i; ?></td>
<td><?php echo $rowfet['STUDENTNAME']; ?></td>
</tr>
</tbody>
</table>
<?php $i++; } ?>
the result of this code is :
reason1
| 1 | John |
reason1
| 2 | Hulk |
reason2
| 3 | Jane |
You could do something like this:
outside while loop:
$lastReasonId = '';
inside while loop:
if($rowfet['reasonid'] != $lastReasonId){
// show heading
}
$lastReasonId = $rowfet['reasonid'];
But your code is far from ok. The $i++ thing is not doing anything, you should probably echo $rowfet['studentid'] there.
And you are using deprecated mysql code.

Efficient php/mysql query to generate a html table in php

I have 3 tables in my MySql database.
-------------
| countries |
-------------
| iso |
| name |
-------------
--------------------
| documents |
--------------------
| id |
| country_iso |
| publication_date |
--------------------
---------------
| files |
---------------
| document_id |
| name |
---------------
Could you suggest a mysql query and php function to print this to a table as below.
Where d = documents->files->name, each year can have many document for a particular country.
-----------------------------------------
| | 2009 | 2008 | 2007 | 2006 |
-----------------------------------------
| country a | d d | | d | d |
| country b | | d | d | |
| country c | d | d d | | d |
| country d | d | | d d | |
-----------------------------------------
Thanks in advance
If there can be only one file per each document, you should combine the documents and files table so that the documents table has a filename field.
This example assumes one-to-one relation between files and documents. Instead of creating exotic SQL-queries, it's simpler to just select plain data from DB and transform it using PHP.
SELECT
countries.name country,
documents.name document,
files.name filename,
DATE_FORMAT(publication_date, '%Y')
FROM
documents
LEFT JOIN
countries
ON
documents.country_iso = countries.iso
LEFT JOIN
files
ON
documents.id = files.document_id
That will result in a followin table from the database:
country | document | filename | year
----------------------------------------------------
Germany | doc_1 | doc_1.pdf | 2009
Germany | doc_2 | doc_2.pdf | 2007
Germany | doc_3 | doc_3.pdf | 2007
Norway | doc_6 | doc_6.pdf | 2008
...
Next that table has to be transformed with PHP to proper form. The proper form in this case would be that all documents are indexed by year which are in turn indexed by customer:
$q = mysql_query("SELECT ...");
$result = array();
$years = array();
while($row = mysql_fetch_assoc($q)) {
$result[$row['country']][$row['year']][] = array('document' => $row['document'], 'filename' => $row['filename']);
$years[] = $row['year'];
}
// Max and min years are used to print the table header
$max_year = max($years);
$min_year = min($years);
Now, if you would like to find Germany's documents for year 2009, you could just use $result['Germany'][2009];. But you can use a nifty looping to print the table automatically:
<table>
<tr>
<th>Country</th>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<th><?php echo $y; ?></th>
<?php endfor; ?>
</tr>
<?php foreach($result as $country => $years): ?>
<tr>
<td><?php echo $country; ?></td>
<?php for($y = $max_year; $y >= $min_year; $y--): ?>
<td>
<?php if(isset($years[$y])): foreach($years[$y] as $document): ?>
<a href="<?php echo $document['filename']; ?>">
<?php echo $document['name']; ?>
</a>
<?php endforeach; endif; ?>
</td>
<?php endfor; ?>
</tr>
<?php endforeach; ?>
</table>
There you go. Note that I haven't tested this, there might be parse errors and some other illogicalities but I guess you get the idea from this.

Categories