Helpful Information
 
 
Category: vB3 Programming Discussions
Number format

Can anyone please tell me an easier way to do this? I want to have the thread views in a number format (with an apostrophe after 1000). Here is the code I got it to work with but there must be a better and easier way to accomplish this.

$dbview = $DB_site->query_first("SELECT SUM(views) AS totalviews FROM thread");
$allviews = $dbview[totalviews];
$countviews=$DB_site->query_first("SELECT SUM(views) AS views FROM thread WHERE postuserid='$userinfo[userid]'");
$userthreadviews=$countviews['views'];
$threadviewpercent = round(($userthreadviews / $allviews) * 100,2);
$userthreadviews=number_format($countviews['views']);
if ($userthreadviews AND $threadviewpercent <> "0") {
eval("\$threadviews = \"".gettemplate("getinfo_threadviews")."\";");
} else {
$threadviews = '';
}

This is the line I had to insert to get it to work right.

$userthreadviews=number_format($countviews['views']);

I tried doing the number_format in the first instance of this code, but it messed up the percentage of totalviews there.

The problem is that number_format(...) changes the resulting variable to a string. If you look back at that code, the variable you are converting ($userthreadviews) is used in an if(...) statement in the next line.

Try the following code:


$dbview = $DB_site->query_first("SELECT SUM(views) AS totalviews FROM thread");
$allviews = $dbview[totalviews];
$countviews=$DB_site->query_first("SELECT SUM(views) AS views FROM thread WHERE postuserid='$userinfo[userid]'");
$userthreadviews=$countviews['views'];
$threadviewpercent = round(($userthreadviews / $allviews) * 100,2);
$userthreadviews=$countviews['views'];
if ($userthreadviews AND $threadviewpercent <> "0") {
$userthreadviews = number_format($userthreadviews);
eval("\$threadviews = \"".gettemplate("getinfo_threadviews")."\";");
} else {
$threadviews = '';
}


The basic idea is, only use number_format(...) right before you display the value, to ensure that you're not screwing up any future logic (there could be more in this case, I wouldn't know without looking at the whole code...)

Thanks for clearing that up for me. :) The code I had above worked, but I had to put two lines in it to make it work. I didn't realize I could move it down to just before I evaluated the template. Thanks again. This will definitely help in the future as well. :)










privacy (GDPR)