Merge and display data from different table with PHP - php

I'm editing a program in PHP and I want to display a table with data from different table.
Now I have 2 tables and I am only printing the first table (registro_eu) with this code:
$sql = "SELECT * FROM registro_eu WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
echo '<div class="table-responsive">';
echo '<table class="table table-striped">';
echo '<thead>
<tr>
<th>Date</th>
<th>Desc</th>
</tr>
</thead>
<tbody>';
while($row = $result->fetch_assoc()) {
echo '
<tr>
<td>'. $row["data"].'</td>
<td>'. $row["desc"].'</td>
</tr>';
}
echo '</tbody>';
echo '</table>';
echo '</div>';
}
TABLE REGISTRO_EU:
ID | DATE | DESC
-----------------------------
1 | 2016-06-15 | TEST DESCT
2 | 2016-06-15 | TEST 2
3 | 2016-06-15 | TEST 3
4 | 2016-06-16 | TEST DESCT
5 | 2016-06-16 | TEST 4
6 | 2016-06-17 | TEST 5
TABLE FONDI:
ID | DATE | TOT
-----------------------------
1 | 2016-06-15 | 88
2 | 2016-06-16 | 100
3 | 2016-06-17 | 120
And I want to display to user (with php) something like this:
DATE | DESC |TOT
-------------------------------
2016-06-15 | TEST DESCT |
2016-06-15 | TEST 2 |
2016-06-15 | TEST 3 |88
2016-06-16 | TEST DESCT |
2016-06-16 | TEST 4 |100
2016-06-17 | TEST 4 |120

Use joins:
$sql = "SELECT * FROM registro_eu LEFT JOIN fondi ON registro_eu.DATE = fondi.DATE WHERE MONTH(data_conv) = 7 AND YEAR(data_conv) = 2016";
you will have to disambiguate which table you're talking about in the WHERE clause but I don't know your schema so I'll leave that to you.
https://en.wikipedia.org/wiki/Join_(SQL)

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)

Fetching/Putting 2 variables in a mysqli_num_row and mysqli_fetch_array in PHP

Basically I want to do a search by category and title. My problem is that the two are located in separate tables. I was thinking of putting 2 variables in the mysqli_num_rows or mysqli_fetch_array but I don't think it's the right idea. The $search variable is working already but I don't know what I will do for $searchcat that is a different table.
<table border="1">
<tr>
<th>ID</th>
<th>Survey Title</th>
<th>Category</th>
</tr>
<?php
if (isset($_POST['submit']))
{
include 'testdb.php'; //connection is written in other page (db.php)
$var =$_POST['search'] ;
$searchtype = $_POST['searchtype'];
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
echo "<h3>Search results:</h3>";
while($show = mysqli_fetch_array($get_data))
{
$id = $show['survey_id'];
$title = $show['title'];
$category = $show['categoryname']; //
echo "<tr align='center'>";
echo "<td><font color='black'>" .$id. "</font></td>";
echo "<td><font color='black'>" .$title. "</font></td>";
echo "<td><font color='black'>" .$category. "</font></td>";
}
}
else{
echo "No Records found!";
}
}
?>
</table>
</body>
This is table category (categoryname is what I need)
+-------------+---------------+-------------+
| category_id | categoryname | datecreated |
| 1 | Philosophical | |
| 4 | Political | |
| 6 | Social | |
This is table survey (title is all I need)
| 1 | survey_id | title | description | duration | gender | age_group_from | age_group_to |
| 2 | 44 | game1 | description1 | 5 | male | 0 | 18 |
| 3 | 45 | game2 | description2 | 25 | female | 18 | 25 |
| 4 | 46 | game3 | description3 | 89 | female | 26 | 35 |
This is table survey_header (survey_id and category_id is what I need)
| 1 | survey_id | date_created | date_updated | opening_date | closing_date | category_id | topic_id |
| 2 | 33 | Not important | Not important | NULL | NULL | 1 | NULL |
| 3 | 45 | Not important | Not important | NULL | NULL | 6 | NULL |
| 4 | 46 | Not important | Not important | NULL | NULL | 4 | NULL |
Try this query :
$my_query="SELECT s.survey_id, s.title,c.categoryname
FROM survey_header as sh
JOIN survey AS s ON sh.survey_id=s.survey_id
JOIN category AS c ON sh.category_id=c.category_id
WHERE $searchtype LIKE '%".$var."%'
ORDER BY title ASC";
$get_data= mysqli_query($con, $my_query) or die ("Couldn't execute query: ".mysqli_error());
if (mysqli_num_rows($get_data) > 0 )
{
/*create table*/
}
else
// do something else

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>";

