I have different tables like profiles, tournaments, tournamentdate, tournamentresult and pigeons.
Profiles columns > ProfileId, Name, Address, etc.
Tournaments columns > tID, name, detail, startdate, enddate, etc.
Tournamentdate columns > tdID, tID, date.
Tournamentresult Columns > ResultID, ProfileID, tID, tdID, starttime and Total (Total Time).
Pigeons Columns > PID, ResultID, ProfileID, TID, TDID, Pigeon Number, FlyingTime, TotalTime.
I have create a tournament for 3 days like 13,15,17 May (added dates into tournamentdate Table). I have added 5 peoples from Profiles Table, 7 Pigeons a day into Pigeons Table.
I will show you how to I display data using PHP.
this is one day record one Person have 7 Pigeons I am using SQL Sum Query for each person for total. Now I want to display same thing but Total as DESC.
Here is my code (function.php)
function GetTournamentDate($conn, $TID)
{
$TID = (int)$TID;
$tournaments = array();
$sql = "SELECT * FROM tournamentdate WHERE `TID`='$TID' ORDER BY `TDate` ASC";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
$tournaments[] = array(
'TDID' => $row['TDID'],
'TID' => $row['TID'],
'TDate' => $row['TDate'],
);
}
}
return $tournaments;
}
//=========
function GetResults($conn, $TID)
{
$TID = (int)$TID;
$Results = array();
$sql = "SELECT * FROM tournamentresult WHERE `TID`='$TID' ORDER BY `Total` DESC";
$Result = $conn->query($sql);
if ($Result->num_rows > 0) {
// output data of each row
while ($row = $Result->fetch_assoc()) {
$Results[] = array(
'ResultID' => $row['ResultID'],
'ProfileID' => $row['ProfileID'],
'TID' => $row['TID'],
'TDID' => $row['TDID'],
'StartTime' => $row['StartTime'],
'Total' => $row['Total'],
'Price' => $row['Price'],
);
}
}
return $Results;
}
// ====================
function GetPigeonByResult($conn, $ResultID, $ProfileID, $TDID)
{
$ResultID = (int)$ResultID;
$ProfileID = (int)$ProfileID;
$TDID = (int)$TDID;
$Results = array();
$sql = "SELECT * FROM pigeons WHERE `ResultID` = '$ResultID' and `ProfileID` = '$ProfileID' and `TDID` = '$TDID' ORDER BY `Pigeon` ASC";
$Result = $conn->query($sql);
if ($Result->num_rows > 0) {
// output data of each row
while ($row = $Result->fetch_assoc()) {
$Results[] = array(
'PigeonID' => $row['PigeonID'],
'ResultID' => $row['ResultID'],
'Pigeon' => $row['Pigeon'],
'PigeonTime' => $row['PigeonTime'],
'PigeonTotalTime' => $row['PigeonTotalTime'],
'Status' => $row['Status'],
);
}
}
return $Results;
}
//=================
// profile_data
function ProfileData($conn, $ProfileID)
{
$ProfileID = (int)$ProfileID;
$func_num_args = func_num_args();
$func_get_args = func_get_args();
if ($func_num_args > 1) {
unset($func_get_args[0]);
unset($func_get_args[1]);
// check arry fields
$valid = array('ProfileID', 'ProfileName', 'ProfileAddress', 'ProfileNumber', 'ProfileDetail');
$fields = array();
foreach ($func_get_args as $arg) {
if (in_array($arg, $valid)) $fields[] = $arg;
}
// convert fields
$fields = '`' . implode('`, `', $fields) . '`';
$sql = "SELECT $fields FROM profiles WHERE ProfileID = '$ProfileID'";
$result = $conn->query($sql);
if ($result->num_rows > 0) {
// output data of each row
while ($row = $result->fetch_assoc()) {
foreach ($func_get_args as $field) {
$func_get_args[$field] = $row[$field];
}
return $func_get_args;
}
} // else
}
}
// =================
and here in index.php
<?php
$TID = 5; // example
$Results = GetResults($conn, $TID);
$TournamentDate = GetTournamentDate($conn, $TID);
if (!empty($TournamentDate)) {
foreach ($TournamentDate as $TD) {
$TDID = $TD['TDID'];
$TDate = $TD['TDate'];
}
$TDID = 8; // example
if (!empty($Results)) {
foreach ($Results as $TR) {
$ResultID = $TR['ResultID'];
$ProfileID = $TR['ProfileID'];
$TotalPigeons = GetPigeonByResult($conn, $ResultID, $ProfileID, $TDID);
}
}
?>
<tbody>
<?php
$RCount = 0;
foreach ($Results as $Result) {
$RCount++;
$ResultID = $Result['ResultID'];
$ProfileID = $Result['ProfileID'];
$StartTime = $Result['StartTime'];
if ($StartTime == 0) {
$StartTime = '';
} else {
$StartTime = secondsToWords($StartTime);
// $StartTime = substr($StartTime, 0, -3);
}
$Total = $Result['Total'];
if ($Total == 0) {
$Total = '<p style="color: #ccc">0</p>';
} else {
$Total = secondsToWords($Total);
// $Total = substr($Total, 0, -3);
}
$ProfileData = ProfileData($conn, $ProfileID, 'ProfileName', 'ProfileAddress');
$ProfileName = $ProfileData['ProfileName'];
$ProfileAddress = $ProfileData['ProfileAddress'];
echo "<tr>";
echo "<td>$RCount</td>";
?>
<td style="text-align: left;">
<h3 style="margin: 0px; padding: 0px; font-weight: bold;">
<?php echo $ProfileName; ?>
</h3>
<p style="margin: 0px; padding: 0px;"><?php echo $ProfileAddress; ?></p>
</td>
<td>
<a href="#!" class="edit" data-type="text" data-pk="<?php echo $ResultID . '-sTime'; ?>">
<?php echo $StartTime; ?>
</a>
</td>
<?php
// Get Pigeons
$TotalPigeons = GetPigeonByResult($conn, $ResultID, $ProfileID, $TDID);
if (!empty($TotalPigeons)) {
$TotalSum = 0;
foreach ($TotalPigeons as $TP) {
$tPigeonID = $TP['PigeonID'];
$PigeonTime = $TP['PigeonTime'];
$PigeonTotalTime = $TP['PigeonTotalTime'];
$PigeonStatus = $TP['Status'];
$TotalSum = $TotalSum + $PigeonTotalTime;
$tPigeonTime = secondsToWords($PigeonTime);
$PigeonTotalTime = secondsToWords($PigeonTotalTime);
if ($PigeonTime == 0) {
if ($PigeonStatus == 1) {
?>
<td>
<a href="#!" class="edit" data-type="text" data-value="" data-pk="<?php echo $tPigeonID . '-pTime-' . $Result['StartTime'] . '-' . $Result['Total']; ?>">
empty
</a>
Active
</td>
<?php
} else {
?>
<td>
Waste
</td>
<?php
}
} else {
if ($PigeonStatus == 1) {
?>
<td>
<a href="#!" class="edit" data-type="text" data-pk="<?php echo $tPigeonID . '-pTime-' . $Result['StartTime'] . '-' . $Result['Total']; ?>">
<?php echo $tPigeonTime; ?>
</a>
<p><small><?php echo $PigeonTotalTime; ?></small></p>
Active
</td>
<?php
} else {
?>
<td style="background-color: red; color: white;">
Waste
</td>
<?php
}
}
}
}
echo "<td>" . secondsToWords($TotalSum) . "</td>";
echo "</tr>";
}
?>
</tbody>
Please tell me how to I Display as total DESC.
If you give me code I will be grateful to you۔
You can do this by creating a SELECT statement to get all the users and inside the SELECT statement you can nest another SELECT to get the total score.
Then you can simply sort your result using ORDER.
SELECT
*,
(SELECT SUM(score) FROM games WHERE user = users.ID) AS total
FROM users ORDER BY total DESC
In your example, if I understand correctly, this would translate to:
SELECT
*, (SELECT SUM(TotalTime) FROM Pigeons WHERE ProfileID = Profiles.ProfileId) AS total
FROM Profiles ORDER BY total DESC
Use group_concat along with group by you can get all data in a single query.
then you can use those data using explode.
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.
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]);
?>
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>';
}