SitePoint Sponsor

User Tag List

Results 1 to 13 of 13
  1. #1
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need help with number_format

    I've used the number_format function before, but I'm having trouble inserting iit in a new script I just acquired. This is the original script:

    PHP Code:
     $row = <<<EOD
        <tr style="background-color:$c"><{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}>
        <td class="tdname">
    {$row["NameC"]} ({$row["NameParent"]})</td>
        <td class="
    {$mydata}">{$row[$myfield]}</td>
        </tr>
    EOD;
    echo 
    $row
    ...and this is how I've inserted number_format, using both double and single quotes:

    PHP Code:
     $row = <<<EOD
        <tr style="background-color:$c"><{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}>
        <td class="tdname">
    {$row["NameC"]} ({$row["NameParent"]})</td>
        <td class="
    {$mydata}">" . number_format{$row[$myfield]} . "</td>
        </tr>
    EOD;
    echo 
    $row
    Neither way works, and I'm not sure how to write it. Any tips? Thanks.

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    geo,

    Quote Originally Posted by php.net/manual/en/language.types.string.php#language.types.string.syntax.heredoc
    Heredoc text behaves just like a double-quoted string, without the double-quotes. This means that you do not need to escape quotes in your here docs, but you can still use the escape codes listed above. Variables are expanded, but the same care must be taken when expressing complex variables inside a here doc as with strings.
    Ergo, your error is in how you're calling PHP code within the string defined by the heredoc assignment. Easy enough to fix.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn
    geo,

    Ergo, your error is in how you're calling PHP code within the string defined by the heredoc assignment. Easy enough to fix.

    Regards,

    DK
    I don't understand. If I remove the quotes and the periods, I wind up with the following, which still doesn't work:

    PHP Code:
     $row = <<<EOD
        <tr style="background-color:$c"><{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}>
        <td class="tdname">
    {$row["NameC"]} ({$row["NameParent"]})</td>
        <td class="
    {$mydata}">number_format{$row[$myfield]}</td>
        </tr>
    EOD; 

  4. #4
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    declare your variables outside of your heredoc thus:

    PHP Code:
    $self $_SERVER["PHP_SELF"];
    $code $row["CCode"];
    $name_c $row["NameC"];
    $name_parent $row["NameParent"];

    $row = <<<EOD
        <tr style="background-color:$c"><$self?id=$code>
        <td class="tdname">
    $name_c ($name_parent)</td>
    EOD; 
    that should work... heredoc cannot process functions etc... it expands variables - thats about it.

  5. #5
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by djdykes
    declare your variables outside of your heredoc thus:

    PHP Code:
    $self $_SERVER["PHP_SELF"];
    $code $row["CCode"];
    $name_c $row["NameC"];
    $name_parent $row["NameParent"];

    $row = <<<EOD
        <tr style="background-color:$c"><$self?id=$code>
        <td class="tdname">
    $name_c ($name_parent)</td>
    EOD; 
    that should work... heredoc cannot process functions etc... it expands variables - thats about it.
    I'm confused; are you saying I can't use the number_format function with a heredoc script? Or do I just have to format number_format outside the heredoc?

    What I'm focusing on right now is $myfield, which is already defined outside heredoc. For example, one page draws data from a field named "pacific," so here's what I have in the head section:

    PHP Code:
    $myfield 'pacific'
    If "pacific" features numerals, can I modify the above function to include number_data? If so, what would it look like? I've tried this:

    PHP Code:
    $myfield 'number_data(pacific)'
    And this is the base script:

    PHP Code:
     $row = <<<EOD
        <tr style="background-color:$c"><{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}>
        <td class="tdname">
    {$row["NameC"]} ({$row["NameParent"]})</td>
        <td class="
    {$mydata}">{$row[$myfield]}</td>
        </tr>
    EOD;
    echo 
    $row
    Thanks.

  6. #6
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    geo,

    You missed the point completely! You are using PHP code within the string without opening PHP to be used. Try:

    Code:
     $row = <<<EOD
        <tr style="background-color:$c"><?php{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}?>
        <td class="tdname"><?php{$row["NameC"]} ({$row["NameParent"]})?></td>
    ... etc. THINK in terms of trying to write PHP code in the middle of your page's HTML and you'll be back on track.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  7. #7
    SitePoint Wizard Young Twig's Avatar
    Join Date
    Dec 2003
    Location
    Albany, New York
    Posts
    1,355
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    David, heredoc is inside of <?php ?> tags. There's no need to open the tags again.

    Geosite, I can't figure out what's going on in your script. Why do you have text in a table row, but outside of the cell? Try this:

    PHP Code:
     $row[$myfield]=number_format($row[$myfield]);
     
    $row = <<<EOD
        <tr style="background-color:{$c}"><{$_SERVER['PHP_SELF']}?id={$row['CCode']}>
        <td class="tdname">
    {$row['NameC']} ({$row['NameParent']})</td>
        <td class="
    {$mydata}">{$row[$myfield]}</td>
        </tr>
    EOD;

    echo 
    $row

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Young,

    You're probably right -- but I don't get $_SERVER (or $_POST/$_GET) variables to work inside double quotes so there's no way for them to work in a heredoc.

    Personally, I think geo shouldn't be using a heredoc in this situation so my suggestion was to try to get his "style" to work. Yeah, yeah, probably the wrong way to attack his problem on my part! Instead, he should be capturing those values as "regular" variables before entering his heredoc and referencing them in the heredoc.

    Good catch.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Guru dbevfat's Avatar
    Join Date
    Dec 2004
    Location
    ljubljana, slovenia
    Posts
    684
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn
    ...

    You're probably right -- but I don't get $_SERVER (or $_POST/$_GET) variables to work inside double quotes so there's no way for them to work in a heredoc.
    ...
    What do you mean by that?

    Inside double quotes within heredoc or like echo "$_SERVER[something]"?

  10. #10
    SitePoint Wizard cranial-bore's Avatar
    Join Date
    Jan 2002
    Location
    Australia
    Posts
    2,634
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You can use $_SERVER, $_POST, $_GET variables in a herodoc syntax if they are enclosed with curly braces. Same when they exist in double quotes.
    i.e
    PHP Code:
     echo "this page is {$_SERVER['PHP_SELF']} and it is great"
    and actually the curly brackets are only needed if the array key has a space in it. Otherwise they are not necessary.

    And geosite, as Young Twig said you can't break out of Herodic to use functions such as number quotes. You need to either number format the variables before going into the heredoc mode, or use a double quoted string which you can break out of and contcatinate using full stops.
    I'd suggest that formatting your variables first will give you easier to read code.

  11. #11
    Non-Member
    Join Date
    Jan 2004
    Location
    Seattle
    Posts
    4,328
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for all the tips. I was off on a long errand, but I've got it working now. This is what I wound up with:

    PHP Code:
    $wow number_format($row[$myfield]);
    echo <<<EOD
         <tr 
    style="background-color:
    $c"><{$_SERVER["PHP_SELF"]}?id={$row["CCode"]}>
         <td class="tdname">
    {$row["NameC"]} ({$row["NameParent"]})</td>
         <td class="
    {$mydata}">{$wow}</td>
         </tr>
    EOD; 
    There's one page that displays text, not numbers, so I'll have to make some sort of IF/ELSE statement that omits the number_format function for that page.

    Thanks.

  12. #12
    SitePoint Evangelist djdykes's Avatar
    Join Date
    Feb 2005
    Location
    Chester, Cheshire
    Posts
    566
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    yeah sorry - i missed off the number format bit of your code... duh...

  13. #13
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,604
    Mentioned
    19 Post(s)
    Tagged
    2 Thread(s)
    Twig, DJ, Dbev, Geo, ...

    My apologies on that heredoc thing -- I'd forgotten ALL about the curly braces and did the necessary search and discovered (after having it pointed out) the error of my ways

    I guess you can teach an old dog new tricks -- like reading the FINE manual!

    Thanks!

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •