How to display table row like this - php

i have this table in mysql
this is mycode
//generate report
<center>
<table border=1 cellpadding=10>
<tr>
<td rowspan="2">TANGGAL</td>
<td colspan="2" align="center">PAGI</td>
<td colspan="2" align="center">SORE</td>
<td rowspan="2">JML. JAM</td>
<td rowspan="2">JML. Rp</td>
</tr>
<tr>
<td>Masuk</td>
<td>Keluar</td>
<td>Masuk</td>
<td>Keluar</td>
</tr>
<?php
$qry_tgl=mysql_query("SELECT id_peg, date(waktu) Tgl from absen where id_peg=1 group by Tgl");
while($row=mysql_fetch_array($qry_tgl)){
echo "<tr>";
echo "<td>".$row['Tgl']."</td>";
echo "</tr>";
}
echo "</table></center>";
I want to display table in php like this
Please help me, I'm very confuse what to do
thankyou

Your HTML code seems to be fine, but SQL query not. I assume you were asking about how to make a correct query. When using GROUP BY you should (and in newer MySQL versions by default must) select only fields with either aggregate functions like SUM, AVG, etc., or select fields that are specified in GROUP BY. You cannot group by result of a select.
Your query should be like this:
SELECT DATE(waktu) Tgl FROM absen WHERE id_peg=1 GROUP BY DATE(waktu)
EDIT: Ok, according to google translate and other sites Masuk/Keluar are Since/Till. And using new provided information this should do the trick:
<center>
<table border=1 cellpadding=10>
<tr>
<td rowspan="2">TANGGAL</td>
<td colspan="2" align="center">PAGI</td>
<td colspan="2" align="center">SORE</td>
<td rowspan="2">JML. JAM</td>
<td rowspan="2">JML. Rp</td>
</tr>
<tr>
<td>Masuk</td>
<td>Keluar</td>
<td>Masuk</td>
<td>Keluar</td>
</tr>
<?php
function display_row($timeRanges) {
$pagi = isset($timeRanges[1]) ? strtotime($timeRanges[1]) - strtotime($timeRanges[0]) : 0;
$sore = isset($timeRanges[3]) ? strtotime($timeRanges[3]) - strtotime($timeRanges[2]) : 0;
$seconds = $pagi + $sore;
echo "<tr>";
echo "<td>" . substr($timeRanges[0], 0, 10) . "</td>"; // TANGGAL
echo "<td>" . substr($timeRanges[0], 11, 8) . "</td>"; // PAGI Masuk
echo "<td>" . (isset($timeRanges[1]) ? substr($timeRanges[1], 11, 8) : "") . "</td>"; // PAGI Keluar
echo "<td>" . (isset($timeRanges[2]) ? substr($timeRanges[2], 11, 8) : "") . "</td>"; // SORE Masuk
echo "<td>" . (isset($timeRanges[3]) ? substr($timeRanges[3], 11, 8) : "") . "</td>"; // SORE Keluar
echo "<td>" . round($seconds / 3600, 2) . "</td>"; // JML. JAM shows rounded number of hours
echo "<td>" . round($seconds * 5000 / 3600) . "</td>"; // JML. Rp number hours * 5000
echo "</tr>";
}
$qry_tgl=mysql_query("SELECT DATE(waktu) Tgl, waktu FROM absen WHERE id_peg=1 ORDER BY waktu");
$lastDate = null;
$timeRanges = array();
while($row=mysql_fetch_array($qry_tgl)){
if( $row['Tgl'] !== $lastDate ) {
if( $lastDate !== null )
display_row($timeRanges); // renders the row when the date changes, but only if we fetched at least one date
$lastDate = $row['Tgl'];
$timeRanges = array();
}
$timeRanges[] = $row["waktu"];
}
if( $lastDate !== null )
display_row($timeRanges); // renders the last row when the loop ends, but also only if we fetched at least one date
echo "</table></center>";
This algorithm reads dates into $timeRanges array until another date is encountered. So when display_row() function is called the $timeRanges will contain only ordered records for the same date.
I'm afraid doing it using only MySQL would be slow and a huge waste of resources.

Related

