2012-05-18 231 views
0

我試圖讓我的結果顯示在循環內部,但由於某些原因,它始終顯示在我的循環外部,當我使我= a =和我添加。 。然而,如果我使用。= for my =並從字符串末尾移除。$ _ 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; 
     ?> 

謝謝任何​​能夠幫助我的人。

+0

您是否打算將「$ _str ='[new Date('。$ stockYear。',...」)替換爲您標記爲問題的行上的「$ _str。= ...」? –

+1

你真的應該閱讀[HEREDOC](http://php.net/heredoc)和[json_encode()](http://php.net/json_encode)。直接從PHP生成javascript風險很高 - 一個單個放錯位的javascript元字符,你打破了整個javascript代碼塊。 –

+0

@tom_yes_tom是我要評論,因爲它不會扭轉結果。 – user874185

回答

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

'$ str。= $ new_str'只是'$ str = $ str。$ new_str'沒什麼特別的。 –