Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts

    getting a string of dynamic text partly generated by a function into a variable

    PHP Code:

    misc php code here

    ?>
        <td class="logtabletd">
    <?php  percentchange($myfieldskey($myfields)  ]   [symbol1],$myfieldskey($myfields)  ]   [price1]) ?>
    </t>
         
    <?php

    more misc php code here

    so percentchange is a function that echos out a string, and the above displays some html code, with a dynamic string that is generated by "percentchange"

    However I want to put all of it into a variable and write it to the contents of a static html file but I can't seem to be able it into a variable...

    I've tried something like this

    PHP Code:

    misc php code here

    $string ='
    ?>
        <td class=/"logtabletd/">
    <?php  percentchange($myfieldskey($myfields)  ]   [symbol1],$myfieldskey($myfields)  ]   [price1]) ?>
    </t>
         
    <?php
    ;
    more misc php code here
    echo $string;    //this does not work


    Does anyone have any idea on how this should work?

    I have a feeling it has to do with the function percentchange but I'm not entirely sure.
    Last edited by phpchick; 12-10-2012 at 08:48 PM.

  • #2
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    if I do this

    PHP Code:
    $string = '

    <td class=/"logtabletd/"><?php  percentchange($myfieldskey($myfields)  ]   [symbol1],$myfieldskey($myfields)  ]   [price1]) ?></t>
         
     ';
    echoing $string gives me

    <td class=/"logtabletd/"><?php percentchange($myfields[ key($myfields) ] [symbol1],$myfields[ key($myfields) ] [price1]) ?></t>


    Which is not what I want.


    If I do this

    PHP Code:
    misc php code
    ?>

    <td class=/"logtabletd/"><?php  percentchange($myfieldskey($myfields)  ]   [symbol1],$myfieldskey($myfields)  ]   [price1]) ?></t>
         
    <?php 
    misc php code
    I get the result I want,

    <td class=/"logtabletd/"><span class="loggreen">3.2%</span></t>

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Yep, its the function.
    Since you have done like so: <?php functionCall(); and it prints the results, that indicates that the function doesn't return a result (or perhaps it does as well), but it prints the results directly.
    Your options are either to rewrite the function (and all of the code that uses it) to return a result (my recommendation), or you can capture the output of the function using output buffering.
    PHP Code:
    ob_start();
    percentchange($myfieldskey($myfields)  ]   ['symbol1'],$myfieldskey($myfields)  ]   ['price1']);
    $result ob_get_contents();
    ob_end_clean();

    printf('Captured from the function: %s' PHP_EOL$result); 

  • #4
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    foulou, this is what my function currently looks like

    PHP Code:
      function getstockstats($stocksymbol,$price)
      {
        include_once(
    'class.yahoostock.php');
        
    $objYahooStock = new YahooStock;
        
    $objYahooStock->addFormat("snl1d1t1cvc1p2");
        
    $objYahooStock->addStock($stocksymbol);

        foreach( 
    $objYahooStock->getQuotes() as $code => $stock)
            {
                                echo  
    '<span class="logred">'number_format($stock[2],2) . '</span>';
            }
      } 


    Would this be a proper way of 'returning' it?

    PHP Code:
    function getstockstats($stocksymbol,$price)
      {
        include_once(
    'class.yahoostock.php');
        
    $objYahooStock = new YahooStock;
        
    $objYahooStock->addFormat("snl1d1t1cvc1p2");
        
    $objYahooStock->addStock($stocksymbol);

        foreach( 
    $objYahooStock->getQuotes() as $code => $stock)
            {
                                
    $returnthis =  '<span class="logred">'number_format($stock[2],2) . '</span>';
            }
            
    return 
    $returnthis;
      } 
    I'm not so sure what happens after this though...

  • #5
    Regular Coder
    Join Date
    May 2011
    Location
    new york
    Posts
    118
    Thanks
    4
    Thanked 0 Times in 0 Posts
    I realized I could ob_start() that entire html sequence

    So I have opted to do that, it seems simple enough and only requires one ob_start() function.

    Thank you so much foulu

  • #6
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 Posts
    Your welcome. If you haven't used this function in anything else, I'd recommend changing it to return a result instead since you have the need to do so.
    The return is almost correct, you need to change this line:
    PHP Code:
                                $returnthis =  '<span class="logred">'number_format($stock[2],2) . '</span>'
    to this:
    PHP Code:
                                $returnthis .=  '<span class="logred">'number_format($stock[2],2) . '</span>'
    And add a $returnthis = ''; immediately before the foreach call.

    There are a number of other options as well which would walk that array and map the strings into them, and then implode it to the returned string. But what you have here would be fine.


  •  

    Posting Permissions

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