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>";
Related
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.
I asked a question last week and had assumed that the final part of it would be the straight forward part. However that is not proving to be the case.
Here is a link to the original question and background.
I have two databases so have done 2 SQL queries and have merged the results into a table with four columns that displays
Name | Number| db1 Count | db2 Count |
That displays all the information perfectly.
However, I need to get it to display, in a 5th column, the difference between db1 Count and db2 Count.
This is the code I have:
$results = array();
while($row = $result1->fetch_assoc()) {
//Adding all the 1st query results
$results[$row['number']] = $row;
}
while($row = $result2->fetch_assoc()) {
if(! isset($results[$row['number']]) {
$results[$row['number']] = $row;
}else {
$results[$row['number']]['db2 Count'] = $row['db2 Count'];
}
}
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);
if ($results) {
echo"<TABLE><caption>Total Call Count Overview</caption><TR>
<TH>Number</TH>
<TH>Company</TH>
<TH>db1 Count</TH>
<TH>db2 Count</TH>
<TH>Difference</TH></TR>";
foreach($results as $row) {
echo"<TR><TD>". $row["number"]. "</TD>";
echo"<TD>". $row["company"]. "</TD>";
echo"<TD>". $row["db1 Count"]. "</TD>";
echo"<TD>". $row["db2 Count"]. "</TD>";
echo"<TD>". $difference . "</TD></TR>";
}
echo"</TABLE>";
} else {
echo"0 Results";
}
I had thought that putting the counts as variables
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);
Would allow me to subtract them and echo the $difference variable, but I am just getting a column filled with zeros.
I have also tried
echo"<TD>". $row['db1 Count'] - $row['db2 Count']. "</TD>";
But only to get the all the db2 Count figures across the top of the page.
Is there something I am missing, because I have tried answers to similar questions online but to no avail. I'm really quite new to this so any tips in the right direction would be much appreciated.
Move these lines:
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);
inside the : foreach($results as $row)
so
foreach($results as $row) {
$db1_count = $row['db1 Count'];
$db2_count = $row['db2 Count'];
$difference = ($db1_count - $db2_count);
echo"<TR><TD>". $row["number"]. "</TD>";
echo"<TD>". $row["company"]. "</TD>";
echo"<TD>". $row["db1 Count"]. "</TD>";
echo"<TD>". $row["db2 Count"]. "</TD>";
echo"<TD>". $difference . "</TD></TR>";
}
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~
I am using mysql 5.1 database and have a # of fields in them that look like this for format:
1234567
When I output them to via php, I would like them formatted like this:
1,234,567
There are no decimals involved.
The select statement is pulling 30 records from my database. Each record has (2) fields that I need formatted thus. Weight and Cost as well as 16 other fields that I need, but do not need formatting. They are being read and entered int a table.
Suggestions?
Note:
Data Fields: Cost Weight
Table: Warships
Current Select statement:
$result = mysql_query('SELECT * FROM `Warships` WHERE `Type` = "BC" ');
while($row = mysql_fetch_array($result))
{
echo "<tr>";
echo "<td>" . $row['ID'] . "</td>";
echo "<td>" . $row['SHIP CLASS'] . "</td>";
echo "<td>" . $row['CAT'] . "</td>";
echo "<td>" . $row['Type'] . "</td>";
echo "<td>" . $row['WEIGHT'] . "</td>";
echo "<td>" . $row['Cost'] . "</td>";
I think PHP's number_format is what you need.
$num = number_format(1234567);
echo $num; // 1,234,567
If only one parameter is given, number will be formatted without decimals, but with a comma (",") between every group of thousands. More info from PHP API
You're looking for the number_format function. Pass your number to it as the first parameter and it will add in a comma for the thousands separator.
Example:
echo "<td>" . number_format($row['Cost']) . "</td>";
number format
$num_format = number_format(1234567);
echo $num_format ; // 1,234,567
I'm using the code below to build a table, but because the values in my database table are constantly incrementing, I'm doing some math to work out differences in values (numerically) but this has screwed up the table layout somehow. I've included a screenshot so you can see that the first row beneath the table header is just not right.
$column is a $_GET value from the user.
$sql = "select * from (select * from mash order by tstamp desc limit 10) s order by s.id";
$result = mysql_query($sql);
$previous = 0;
$firstRun = true;
echo "<table id='dataTable' border='1'>";
echo "<tr><th>Date</th>
<th>Value</th></tr>";
while($row = mysql_fetch_array($result)){
$difference = $row[$column] - $previous;
if (!$firstRun)
echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
echo "<td>" . $difference . "</td></tr>";
$previous = $row[$column];
$firstRun = false;
}
echo "</table>";
My question: Can anyone spot from the code, why the first row would come out like this?
The problem comes from this line:
if (!$firstRun)
echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
If you don't want to display the first line, use the brackets:
if (!$firstRun){
echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
echo "<td>" . $difference . "</td></tr>";
}
It's your "if" statement. It doesn't echo anything on the first run, so the starting tr and td don't get echoed, so you end up with an incorrect row (ended with the /tr tag) containing only a single td value. Did you mean to put brackets around the two echo statements so that they only happen when firstrun is false?
first of all, where is $column defined ?
$difference = $row[$column] - $previous;
second, this is only executed as of the second iteration
if (!$firstRun)
echo "<tr><td>" . date("G:i:s", strtotime($row["tstamp"])) . "</td>";
This means that the first time in the while loop, you are not creating the table row <tr> , although I'm guessing the browser is able to "fix" the missing tag, but this would be the reason why -32722 appears in the first column.