user874185
user874185

Reputation: 853

loop results display outside of loop

I am trying to get my results to display inside of the loop but for some reason it keeps displaying outside of my loop when ever I make my = a = and I add .$_str at the end. however if i use .= for my = and remove the .$_str from the end of the string it then displays inside of my loop but doesnt not reverse the output from the foreach loop, which is my mean goal im trying to achieve.

I have commented the area I am having the problem at. Please feel free to comment out either one of the lines to test and see what I am saying, the code is already ready to be tested.

        <script type='text/javascript' src='http://www.google.com/jsapi'></script>
        <?php
        $file = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=03&b=18&c=2004&d=04&e=17&f=2012&g=d&ignore=.csv");

            $stockcontent = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '',  $file);
            $stockcontent = trim($stockcontent);

            $stockcontentex = str_getcsv($stockcontent, "\n");

            $i = 0;
            $j = 0;

            $_str = '';  
            $_str .= "<script type='text/javascript'>
              google.load('visualization', '1', {'packages':['annotatedtimeline']});
              google.setOnLoadCallback(drawChart);
              function drawChart() {
                var data = new google.visualization.DataTable();
                data.addColumn('date', 'Date');
                data.addColumn('number', 'High');
                data.addColumn('number', 'Low');
                data.addRows([";
            foreach($stockcontentex as $stockexplode){
                $stockex = explode(',',$stockcontentex[$i++]);
                $stockexdate = explode('-', $stockex[0]);
                $stockYear = $stockexdate[0];
                $stockMonth = $stockexdate[1];
                $stockDay = $stockexdate[2];
                $stockHigh = $stockex[2];
                $stockLow = $stockex[3];

    //right here is where I am having the problem.
$_str = '[new Date('.$stockYear.', '.$stockMonth.', '.$stockDay.'), '.$stockHigh.', '.$stockLow.'],'. "\n".$_str;


    //$_str .= '[new Date('.$stockYear.', '.$stockMonth.', '.$stockDay.'), '.$stockHigh.', '.$stockLow.'],'. "\n";

                }

              $_str .= "]);

                var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
                chart.draw(data, {displayAnnotations: false});
              }
            </script>
            <div id='chart_div' style='width: 700px; height: 240px;'></div>";

                echo $_str;
        ?>

Thank you to anyone that can help me on this.

Upvotes: 0

Views: 143

Answers (1)

sarveshseri
sarveshseri

Reputation: 13985

    <script type='text/javascript' src='http://www.google.com/jsapi'></script>
    <?php
    $file = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=GOOG&a=03&b=18&c=2004&d=04&e=17&f=2012&g=d&ignore=.csv");

        $stockcontent = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '',  $file);
        $stockcontent = trim($stockcontent);

        $stockcontentex = str_getcsv($stockcontent, "\n");

        $i = 0;
        $j = 0;

        $_str = '';  
        $_str .= "<script type='text/javascript'>
          google.load('visualization', '1', {'packages':['annotatedtimeline']});
          google.setOnLoadCallback(drawChart);
          function drawChart() {
            var data = new google.visualization.DataTable();
            data.addColumn('date', 'Date');
            data.addColumn('number', 'High');
            data.addColumn('number', 'Low');
            data.addRows([";

        $tstr = "";
        foreach($stockcontentex as $stockexplode){
            $stockex = explode(',',$stockexplode); // no need to do this $stockcontentex[$i++]); here
            $stockexdate = explode('-', $stockex[0]);
            $stockYear = $stockexdate[0];
            $stockMonth = $stockexdate[1];
            $stockDay = $stockexdate[2];
            $stockHigh = $stockex[2];
            $stockLow = $stockex[3];

            //right here is where I am having the problem.
            $tstr = '[new Date('.$stockYear.', '.$stockMonth.', '.$stockDay.'), '.$stockHigh.', '.$stockLow.'],'. "\n".$tstr;


            }
         $_str = $_str.$tstr; // $x .= $xx; is just a shotcut for $x = $x.$xx;
         $_str .= "]); 

         var chart = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
            chart.draw(data, {displayAnnotations: false});
          }
        </script>
        <div id='chart_div' style='width: 700px; height: 240px;'></div>";

        echo $_str;
    ?>

Upvotes: 1

Related Questions