Displaying mysql returns in clean and professional tabular report

This is my second question (so far) this week. My first one achieved a successful solution so I thought this one could also get lucky.
I want to layout/design the returns my query outputted me in this way:
date 1 | id | time | updated by | Status Update
| 1 | 04:06 | user1 | this is a remark
| 2 | 05:05 | user2 | this is again a remark
| 3 | 05:08 | user1 | this is another remark
| 4 | 07:09 | user3 | and this one also
date 2 | id | time | updated by | Status Update
| 5 | 04:06 | user3 | this is a remark for day 2
| 6 | 05:05 | user3 | this is again a remark for day 2
date 3 | id | time | updated by | Status Update
| 7 | 04:06 | user1 | this is a remark
| 8 | 05:05 | user2 | this is again a remark
| 9 | 05:08 | user2 | this is another remark
| 10 | 07:09 | user3 | and this one also
Here is my php codes...
mysql declaration here
{
echo "<table border='0' align=center class='tablefont'>
<tr class='colheadbg'>
<th>Date</th>
<th>ID</th>
<th>Time</th>
<th>Updated by</th>
<th>Status Update</th>
</tr>";
while($info = mysql_fetch_array( $data ))
{
$id_num=$id_num + 1;
echo "<tr>
<td>".$info['date']."</td>
<td>$id_num</td>
<td>".$info['time']."</td>
<td>".$info['user']."</td>
<td>".$info['remarks']."</td>";
echo "</tr>";
}
echo "</table>";
}
?>
So far, this outputs a tabular form report which is not what I really want. I've heard this "group" but I really need to have a good starter-idea to get it going.
Make a $date variable in your function and then on every iteration compare it to the current rows' date like so:
var $date = "";
while($info = mysql_fetch_array( $data )) {
$id_num=$id_num + 1;
echo "<tr>";
if ($date==$info['date']) {
echo "<td>".$info['date']."</td>
<td>$id_num</td>
<td>".$info['time']."</td>
<td>".$info['user']."</td>
<td>".$info['remarks']."</td>";
}
else {
$date=$info['date']; //set it for the new date first
//do your row magic
}
echo "</tr>";
}
Updated from OPs screenshot
var $date = "";
while ($info = mysql_fetch_array( $data )) {
$id_num++;
echo "<tr>";
if ($date == $info['date']) {
echo "<td>"./**nothing**/."</td>";
echo "<td>$id_num</td>
<td>{$info['time']}</td>
<td>{$info['user']}</td>
<td>{$info['remarks']}</td>";
}
else {
$date = $info['date']; //set it for the new date first
echo "<td>$date</td>
<td>$id_num</td>
<td>{$info['time']}</td>
<td>{$info['user']}</td>
<td>{$info['remarks']}</td>";
}
echo "</tr>";
}
Based on what you're showing in your screenshot, it looks like that might fix it.

How to echo specific data in loop with a condition specific to the last row

