0
我有一些問題,我需要指導。我試圖解析3個CSV文件,並把它們放入一個foreach循環,但它的可怕性是錯誤的。字符串$ stockClose不會改變它仍然是相同的,通過了循環,$ nas_stock變化,每20環,它是想改變每一個循環中,這似乎是正常工作的僅$ sp500_stock,即時通訊不知道爲什麼在foreach中解析foreach
$currentMonth = date('n');
$currentMonth = $currentMonth - 1;
$prevcurrentMonth = date('n');
$prevcurrentMonth = $prevcurrentMonth - 2;
$currentDay = date('j');
$lastDay = $currentDay - 6;
$currentYear = date('Y');
$filedow = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EDJI&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$filenas = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EIXIC&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$filesp500 = file_get_contents("http://ichart.finance.yahoo.com/table.csv?s=%5EGSPC&a=$prevcurrentMonth&b=$lastDay&c=$currentYear&d=$currentMonth&e=$currentDay&f=$currentYear&g=d&ignore=.csv");
$stockcontent = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filedow);
$stockcontent = trim($stockcontent);
$stockcontentex = str_getcsv($stockcontent, "\n");
$stocknas = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filenas);
$stocknas = trim($stocknas);
$stocknasex = str_getcsv($stocknas, "\n");
$stocksp500 = str_replace('Date,Open,High,Low,Close,Volume,Adj Close', '', $filesp500);
$stocksp500 = trim($stocksp500);
$stocksp500ex = str_getcsv($stocksp500, "\n");
$i = 0;
$j = 0;
$_str = '';
$_str .= "<script type='text/javascript'>
google.load('visualization', '1', {packages: ['annotatedtimeline']});
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Dow Jones');
data.addColumn('number', 'Nasdaq');
data.addColumn('number', 'S&P 500');
data.addRows([";
$tstr = "";
$_str = '';
$_str .= "<script type='text/javascript'>
google.load('visualization', '1', {packages: ['annotatedtimeline']});
function drawVisualization() {
var data = new google.visualization.DataTable();
data.addColumn('date', 'Date');
data.addColumn('number', 'Dow Jones');
data.addColumn('number', 'Nasdaq');
data.addColumn('number', 'S&P 500');
data.addRows([";
$tstr = "";
foreach($stocknasex as $nas){
$nasex = explode(',',$nas);
$nas_stock = $nasex[4];
}
foreach($stocksp500ex as $sp500){
$sp500ex = explode(',',$sp500);
$sp500_stock = $sp500ex[4];
}
foreach($stockcontentex as $stockexplode){
$stockex = explode(',',$stockexplode);
$stockexdate = explode('-', $stockex[0]);
$stockYear = $stockexdate[0];
$stockMonth = $stockexdate[1] - 1 ;
$stockDay = $stockexdate[2];
$stockHigh = $stockex[2];
$stockLow = $stockex[3];
$stockClose = $stockex[4];
}
for($i=0; $i<=30; $i++){
$tstr = '[new Date('.$stockYear.', '.$stockMonth.', '.$stockDay.'), '.$stockClose.', '.$nas_stock.', '.$sp500_stock.'],'. "\n".$tstr;
}
$_str = $_str.$tstr;
$_str .= "]);
var annotatedtimeline = new google.visualization.AnnotatedTimeLine(document.getElementById('chart_div'));
annotatedtimeline.draw(data, {
//'allValuesSuffix': '%', // A suffix that is added to all values
'colors': ['#12577F', '#769422', '#999999'], // The colors to be used
'displayAnnotations': true,
'displayExactValues': true, // Do not truncate values (i.e. using K suffix)
'displayRangeSelector' : false, // Do not sow the range selector
'displayZoomButtons': true, // DO not display the zoom buttons
'legendPosition': 'newRow', // Can be sameRow
//'max': 600, // Override the automatic default
//'min': 500, // Override the automatic default
'scaleColumns': [0, 1], // Have two scales, by the first and second lines
'scaleType': 'allmaximized', // See docs...
'thickness': 2, // Make the lines thicker
});
}
google.setOnLoadCallback(drawVisualization);
</script><div style='float:left; border:1px solid #ccc;'>
<div id='chart_div' style='width: 500px; height: 230px;'></div></div>";
return $_str;
break;
}
我繼續調整我的代碼,我明白你的說法,但即時通訊也不太清楚如何固定我的問題。我希望所有人都能在沒有前面的人的情況下循環。我把它從foreach循環中取出,並在其上放置了一個for循環,但這只是讓它們在每個循環中重複相同的輸出而不是前進,我感謝您循環並向我解釋 – user874185
我仍然不明白正是你真正想要完成的。從循環的細節中脫離出來;在高層次,你想做什麼?從三個CSV文件中獲取數據並執行相關操作?將它合併成一個文件? –
從所有3個文件中獲取數據並使它們在一個循環中打印 – user874185