2011-09-14 74 views
0

我正在使用WP Alchemy創建具有多個複選框的元框。將CSV輸出到數組

我遇到的是麻煩,我有,我想創建一個從複選框選項的學術課程CSV列表。所以我正在把csv變成一個盒子製成的陣列。但是現在我已經有了代碼,只有最後一行在數組中創建。

我是自學的FrankinCoder當涉及到PHP的,所以裸跟我...

<?php 

$file_handle = fopen('curriculum.csv', 'r'); 

while (!feof($file_handle)) { 

$line_of_text = fgets($file_handle); 
$parts = explode(',', $line_of_text); 

$items = array ($parts[2] .$parts[3],); 

} 

fclose($file_handle); 

?> 

<?php foreach ($items as $i => $item): ?> 

    <?php $mb->the_field('cb_ex3'); ?> 

    <!-- similar to test #2, the same thing can be accomplished by simply 
    adding array brackets "[]" to the name --> 
    <input type="checkbox" name="<?php $mb->the_name(); ?>[]" value="<?php echo $item; ?>"<?php $mb->the_checkbox_state($item); ?>/> <?php echo $item; ?><br/> 

<?php endforeach; ?> 

等出了幾百行代碼中,我得到的是最後一行。 這裏是我正在使用的實際文件:http://www.ouhsd.k12.ca.us/educational_services/curriculum/curriculum.csv

任何幫助或建議,非常感謝!

+2

1'fgetcsv()'](http://php.net/fgetcsv )2.'$ items [] = ...' – salathe

回答

2

你應該使用fgetcsv()docs)函數,而不是試圖使CSV數據自己的感覺。另外,當你遍歷文件時,你應該建立$items數組(而不是覆蓋它),每次添加一個新項目。

$file_handle = fopen('curriculum.csv', 'r'); 
fgets($file_handle); // this skips the csv header line 
while (($parts = fgetcsv($file_handle)) !== FALSE) { 
    $items[] = $parts[2] . ' ' . $parts[3]; 
} 
fclose($file_handle); 

foreach ($items as $item) { 
    // Your HTML code goes here 
    echo $item . PHP_EOL; 
} 

獎勵積分(你會看起來冷卻器,使用對象和迭代器!)

$file = new SplFileObject('curriculum.csv'); 
$file->setFlags(SplFileObject::READ_CSV); 
foreach (new LimitIterator($file, 1) as $parts) { 
    $items[] = $parts[2] . ' ' . $parts[3]; 
} 

foreach ($items as $item) { 
    // Your HTML code goes here 
    echo $item . PHP_EOL; 
} 
+0

你是個天才和聖人!它工作過!!! :)我唯一想要的是echo $item . PHP_EQL;因爲它在複選框上打印所有內容。 –

4

在當你要添加到它,你替換$項目循環的每次迭代,因此只有到了最後的值。

而且http://php.net/fgetcsv可能是一些對你有用的。

+2

+1擊敗了我。 fgetcsv是在PHP中處理CSV的好方法。 –

+0

我想它是循環但不保留任何東西。任何想法如何使其fgetcsv和保持爆炸數組?我嘗試的每件事都留下了一個空白複選框。:( –

+0

+1 for fgetcsv。@OUHSD網站管理員:我也想添加(爲了將來的參考),當你想向數組添加內容時,'$ items'將會是'$ items []',但是在這個case,肯定跟fgetcsv一起 – Herbert

0

項目應該是你的情況的陣列。初始化它while循環外

$items = array(); 

然後while循環中

$items[] = array ($parts[2] .$parts[3],); 

應該工作。

也期待在fgetcsv以及建議。

+0

當我嘗試這個並將「fgets」改爲fgetcsv時「我得到幾個數組複選框,每個數組伴隨着一個數組,我認爲每行都有一個CSV,所以也許我是 –

+0

你看過fgetcsv文檔嗎? –

0

你需要一個額外的dimenson你的陣列。

$i=0; 
$items= new array(); // i think you need this for some fun reason 
while (!feof($file_handle)) { 

    $line_of_text = fgets($file_handle); 
    $parts = explode(',', $line_of_text); 

    $items[$i] = array ($parts[2] .$parts[3],); 

} 
+0

它會得到'new array'行的語法錯誤:/ –

0

您正在覆蓋$items陣列。我想提出一個更簡單的方法來

<code><pre><?php 

    // in one step, load the datafile as an array, and loop over each line 
    // then explode the line by comma, and add to $lines array 
    foreach(file('curriculum.csv') as $line) 
      $lines[] = explode(',',$line); 

    // display the structure of the captured data 
    print_r($lines); 

?></pre></code> 

我認爲一旦你理解了捕獲的數據結構在這裏,因爲你在你的輸出循環願意,你可以使用的值。

codeprephp標籤增加了便利