php sql formatting SELECT columns numbers

I am retrieving various columns from an SQL table and some of those are numeric or currency, in a sample let’s say:
$sql="SELECT id_column, event_column, amount_column FROM table1";
Then I show them using that:
$result = mysqli_query($conn,$sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>event time</th>
<th>amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row[' id_column '] . "</td>";
echo "<td>" . $row[' event_column '] . "</td>";
echo "<td>" . $row[' amount_column '] . "</td>";
echo "</tr>";
}
echo "</table>";
Is it possible to change the format numbers get out in amount_column?
I saw should be possible to use a command to change a single number data the way I wuold like - number_format($number, 2, ',', '.') – but this seems not to apply for entire columns
What I do need is using comma for decimal under one (yy) and point for others grouped by 3 (x.xxx) thousands, something like xx.xxx.xxx,yy
Does some one have any suggestion? (including how to change the settings in PHP or SQL by the moment when I entry the data via form those have a comma instead of point for decimal but SQL save them in a different way – UK/USA decimal punctuation I guess while I need EU Italian/Germany punctuation or at least the ISO standard using comma for decimal and space for each group of three numbers).
Correct answer is my comment:
echo "<td>" . number_format($row['amount_column'], 2, ',', ' ') . "</td>";
You can not do:
$row[' amount_column ']
Spacing for array index count!
here is a proof https://ideone.com/FtPEc6
So technically the answer you've approved - is wrong.
UPDATE:
Yes, true money_format works not on windows. Thanks Dave. :)
Use number_format( $row[' amount_column '], 2, ',', '.')
It gives you something like 123.456.789,12
PHP number_format
(Not on windows!)
You can use php´s money_format('%i', $row[' amount_column '])
PHP money_format
<?php
setlocale(LC_MONETARY, 'en_US');
$result = mysqli_query($conn,$sql);
echo "<table border='1'>
<tr>
<th>id</th>
<th>event time</th>
<th>amount</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td>" . $row[' id_column '] . "</td>";
echo "<td>" . $row[' event_column '] . "</td>";
echo "<td>" . money_format('%i', $row[' amount_column ']) . "</td>";
echo "</tr>";
}
echo "</table>";

Adding total of array in php?

I have a database and PHP file which outputs a module catogaries by the year the module was taken in. accompanying this is how much the module is worth.. eg Computer Science 10. The output on the screen needs to have the TOTAL of the points within that YEAR
So it looks like this:
2001/02
cs 10
bi 10
chem 10
total 30
This is all fine and works APART from if there is more years like this:
2001/02 0 points
2002/03 120 points
2003/04 120 points
But there is points in 2001/02 but the code seems to overwrite this before outputting it.
Here is the PHP code:
$points = array();
while ($row = mysql_fetch_array($result)) {
if ($year != $row["ayr"]) {
echo "<tr><th colspan='3'><b>" . $row["ayr"] . "</b></th></tr>";
$year = $row["ayr"];
echo "<td align='right'><b> Total Module Points: ".array_sum($points)."<td></b>";
$points = array();
}
if ($year = $row["ayr"]) {
array_push($points, $row["credits"]);
}
echo "<tr>";
echo "<td>" . $row["mid"] . "</td>";
echo "<td>" . $row["mtitle"] . "</td>";
echo "<td>" . $row["credits"] . "</td>";
echo "<tr>";
if ($year != $row["ayr"]) {
echo "<td align='right'> Total Module Points: ".array_sum($points)."<td>";
}
}
So the code gets the student number, output each module by year and then adds up the module points and gives a total but I cannot get the first table to work
regards
Maybe this causes your problem?
if ($year = $row["ayr"]) {
array_push($points, $row["credits"]);
}
Basically, $year will always be equal to $row["ayr"], is this a desired behavior?
The 'equal' comparison operator is '==' as stated before.

php make html table rows equal

