0

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

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

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

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

4

1 回答 1

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;
    ?>
于 2012-05-18T00:33:51.823 回答