ExpressionEngine CMS
Open, Free, Amazing

Thread

This is an archived forum and the content is probably no longer relevant, but is provided here for posterity.

The active forums are here.

number_format and variable

June 01, 2011 6:30pm

Subscribe [4]
  • #1 / Jun 01, 2011 6:30pm

    haggis

    70 posts

    I’m just blundering around for fun, attempting to format the ‘hits’ variable output with a comma for one particular template.

    EE 2.1.3
    PHP 5.2 (also tried 5.3)
    PHP processed on template output

    My test template contains these attempts to tease out where the error might be since I know nothing:

     1. <?php echo "{hits}"; ?> 
     2. <?php echo number_format( "{hits}" ); ?> 
     3. <?php $number ="{hits}"; echo number_format( $number ,0 ); ?> 

    which outputs this:

    1. 155052
    2. 0
    3. 0

    I’ve tried cutting and pasting a few other number_format examples I found various places, but it seems I really only need the simplest use case of one argument, which should just be a whole number with thousands delimited by a comma. Why the ‘0’ results?

    I’m pretty much a “cut-n-paste PHP newbie” so apologies in advance if I’m staring something obvious right in the eye 😛

  • #2 / Jun 02, 2011 6:50am

    John Henry Donovan

    12339 posts

    Hi haggis,

    Have you tried setting the template to parse on input?

    I found this handy plugin last week which may help you.

  • #3 / Jun 02, 2011 10:56am

    haggis

    70 posts

    Parsing input v. output doesn’t appear to affect result.

    The plugin indeed looks promising. I did try a plug-in called, appropriately, Number Format, but I am running MSM and had some trouble installing it. Neither plug-in is tagged as MSM-compatible, unfortunately.

  • #4 / Jun 03, 2011 6:32am

    John Henry Donovan

    12339 posts

    haggis,

    Regardless of how that plugin was tagged it works across MSM sites for me. Can you give it a shot?

  • #5 / Jun 06, 2011 11:04am

    haggis

    70 posts

    Good news is the plugin works; bad news is it returns a zero on “hits”.

    {exp:price_format price="155000" decimals="0"}

    155,000

    {exp:price_format price="{hits}" decimals="0"}

    0

  • #6 / Jun 06, 2011 2:23pm

    Kevin Smith

    4784 posts

    Hey haggis–

    That’s going to be a parse order issue. {hits} isn’t parsed until after plugins, so the plugin doesn’t have a value to work with. Same thing even with PHP parsed on output. It takes place before the {hits} variable is available.

    You might want to see if you can find a solution in the forums for accessing Global Variables using PHP instead of the usual template tags. I did a quick search, but didn’t find anything myself.

    Kevin

  • #7 / Jun 06, 2011 2:32pm

    haggis

    70 posts

    That makes a lot of sense explaining the goings-on. Will see if I can find a PHP way into {hits}. Cheers!

  • #8 / Jun 06, 2011 5:47pm

    Kevin Smith

    4784 posts

    Sounds good! I’ll leave this thread open for a bit to give you time to hunt it down. If you do, please post back here to help out others that might find this thread.

    As a nice resource, you may want to download this to your computer. One of our community members, Lodewijk Schutte, put together that nice PDF with detailed information on EE2’s parse order.

  • #9 / Jun 13, 2011 11:05am

    haggis

    70 posts

    Well neither my search-foo nor my PHP skillz are up to the task 😛 I found what I think are some sorta-similar questions that make me suspect that I might need an expression of the general equivalent to this, but somehow tweaked for {hits}.

    <?php $member_id = $this->EE->session->userdata('member_id'); ?>
  • #10 / Jun 14, 2011 4:10pm

    Greg Salt

    3988 posts

    Hi haggis,

    This may be what you’re after (PHP in Output on a template):

    <?php echo $this->EE->TMPL->template_hits; ?>

    Cheers

    Greg

  • #11 / Jun 14, 2011 4:55pm

    haggis

    70 posts

    w00t! That’s the answer!

    <?php echo number_format( $this->EE->TMPL->template_hits,0 ); ?>

    gives me exactly what I wanted.

    Many thanks!

  • #12 / Jun 15, 2011 3:06pm

    Brandon Jones

    5500 posts

    Awesome, haggis. Glad Greg’s solution worked!

.(JavaScript must be enabled to view this email address)

ExpressionEngine News!

#eecms, #events, #releases