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
Related
I have an sql query that looks like this:
//do a query to find all the samples in the currently selected rack
$sql = "SELECT SampleID, ColumnNumber, RowNumber FROM Samples WHERE Rack = ?";
$stmt = $conn->prepare($sql);
$stmt->bind_param("s", $rack);
$stmt->execute();
$rackContents = $stmt->get_result();
In each rack, a ColumnNumber and RowNumber combination describes a position in the rack, starting with:
ColumnNumber = 1, RowNumber = 1 then
ColumnNumber = 2, RowNumber = 1
and so on until (for example, in a 10x10 rack)
ColumnNumber = 10, RowNumber = 10.
I want to display the SampleIDs in each position of the rack in a table. Using a nested loop, I can draw a table that has a <td> for each rack position. What I can't figure out is how to display the SampleID based on the ColumnNumber and RowNumber combination for each position.
This is how I build the table:
//start a loop that visits each row
$rownumber = 1;
while ($rownumber <= $numberofrows){
//start a loop that visits each column
//start a row
echo "<tr>";
$columnnumber = 0;
while ($columnnumber <= $numberofcolumns){
if ($columnnumber == 0){
echo "<td>" . $rownumber . "</td>";
}
else{
//find the sample number for the relevant column and row
//inside the array we got in an sql query earlier, $rackContents
echo "<td>" . x . "</td>";
//What do I put in place of the x to output the appropriate
//SampleId for the ColumnNumber/RowNumber combo?
}
$columnnumber++;
}//end the columnnumber loop
//end the row
echo "</tr>";
$rownumber++;
}//end the rownumber loop
I've been looking at array_search, but I just can't get anywhere.
If I am understanding you correctly I think you just need to fetch the array from your result. Although I am not sure this is the result you actually want.
while ($rackContent = $rackContents->fetch_array(MYSQLI_NUM))
$rownumber = 1;
while ($rownumber <= $numberofrows){
echo "<tr>";
$columnnumber = 0;
while ($columnnumber <= 3){
if ($columnnumber == 0){
echo "<td>" . $rownumber . "</td>";
} else {
echo "<td>" . $rackContent['SampleID'] . "</td>";
}
$columnnumber++;
echo "</tr>";
$rownumber++;
}
}
But I think you probably want something more like this.
while ($rackContent = $rackContents->fetch_array(MYSQLI_NUM))
echo "<tr>";
echo "<td>" . $rownumber . "</td>";
echo "<td>" . $rackContent['SampleID'] . "</td>";
echo "<td>" . $rackContent['ColumnNumber'] . "</td>";
echo "<td>" . $rackContent['RowNumber'] . "</td>";
echo "</tr>";
}
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>";
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.
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;
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.