I'm trying to build a table of results from an array. I currently have results outputting like this:
ID VALUE EXTRA
--------------------------------------
| 1 | Value 1 | Extra 1 |
|-----|---------------------|----------|
| 1 | Value 2 | Extra 2 |
|-----|---------------------|----------|
| 2 | Value 3 | Some 1 |
|-----|---------------------|----------|
| 3 | Value 4 | Some 2 |
|-----|---------------------|----------|
| 3 | Value 5 | Nothing |
--------------------------------------
Note the repeating ID value. What I'd like to do is build a loop inside my current loop that will not display duplicate IDs. Something like this:
ID VALUE EXTRA
--------------------------------------
| 1 | Value 1 | Extra 1 |
| |---------------------|----------|
| | Value 2 | Extra 2 |
|-----|---------------------|----------|
| 2 | Value 3 | Some 1 |
|-----|---------------------|----------|
| 3 | Value 4 | Some 2 |
| |---------------------|----------|
| | Value 5 | Nothing |
--------------------------------------
Here's my current code, simplified:
<?php
$i=0;
while ($i < $mynum) {
$f1=mysql_result($myresult,$i,"tableID");
$f2=mysql_result($myresult,$i,"values");
$f3=mysql_result($myresult,$i,"extra");
?>
<tr>
<td><?php echo $f1; ?></td>
<td><?php echo $f2; ?></td>
<td><?php echo $f3; ?></td>
</tr>
<?php
$i++;
}
?>
Is there a way to build this table dynamically in the way I want? Or should I rethink my strategy?
Assuming the IDs are sorted sequentially, store the ID in a separate variable and check to see if it has changed. If it has, print the ID; if not, print a or similar value.
Here a way to do it from the code you provided
<?php
$i=0;
$previousId = ''; // keeps track of the previous Id
while ($i < $mynum) {
$html = '';
$f1=mysql_result($myresult,$i,"tableID");
$f2=mysql_result($myresult,$i,"values");
$f3=mysql_result($myresult,$i,"extra");
$html .= '<tr>';
if($previousId != $f1){ // fill the cell only if the new Id is different from the previous value
$html .= '<td>'.$f1.'</td>';
} else {
$html .= '<td> </td>';
}
$previousId = $f1;
$html .= '<td>'.$f2.'</td>';
$html .= '<td>'.$f3.'</td>';
$html .= '</tr>';
$i++;
}
echo $html;
?>
But that is assuming that the $f1 are ordered.
Related
Problem: I need to hide the data BUT not to delete it from the database that has a value or amount of zero or none value. Is there any possibilities to achieve this problem?
I just need to remove it from the bootstrap table
here is what my table looks like right now.
--------------------------------------
| id | amount | name | price |
| 1 | 3000 | John | 200 |
| 2 | 0 | John | 100 |
| 3 | 0 | John | 200 |
| 4 | 3000 | Pat | 400 |
--------------------------------------
The problem is there is the value of zero that should not be seen on my table, can that be solved by hiding or removing it?
To put it simply the table must be look like this
--------------------------------------
| id | amount | name | price |
| 1 | 3000 | John | 200 |
|------------------------------------
| 4 | 3000 | Pat | 400 |
--------------------------------------
Here is my code
<?php
//Fetch data
$host = "local";
$username = "name";
$password = "";
$database = "dbname";
try {
$connect = new PDO("mysql:host=$host;dbname=$database",$username,$password);
$connect->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
$query = "SELECT * FROM cabinqr_logs";
$data = $connect->query($query);
?>
<?php foreach($data as $row)
{?>
<tr>
<th scope="row"><?php echo $row["passenger"] ?></th>
<td><?php echo $row["flight_no"] ?></td>
<td><?php echo $row["amount"] ?></td>
<td><?php echo $row["date_time"] ?></td>
<td><?php echo $row["crew_name"] ?></td>
</tr>
</tbody>
<?php
}
}
catch (PDOException $error)
{
$error->getMessage();
}
?>
You just need to include a WHERE clause which says only to select rows with a value greater than zero in them...
$query = "SELECT * FROM cabinqr_logs WHERE amount > 0";
I want to make sub group in group by (SQL).
in my case, I want to make classified detail in group detail which it come from two different column, for more detail, check this out :
I've sql table like this :
+----+-------+--------+-------+
| No | data1 | detail | price |
+----+-------+--------+-------+
| 1 | ID1 | Food | $ 100 |
| 2 | ID1 | Drink | $ 25 |
| 3 | ID2 | Drink | $ 25 |
| 4 | ID1 | Snack | $ 50 |
| 5 | ID2 | Snack | $ 50 |
+----+-------+--------+-------+
I want to make result in my php page like this :
+----+-------+--------+-------+
| No | detail_lunch | price |
+----+-------+--------+-------+
| 1 | Food | |
| | - ID1 | $ 100 |
| 2 | Snack | |
| | - ID1 | $ 25 |
| | - ID2 | $ 25 |
| 3 | Drink | |
| | - ID1 | $ 10 |
| | - ID2 | $ 10 |
+----+-------+--------+-------+
I already tried with GROUP CONCAT as below :
Controller :
$d['data'] = $this->db->query("select detail_item, GROUP_CONCAT (detail) detail_lunch
from table ORDER BY detail_lunch ASC");
Views :
<?php
foreach($data->result_array() as $d)
{
?>
<tr>
<td><?php echo $no; ?></td>
<td><?php echo $d['detail_lunch']; ?></td>
<td><?php echo $d['price']; ?></td>
</tr>
<?php
$no++;
}
?>
My code above not working,
is there any suggestion to solve my problem?
thanks...
GROUP BY is not needed since you're not using an aggregate function (combining two rows of values into a single value), you are attempting to create a pivot table, (joining two columns under a related column as multiple rows). You only need to ORDER BY detail ASC, data1 ASC, iterate over the resultset to display the detail when it changes, otherwise display the data1 and price
Example: https://3v4l.org/7tVDS
Query:
$data = $this->db->query('SELECT data1, detail, price
FROM table ORDER BY detail ASC, data1 ASC');
View:
<?php
//...
$no = 1;
$currentDetail = null;
foreach ($data->result_array() as $d) {
if ($d['detail'] !== $currentDetail) { ?>
<!-- Only Show the Detail when changed -->
<tr>
<td><?php echo $no++; ?></td>
<td><?php echo $d['detail']; ?></td>
<td> </td>
</tr>
<?php } ?>
<!-- Always show the data1/price on subsequent row -->
<tr>
<td> </td>
<td>- <?php echo $d['data1']; ?></td>
<td><?php echo $d['price']; ?></td>
</tr>
<?php
$currentDetail = $d['detail'];
} ?>
Result:
| 1 | Food |
| - ID1 | $ 100 |
| 2 | Drink |
| - ID1 | $ 25 |
| - ID2 | $ 25 |
| 3 | Snack |
| - ID1 | $ 50 |
| - ID2 | $ 50 |
Alternatively you can manipulate how the data is displayed in your view, such as using an unordered list ul in the detail table column td along with price. By using PHP to organize the values how you would like them to be displayed in your view, in an organized associative array.
Example: https://3v4l.org/8sIrR
$dataArray = [];
foreach ($data as $d) {
if (!array_key_exists($d['detail'], $dataArray)) {
$dataArray[$d['detail']] = [];
}
$dataArray[$d['detail']][] = $d;
}
//...
$no = 1;
foreach ($dataArray as $detail => $d) { ?>
<tr>
<td><?php echo $no++; ?></td>
<td>
<?php echo $detail; ?>
<ul>
<?php foreach ($d as $v) { ?>
<li><?php echo $v['data1']; ?> <?php echo $v['price']; ?></li>
<?php } ?>
</ul>
</td>
</tr>
<?php } ?>
Query is the problem .
Select detail_lunch from table group by details.
Then you can use if statement on that result.
I will update my answer after I get to work on my computer.
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>";
This is my code
$Qemaster="select * from emaster where `branch`='$bid' and `department`='$did' and `status`!='L'";
$Remaster=mysql_query($Qemaster);
while($Rowemaster=mysql_fetch_array($Remaster)){
$empcode=$Rowemaster[id];
$name=$Rowemaster[name];
$Tleave=0;
echo "<tr>";
echo "<td rowspan='2'>".$name."</td>";
echo "<td>Leave</td>";
$Qlp="select `leave` from lpsummary where ((`month` IN(04,05,06,07,08,09,10,11,12) and `year`='$year') or (`month` IN(01,02,03) and `year`='$Nyear')) and `empcode`='$empcode'";
$Rlp=mysql_query($Qlp);
while($Rowlp=mysql_fetch_array($Rlp)){
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
echo "<td>".$leave."</td>";
}
echo "<td><font color='red'>".$Tleave."</font></td>";
echo "<tr><td>Percentage</td>";
}
and my table is
------------------------------------------
| name | apr-12 | may-12 | jun-12 | jul-12 |
|------|--------|--------|--------|--------|
|Kumar | 2 | 1 | 0 | 3 |
|Rajan | 4 | 0 | 2 | |
| | | | | |
|------------------------------------------
Here under the name Rajan there is no data in jun-12 but jul-12 had the value 2...ie)empty row in the table lpsummary ...... if there is empty i wanna to replace it with as '-'... How can i do that by my code.....
In your while loop, you need to put a condition to check if a null from was returned.
while($Rowlp=mysql_fetch_array($Rlp)){
if (is_null($Rowlp['leave'])) {
$leave = '-';
} else {
$leave=$Rowlp['leave'];
$Tleave=$Tleave+$leave;
}
echo "<td>".$leave."</td>";
}
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>