So I have this tables on my database that I have to display as table in html. The problem is I do not know how I am going to inline my td with my th and add zero to the person who doesn't have allowance.
This is the image of my allowance table
.
So far this is what i have:
<?php
error_reporting(0);
include "./includes/connectdb.php";
$emp = "select * from employee";
$q = $dbhandle->query($emp);
if($q->num_rows>0){
$all = "select distinct allowance from emp_allowances order by allowance asc";
$a = $dbhandle->query($all);
if($a->num_rows>0) {
while($b = $a->fetch_assoc()){
$thallowance .= '<th>'.$b['allowance'].'</th>';
$empallowance = $b['allowance'];
}
}
while($r = $q->fetch_assoc()){
$id= $r['id'];
$name= $r['name'];
$all2 = "select * from emp_allowances order by allowance asc";
$c = $dbhandle->query($all2);
if($c->num_rows>0){
$tdallow='';
while($d = $c->fetch_assoc()){
$empid = $d['emp_id'];
$empallowance = $d['allowance'];
$amount = $d['amount'];
if($empid==$id){
$tdallow.='<td>'.$amount.'</td>';
}
}
}
$tbody .= '<tr>
<td>'.$name.'</td>
'.$tdallow.'
</tr>';
}
}
$thead = '
<thead>
<tr>
<th>Name</th>
'.$thallowance.'
</tr>
</thead>
';
?>
<table border=1>
<?php echo $thead; ?>
<tbody>
<?php echo $tbody; ?>
</tbody>
And from this code i got this result.
I changed your while loop, try to copy it and see if works.
The $tdarray sets all allowances to zero, if some of them has a greater value it is set in the employee allowances loop. This way if there is no allowance for an employee there will be zero and properly inline, because the default values (zeros) are preset.
Let me know if works.
<?php
error_reporting(0);
include "./includes/connectdb.php";
$emp = "select * from employee";
$q = $dbhandle->query($emp);
if($q->num_rows>0){
// array that will hold key value pairs for allowances
$tdarray = array();
$all = "select distinct allowance from emp_allowances order by allowance asc";
$a = $dbhandle->query($all);
if($a->num_rows>0) {
while($b = $a->fetch_assoc()){
$thallowance .= '<th>'.$b['allowance'].'</th>';
$empallowance = $b['allowance'];
// fill the array with keys (allowances) and default, zero values
$tdarray[$b['allowance']] = 0;
}
}
// Looping through employees
while($r = $q->fetch_assoc()){
$id= $r['id'];
$name= $r['name'];
$all2 = "select * from emp_allowances order by allowance asc";
$c = $dbhandle->query($all2);
if($c->num_rows>0){
$tdallow='';
// Looping through employee allowances
while($d = $c->fetch_assoc()){
$empid = $d['emp_id'];
$amount = $d['amount'];
$empallowance = $d['allowance'];
if($empid==$id){
$tdarray[$empallowance] = $amount;
}
}
}
$tbody .= '<tr>';
$tbody .= '<td>'.$name.'</td>';
foreach ($tdarray as $allowance => $amount) {
$tbody .= '<td>'.$amount.'</td>';
// reset to zero
$tdarray[$allowance] = 0;
}
$tbody .= '</tr>';
}
try adding else with your code:
if($empid==$id){
$tdallow.='<td>'.$amount.'</td>';
}else{ // add else block for print zero
$tdallow.='<td>0</td>';
}
Related
i have this kind of report
i want to make a report like in the image above.
i already try it with this code :
$id = 0;
$id2 = 0;
$no = 1;
$q3 = mysql_query("SELECT * FROM pendidikan, unsur, subunsur WHERE pendidikan.id_sub = subunsur.id_sub AND subunsur.id_unsur = unsur.id_unsur AND pendidikan.nidn = $_GET[nidn]");
$q4 = mysql_query("SELECT * FROM pendidikan, unsur, subunsur WHERE pendidikan.id_sub = subunsur.id_sub AND subunsur.id_unsur = unsur.id_unsur AND pendidikan.nidn = $_GET[nidn]");
$data = array();
while($d=mysql_fetch_assoc($q3)){
$no++;
if($id != $d['id_unsur']){
$no = $no+1;
echo"<tr>
<td>$d[kode_unsur]</td>
<td colspan='8'>$d[nama_unsur]</td>
</tr>";
while($f=mysql_fetch_array($q3)){
if($id2 != $f['id_sub']){
echo"<tr>
<td rowspan='$no'>$d[kode_sub]</td>
<td colspan='8'>$d[nama_sub]</td>
</tr>";
}
$id2 = $f['id_sub'];
while($g=mysql_fetch_assoc($q4)){
if($g['id_sub'] == $f['id_sub']){
echo"<tr>
<td>$g[no_butir]</td>
<td>$g[uraian]</td>
<td>$g[thn_smt_akademik]</td>
<td>$g[tanggal]</td>
<td>$g[satuan_hasil]</td>
<td>$g[jml_vlm_kegiatan]</td>
<td>$g[jml_angka_kredit]</td>
<td>$g[bukti]</td>
</td>";
}
}
}
}
$id = $d['id_unsur'];
}
what should i do, because with that code, im creating row with the same sub A, not B like this :
any help will be appreciated. Thanks.
I hope you are doing well. I am working in a school management project , i want to get the average of 5 test(devoir) of each subject(Matiere) like-
i tried to use AVG() inside while loop of getting horizontale devoir data, i got no result ,any suggestions ??
$query5 = "select * from devoir where CIN_ELEVE = '$cinE' and ID_ELV_AN_CLS = $select2 GROUP BY MODULE_DEVOIR";
$result5 = mysqli_query($con,$query5);
if(mysqli_num_rows($result5)>0)
{
echo"<table class='table table-bordered table-striped table-hover table-info'><tr>
<th>MATIERE</th>
<th>devoir1</th>
<th>devoir2</th>
<th>devoir3</th>
<th>devoir4</th>
<th>devoir5</th>
<th>AVG</th>
</tr>
";
while($rows = mysqli_fetch_assoc($result5))
{
$moduleD = $rows['MODULE_DEVOIR'];
$queryDEV = "select * from devoir inner join eleve on devoir.CIN_ELEVE = eleve.CIN_ELEVE where eleve.CIN_ELEVE = '$cinE' and MODULE_DEVOIR = '$moduleD' order by MODULE_DEVOIR";
$resultDEV = mysqli_query($con,$queryDEV);
/*to print subject name*/
echo"<tr><td>$moduleD</td>";
while($rows = mysqli_fetch_assoc($resultDEV))/*loop2*/
{
$noteDEV = $rows['NOTE_DEVOIR'];
/*to print subject mark(note)*/
echo"<td>$noteDEV</td>";
/*i add the query of average inside the loop to be with touch by all row data mark*/
$queryAVG="select avg($noteDEV) as average_devoir from devoir where MODULE_DEVOIR = '$moduleD' group by MODULE_DEVOIR ";
}
$resultAVG = mysqli_query($con,$queryAVG);
if($rows = mysqli_fetch_assoc($resultAVG))
{
if($rows = mysqli_fetch_assoc($resultAVG))
{
$avg = $rows['average_devoir'];
echo"<td>$avh</td>";
}
}
echo"</tr>";
}
echo"</table>";
}
If you are not required to use SQL to calculate the average, then doing this seems a better option.
while($rows = mysqli_fetch_assoc($result5))
{
$moduleD = $rows['MODULE_DEVOIR'];
$queryDEV = "SELECT * FROM devoir INNER JOIN eleve " .
"ON devoir.CIN_ELEVE = eleve.CIN_ELEVE " .
"WHERE eleve.CIN_ELEVE = '$cinE' " .
"AND MODULE_DEVOIR = '$moduleD' " .
"ORDER BY MODULE_DEVOIR";
$resultDEV = mysqli_query($con,$queryDEV);
/*to print subject name*/
echo"<tr><td>$moduleD</td>";
// We will use this variable to store the sum
$sum = 0;
// And this is a counter to store the number of results
$count = 0;
while($rows = mysqli_fetch_assoc($resultDEV))/*loop2*/
{
$noteDEV = $rows['NOTE_DEVOIR'];
/*to print subject mark(note)*/
echo"<td>$noteDEV</td>";
// Update the sum
$sum += $noteDEV;
// Update the counter
$count ++;
}
// Calculate and display the average
$avg = $sum / $count;
echo "<td>$avh</td>" . "</tr>";
}
If you know that the number of results is always constant, and equals 5, you can get rid of the $count variable and divide by 5 directly.
You may also wish to format the output to fit your needs.
Firstly, the headings are stored in h1 tags. This is taken from a separate table named "menu_type". Which is linked through a "menu" table.
I am trying to display data on the base like this:
HEADING
Table Data
2nd Heading
Second Data
--- In a loop until it is all completed ---
Here is a like page of what it is doing
I believe I have the methods correct and can see what it is doing, it is printing the first heading, then a blank table, then the second heading and then the data from the first table.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $numRows; $i++){
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>$result_array[$i]</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
<tr>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo "
<td>$name - <small>$description</small></td>
<td class='text-right'>£$price </td>
";
}
echo
"
</tr>
</tbody>
</table>
";
}
// print_r($result_array[2]);
?>
You don't need these anymore, it's like you're repeating the query. It looks incorrect also.
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
The menu items are already in this query, you just have to loop through it;
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
UPDATE 1: this code is incorrect, it doesn't insert the value to the array. I updated the code (after "try this").
$result_array[] = $menuType;
UPDATE 2: the $result is repeatedly used in mysqli functions, the index is being moved. What I did is copied the initial $result to $resultCopy. Try code again, haha
Use array_push($array,$value_you_insert) function, for inserting elements to an array.
Try this;
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$resultCopy = $result;
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($resultCopy)){
$menuType = $row['type'];
array_push($result_array,$menuType);
}
for($i = 0; $i < $numRows; $i++){
echo "
<div id='hide'>
<h1 id='wines' class='head-font text-center head'>".$result_array[$i]."</h1>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>Item</th>
<th class='text-right'>Price</th>
</tr>
</thead>
<tbody>
";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($result)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo"
<tr>
<td>".$name." - <small>".$description."</small></td>
<td class='text-right'>£".$price."</td>
</tr>
";
}
echo
"
</tbody>
</table>
";
}
?>
I have set up a for and while loop to print through the contents of my database for my menu system. It works fine, however, the places to where each of the table contents is displayed is one below where it should be. See this picture below:
The problem is that the items which are showing under "Main Courses" are supposed to be under the "Starters" section.
See my code:
<?php
$query = "SELECT * FROM menu_type";
$result = mysqli_query($connect, $query);
$result_array = array();
$numRows = mysqli_num_rows($result); // returns the num of rows from the query above, allowing for dynamic returns
while($row = mysqli_fetch_assoc($result)){
$menuType = $row['type'];
$result_array[] = $menuType; // This array holds each of the menu_types from DB
}
$countArray = count($result_array);
for($i = 0; $i < $countArray; $i++){
echo "<h1 id='starters' class='head-font text-center head'>$result_array[$i]</h1>";
$query = "SELECT * FROM menu WHERE type_id='$result_array[$i]'";
$result = mysqli_query($connect, $query) or die(mysqli_error($connect));
echo
"
<div id='hide'>
<table class='table table-hover table-responsive'>
<thead>
<tr>
<th>
Item
</th>
<th class='text-right'>
Price
</th>
</tr>
</thead>
<tbody>
";
$menuQuery = "SELECT * FROM menu, menu_type WHERE type_id='$i' AND menu.type_id = menu_type.id";
$menuResult = mysqli_query($connect, $menuQuery) or die(mysqli_error($connect));
while ($row = mysqli_fetch_assoc($menuResult)) {
$name = $row['name'];
$description = $row['description'];
$price = $row['price'];
echo
"<tr>" .
"<td>$name - <small>$description</small></td>" .
"<td class='text-right'>" . "£" . $price . "</td>" .
"</tr>";
}
echo "</tbody>
</table>
</div>";
}
for($j = 1; $j < $numRows+1; $j++){
echo $j;
}
// print_r($result_array[2]);
?>
It is a tracking system like DHL. Tracking shipment number from MySQL database using php form.
but I need it Search multiple row separate by comma from mysql using php.
<?php
$ship=$_POST['Consignment'];
$cons = explode(',',$ship);
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no = '$cons[]'";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Shipment Name: <?php echo $ship_name; ?>
Shipment Phone: <?php echo $phone; ?>
<?php }//while
}//if
else {
echo 'In else....';
?>
Consignment Number not found.Search Again.
<?php
}//else
?>
So I need my search will work with separating by comma(,).
Thanks for helping me.
You can use IN operator in that case.
<?php
$ship=$_POST['Consignment'];
?>
<?php
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";
$result = dbQuery($sql);
$no = dbNumRows($result);
if($no == 1){
while($data = dbFetchAssoc($result)) {
extract($data);
?>
Hope it will help to you.
change your sql query you have written '$cons[]' in select query which is wrong . after explode you will get data as 1,2,3 so you just need to write variable in query not array and user IN Operator like this.
`$sql = "SELECT * FROM tbl_courier WHERE cons_no IN(".$ship.")";`
You should always prepare/sanitize the POST data before using it in MySql query (in terms of security):
<?php
if (isset[$_POST['Consignment']] && !empty($_POST['Consignment'])) {
$ship = $_POST['Consignment'];
$cons = explode(',', $ship);
$cons = array_filter($cons, function($v){
return trim(strip_tags($v));
});
$cons = '"' . implode('","', $cons) . '"';
$sql = "SELECT * FROM tbl_courier WHERE cons_no IN ($cons)";
$result = dbQuery($sql);
$no = dbNumRows($result);
if ($no == 1) {
while ($data = dbFetchAssoc($result)) {
extract($data);
....
}
....
}
?>
Please Use Find IN SET
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'1,2,3,4,5')
Updated
SELECT * FROM tbl_courier WHERE FIND_IN_SET(cons_no,'$ship')
Note :- $ship Comma Separated Value not an array
I found the Answer:
if(isset($_POST['Consignment'])){
$ship=$_POST['Consignment'];
$shipment= explode(',',$_POST['Consignment']);
$ship = implode("', '",$shipment) ;
$query = "SELECT * FROM `tbl_courier` WHERE `cons_no` IN('$ship')";
$results = $mysqli->query($query);
if($results){
print '<table border="1">';
while($row = $results->fetch_assoc()) {
print '<tr>';
print '<td>'.$row["cons_no"].'</td>';
print '<td>'.$row["customerName"].'</td>';
print '<td>'.$row["customerPhone"].'</td>';
print '</tr>';
}
print '</table>';
// Frees the memory associated with a result
$results->free();
}
else {
echo "Query Not Match";
}
$mysqli->close();
}
Thanks to Answer.