PHP Format Number With Dollar Sign And Comma And ROUND() - php

I have been using this syntax
print "<td>" . "$" . round($res->DollarValue) . "</td>";
Which will output something like this: $8812 I want the format to be $8,812 so I tried this
print "<td>" . "$" . round(number_format($res->AveragePrice)) . "</td>";
but that did not do the trick either.
What I am after is rounding with a dollar sign and comma, but no decimals.
Further Examples
1000.78 -> $1,001
1000.23 -> $1,000
What is the php synatx for this?

Related

Match an actual dollar symbol in php regex

I haven't used regex in a long time and can't figure out how to match an actual dollar symbol and any reference to dollar symbol and regex tells me about the special meanings and cases. I need to match a $. I expected that \$ or $$ was supposed to escape it, but I'm still not matching it.
Here's my text
(WW) Capacity Charge
. . . . . . . . . . . . . . . . $ 123.45
WW Commodity Charge . . . . . . . . . $ 67.89
I'm trying to capture 123.45 I figured I should just match the first occurrence where some characters are sandwiched between the dollar symbol, space and a newline. Here are a few of the regexes I've tried.
preg_match("|(?<=\$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$\s)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=\$)(.*)(?=\n)|",$data[1],$matches); //no matches
preg_match("|(?<=$$)(.*)(?=\n)|",$data[1],$matches); //no matches
Just to check that something matches I even did
preg_match("|(?<=\.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=.)(.*)(?=\n)|",$data[1],$matches); // . . . . . . . . . . . . . . . $ 123.45
preg_match("|(?<=1)(.*)(?=\n)|",$data[1],$matches); // 23.45
How can I match the text between the $ and the newline?
You are in double quotes so you need to escape twice (once for PHP, then once for PCRE). I prefer a character class though because that works in all regex flavors.
(?<=[$]\s)(.*)(?=\n)

PHP Passing variable in URL

Im trying to pass a variable in the URL and to post the variable on the page. I currently have a table and a variable named $record['Filename']. The value is not displaying correctly.
Right now I have the following
while($record = mysql_fetch_array($mydata)){
echo "<tr>";
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
echo "<td>" . $record['Description'] . "</td>";
echo "</tr>";
}
PHP strings 101:
echo "<td>" ."<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>". " </td>";
^--start string end string --^
Since you never "exit" your "-quoted strings, your . are just plaintext periods, not concatenation operators.
You probably want this:
echo <<<EOL
<td>{$record['Filename']}</td>
EOL;
Notice how using a heredoc removes the need to hop in/out of string mode, and you can use proper quoting in the HTML. Also note the {}-extended string syntax around the variables.
try to change,
<a href='Info.html?page='.$record['Filename'].> $record['Filename'] </a>
to
<?php echo $record['Filename'];?>
Use single quotes instead of double quotes. It's faster and more efficient.
while($record = mysql_fetch_array($mydata)){
echo '<tr>';
echo '<td>'.$record['Filename'].'</td>';
echo '<td>'. $record['Description'].'</td>';
echo '</tr>';
}

PHP, While Loop a Table to show results from a database

I am having a problem trying to get a row to create everytime I enter a new database entry.
My code so far is:
<table align="center">
<th>MH/s</th><th>Contact Length</th><th>Date Bought</th><th>Payment</th>
<?php while ($row_cnt > 0) {
echo "<tr><td>" . $row['mhbought'] . "</td><td>" . $row['length'] . "</td><td>" . date(d-m-Y, $row['datebought'] . "</ td><td>" . $row['payment'] . "</td></tr>";
}
?>
</table>
The error I receive however states PHP Parse error: syntax error, unexpected ';' (which is the echo line) Am I doing something completely dumb here, can this even be done?
Thank you for any help you may provide.
Looking at the code, the problematic line is this:
date(d-m-Y, $row['datebought']
^^ missing quotes ^ missing closing parenthesis
Change it to:
date('d-m-Y', $row['datebought'])
You haven't close date() right bracket and there is quotation missing for date format. Your echo should look like:
echo "<tr><td>" . $row['mhbought'] . "</td><td>" . $row['length'] . "</td><td>" . date('d-m-Y', $row['datebought']) . "</td><td>" . $row['payment'] . "</td></tr>";

Escaping minus sign in PHP echo statement

I'm sure I'm missing something obvious here: the following is echoing lat-long variables from MySQL, and the longitude variable begins with a minus sign, which prevents the echo statement from reading it and all that follows it. I'm sure there is a way to clean/escape that but just can't work it out.
echo "http://maps.google.com/maps?ll=" . $row['latitude'] . "," . $row['longitude'] . " target=_new>View in Google Maps";
This is output from a PDO query and testing passing the lat-long into Google Maps.
As I understand, it's a link?
Then, use urlencode for string.
The minus signs are not a problem. You may need to urlencode() because of the comma, but you need quotes around the URL in the href as well:
echo '<br /><a href="http://maps.google.com/maps?ll='
. urlencode($row['latitude'] . ',' . $row['longitude'])
. '" target="_new">View in Google Maps</a>';

html entities and angle brace issue

So, I have this rel/URL I am trying to stuff into a variable so I can print it out elsewhere:
$relnext = "<link rel='next'
href='javascript:".$content_pager->PagerName
. "_form."
. $content_pager->PagerName
. "PagerPage.value=\""
. $content_pager->Page+1
. "\"; "
. $content_pager->PagerName
. "DoSubmit();' />";
As it is, when I print it out, all I get is:
1"; MediaBoxContentDoSubmit();' />
After some research it 'appears' that I should use htmlentities, but:
echo htmlentities($relnext);
also just produces:
1"; MediaBoxContentDoSubmit();' />
Is there some other function that should be used here?
Thanks very much for any help you can give!
You have an operator precedence / associativity problem. The . operator to the left of the + operator execute before the + operator, because they are all left-associative and have the same precedence. You want the + operator (in $content_pager->Page+1) to execute first, then all the . operators.
As it is, you are adding a string to a number (1) with the + operator, in which case the string (everything before the +) will be taken as 0. That is why the first character is a 1, because it is the result of "some string"+1, which is interpreted as 0+1.
So, your first snippet should be:
$relnext = "<link rel='next' href='javascript: " . $content_pager->PagerName . "_form." . $content_pager->PagerName . "PagerPage.value=\"" . ($content_pager->Page+1) . "\"; " . $content_pager->PagerName . "DoSubmit();' />";
Note that the $content_pager->Page+1 part is now in parentheses.
More information:
http://php.net/manual/en/language.operators.precedence.php

Categories