I am getting data from a my database. There is a admin panel also where people can add data to the database. The data gets on the page but some of the rows(<tr>) have less table data tags(<td>) than others. thus the table is not justified. Is there a way to add empty <td> to rows that need them? I have tried everything but i can't figure it out.
Picture on how the table looks at the moment:
The green numbers are the total sum of points but it's not clear because the table rows are jagged. How to fix tis?
If there is a jQuery solution that's also fine.
my code:
echo "<table class=\"zebra1\">";
echo "<th>N. </th>" . "<th>Team name: </th>" . "<th colspan=\"5\">Points: </th>" . "<th>Sum: </th>";
$numbering =1;
$query2 = $db->prepare("SELECT pisteet_1 As PIY, pisteet_2 as PIK, nimi As NIM, opisto As OPI, pisteet.kaupunki_id As KA FROM
pisteet INNER JOIN joukkueet ON joukkueet.id = pisteet.team_id INNER JOIN oppilaitokset ON oppilaitokset.opisto_id = joukkueet.opisto_id ORDER BY team_id ASC");
$query2->execute();
$results = $query2->fetchAll();
$tableD = array();
foreach ($results as $key) {
$tableD[$key['NIM']][] = array('PIY'=>$key['PIY'],'PIK'=>$key['PIK'],'KA'=>$key['KA'], 'OPI'=>$key['OPI']);
}
foreach($tableD as $teamN=>$values2){
//Echoing the Team name
echo "<tr class=\"all " . $values2[0]['KA'] . "\">";
echo "<td>" . $numbering . "</td>";
echo "<td>" . $teamN ."<span>" . $values2[0]['OPI'] ."</span></td>";
$sum1=0;
$sum2=0;
//Echoing the points
foreach($values2 as $v2){
echo "<td class=\"points\">" . $v2['PIY'] . "/" . $v2['PIK'] . "</td>";
$sum1 +=$v2['PIY'];
$sum2 +=$v2['PIK'];
}
//Echoing the total sum of points
echo '<td class="Sum">'.$sum1.'/'.$sum2."</td>";
echo "</tr>";
$numbering ++;
}
echo '</table>';
I have a variable named: $colspancalculated that has the longest row: at the moment it stores the value 5.
Assuming you have a fixed number of columns (I assume this because you've got a colspan on your table header cell), you need to output the td elements as you are doing, or output blank cells if the records don't exist.
Consider something like this instead of your foreach:
// Echoing the points - as you mention in your comment, you've calculated
// the maximum column size as $colspancalculated - so you that as your upper limit
for($i = 0; $i < $colspancalculated; $i++) {
if(!isset($values2[$i]['PIY'])) {
// This record doesn't exist! Output a blank cell
echo '<td></td>';
continue;
}
// Otherwise, output the cell and do your calculations
echo '<td class="points">' . $values2[$i]['PIY'] . '/' . $values2[$i]['PIK'] . '</td>';
$sum1 += $values2[$i]['PIY'];
$sum2 += $values2[$i]['PIK'];
}
Instead of a foreach loop, use a for loop -- or, since you have to work with an iterator, anyway, just do:
$i = $numberOfColumnsLeftAtThisPointInYourScript
foreach($values2 as $v2){
echo "<td class=\"points\">" . $v2['PIY'] . "/" . $v2['PIK'] . "</td>";
$sum1 +=$v2['PIY'];
$sum2 +=$v2['PIK'];
$i--;
}
while($i > 0){
echo '<td> </td>';
$i--;
}

Date sorting from input in php from sql?

I'm doing a sales recording for my own small shop using the combination of html and php.
I want to have a time selecting input (something like March 2014, April 2014 when selecting)Here is my index.php
<?php
$con=mysqli_connect("192.168.1.248","a","a","services");
if (mysqli_connect_errno()) {
echo "Failed to connect to MySQL: " . mysqli_connect_error();
}
$result = mysqli_query($con,"SELECT * FROM pcsales");
echo "<h3><marquee><b>====PC Sales====</b></marquee></h3>";
echo "<button type='button' name='add' onClick='add()'>Add</button>";
echo "<script type='text/javascript'>
function add()
{
window.location='./edit';
}
</script>";
echo "<button type='button' name='edit' onClick='edit()'>Edit</button>";
echo "<script type='text/javascript'>
function edit()
{
window.location='./edit/edit.html';
}
</script>";
echo "<table border='1'>
<tr>
<th>ID</th>
<th>Type</th>
<th>If Others</th>
<th>Brand</th>
<th>Description</th>
<th>Date Sold</th>
<th>Serial No.</th>
</tr>";
while($row = mysqli_fetch_array($result)) {
echo "<tr>";
echo "<td><p style='font-size:12px'>" . $row['ID'] . "</p></td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['Types'] . "</td>";
echo "<td>" . $row['Brand'] . "</td>";
echo "<td><p style='font-size:12px'>" . $row['Description'] . "</p></td>";
echo "<td><p style='font-size:12px'>" . $row['Selldate'] . "</p></td>";
echo "<td><p style='font-size:12px'>" . $row['Serial'] . "</p></td>";
echo "</tr>";
}
echo "</table>";
echo "<h3><marquee direction=right><b>====PC Sales====</b></marquee></h3>";
mysqli_close($con);
?>
I'm using TIMESTAMP in mysql.
Here is the output
ID Type If Others Brand Description Date Sold Serial No.
1 Notebook Acer E1 Add 2GB DDR3 1600 RAM 2014-06-25 11:57:58 123456789
2 Others AIO Asus N/A 2014-07-25 12:52:12 987654321
3 Desktop Trendsonic Full spec listed. 2014-07-30 09:55:10 N/A
When I put a range on a textbox (more expedient if selectable)
Example:
July 2014
Then the output shall be
ID Type If Others Brand Description Date Sold Serial No.
2 Others AIO Asus N/A 2014-07-25 12:52:12 987654321
3 Desktop Trendsonic Full spec listed. 2014-07-30 09:55:10 N/A
If only2014 is inserted, then it shall output everything from Year 2014
Is it possible to do that?
Note: I don't care about exploits, as it is used internally.
Better to take datepicker and fetch date from there in variable and make query some how like this.
SELECT *
FROM TABLENAME
WHERE MONTH(dateColumnname) = $month AND YEAR(dateColumnname) = $year;
EXAMPLE: SELECT *
FROM testerpractice
WHERE MONTH(datess) = '03' AND YEAR(datess) = '2017';
Best OF Luck...
Better use dropdown lists ( tag) for day, month and year (with defaults 01 for day, Jan for month, current year for the year) , and concatenate their values to get the search string. You'll have expediency and will be able to construct the query easy.
-Problem Solved-
My solution(not that "professional")
I do a sort.html and a sort.php
The sort.html contains
Year:<input type="text" value="" name="year"/>
Month:<input type="text" value="" name="month"/>
While the sort.php contains
$year = mysqli_real_escape_string($con, $_POST['year']);
$month = mysqli_real_escape_string($con, $_POST['month']);
$result = mysqli_query($con,"SELECT * FROM pcsales WHERE Selldate >= '$year-$month-01 00:00:00' AND Selldate <= '$year-$month-31 23:59:59'");
This might be a reference for other people who might encounter the same problem as me~

Math operation in PHP & MySQL

I'm creating a table using a PHP from the MySQL query which return a total count of rows from two columns in the database, "total_tr" and "total_rc".
I've already done and successfully view the count in the PHP table, the coding is:
while($row = mysql_fetch_array($result))
{
echo "<tbody>";
echo "<tr>";
echo "<td>Zone</td>";
echo "<td>" . $row['segment_code'] . "</td>";
echo "<td>" . $row['COUNT(total_tr)'] . "</td>";
echo "<td>" . $row['COUNT(repeat_rc)'] . "</td>";
echo "</tr>";
echo "</tbody>";
}
My problem now is, I want to take the total count value of "total_tr", divided with total count of "repeat_rc" and multiply with 100 to get the percentage of total_rc.
Any ideas on how can I do that?
$myresult = $row['COUNT(total_tr)'] / $row['COUNT(repeat_rc)'] * 100;
Keep a running count as you echo the rows
$total_tr = 0;
$total_rc = 0;
while($row = ...) {
$total_tr += $row['COUNT(total_tr)']);
$total_rc += $row['COUNT(repeat_rc)']);
... html here ...
}
echo $total_tr / $total_rc * 100;

Categories