循环结果显示在循环外部

时间:2012-05-18 00:13:21

标签: php

我试图让我的结果显示在循环内部,但由于某种原因,它一直显示在我的循环之外,当我创建我的= a =并且我在最后添加。$ _ str。但是,如果我使用。= for my =并从字符串末尾删除。$ _ str,那么它将显示在我的循环内部,但不会反转foreach循环的输出,这是我试图实现的平均目标。 / p>

我评论了我遇到问题的地区。请随意注释掉其中一行以进行测试,看看我在说什么,代码已经准备好进行测试了。

        <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;
        ?>

感谢任何可以帮助我的人。

1 个答案:

答案 0 :(得分:1)

    <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;
    ?>