For an accounting system, I'm using PHP & MySQL. I've two tables "GROUP" and "ACHEADS".
In the GROUP table, I have:
---------------------
| id (AI) | group |
---------------------
| 1 | Group 1 |
| 2 | Group 2 |
---------------------
In the ACHEADS table, I have:
-----------------------------------------
| id (AI) | ac_head | amount | j_id |
-----------------------------------------
| 1 | Something 1 | 2000 | 1 |
| 2 | Something 2 | 1000 | 1 |
| 3 | Something 3 | 5000 | 2 |
| 4 | Something 4 | 4000 | 2 |
| 5 | Something 5 | 8000 | 2 |
-----------------------------------------
I've joined the two tables as GROUP.id <<->> ACHEADS.j_id
Now I need to preview the data like this:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | |
Something 4 | 4000 | |
Something 5 | 8000 | 17000 |
----------------------------------------------
GRAND TOTAL | | 20000 |
------------------------------------==========
Challenges
The table will be dynamic and will generate within a PHP loop (I'm
using a WHILE loop)
Remember: it's a table and if I miss echoing a td, then the table will break up
Problems
When I'm using the loop it's echoing the data on the Details td
accurately. But the sum of the details row according to j_id is also
echoing in each td
Preview here:
----------------------------------------------
Particulars | Details | Total |
----------------------------------------------
Group 1 | | |
Something 1 | 2000 | 3000 |
Something 2 | 1000 | 3000 |
----------------------------------------------
Group 2 | | |
Something 3 | 5000 | 17000 |
Something 4 | 4000 | 17000 |
Something 5 | 8000 | 17000 |
----------------------------------------------
My thoughts
If I can check whether it is the last data of the query, if isset,
then echo the total amount with it's td. (But remember the
Challenge#2)
Does it require a foreach loop?
I failed
I tried checking max(id), it works fine in SQL, but can't use it in
condition within a loop.
(If you still can't understand me, then on the second phase, I'll post my code.)
I would do 2 loops:
Fetch id from GROUP
Fetch amount from ACHEADS based on j_id
This would look something like (non-tested code):
echo '<table><tr><td>Particulars</td><td>Details</td><td>Total</td></tr>';
$total = 0;
$q1 = "SELECT id FROM `GROUP`";
$res1 = mysqli_query($q1);
while($row1 = mysqli_fetch_assoc($res1)) {
echo
$group_total = 0;
$j_id = $row1[id];
$q2 = "SELECT ac_head, amount FROM ACHEADS WHERE j_id = $j_id";
$res2 = mysqli_query($q2);
while($row2 = mysqli_fetch_assoc($res1)) {
echo '<tr><td>' . $row2[ac_head] . '</td>';
echo '<td>' . $row2[amount] . '</td></tr>';
$group_total = $group_total + $row2[amount];
$total = $total + $row[amount];
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
echo '<tr><td>GRAND TOTAL</td>';
echo '<td colspan="2" align="right">' . $total . '</td></tr>';
echo "</table>";
njk rockz!
It worked nicely. Thanks a lot, brother - it helped me a lot, I can't explain.
Here is my final code:
<tr style="background: #000; color:#fff;">
<th style="width:150px;">Particulars</th>
<th>Details</th>
<th>Amount</th>
</tr>
<tr>
<td>Opening Balance</td>
<td></td>
<td>500000</td> <!-- till not dynamic -->
</tr>
<?php
$total = 0;
$se = "SELECT * FROM group";
$res = mysql_query($se) or die (mysql_error());
while ($row = mysql_fetch_array($res))
{
?>
<tr>
<td colspan="3" style="font-weight:bold;"><?php echo $row['group']; ?></td>
</tr>
<tr>
<?php
$group_total = 0;
$se1 = "SELECT ac_head, amount FROM `acheads` WHERE `j_Id` = '".$row['id']."'";
$res1 = mysql_query($se1) or die (mysql_error());
while ($row1 = mysql_fetch_array($res1))
{
$group_total = $group_total + $row1['amount'];
?>
<td><?php echo $row1['ac_head']; ?></td>
<td><?php echo $row1['amount']; ?></td>
<td> </td>
</tr>
<?php
}
echo '<tr><td colspan="3" align="right">' . $group_total . '</td></tr>';
}
?>
</table>
</code>

Categories