dqx36753
2011-09-14 20:02
浏览 53
已采纳

将CSV输出到数组

I am using WP Alchemy to create a meta box with multiple check boxes.

The trouble I'm having is, I have a csv list of academic courses that I want to create the check box options from. So I'm taking the csv turning it into an array that the boxes are made from. However with the code I have now, only the LAST line is created in the array.

I'm a self taught FrankinCoder when it comes to php, so bare with me...

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

So out of the few hundred lines of code, all I get is the last line. Here is the actually file I'm working with: http://www.ouhsd.k12.ca.us/educational_services/curriculum/curriculum.csv

Any help or suggestions are greatly appreciated!

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 邀请回答

5条回答 默认 最新

  • duanguanya3052 2011-09-14 20:43
    已采纳

    You should be using the fgetcsv() (docs) function rather than trying to make sense of the CSV data yourself. Also, you should build the $items array (rather than overwriting it) as you loop over the file, adding a new item each time.

    $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;
    }
    

    Bonus points (you'll look cooler, using objects and iterators!)

    $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;
    }
    
    点赞 评论
  • dty3416 2011-09-14 20:05

    In each iteration of the loop you replace $items when you want to add to it, thus it only having the last value.

    Also http://php.net/fgetcsv might be of some use to you.

    点赞 评论
  • dongyun9120 2011-09-14 20:09

    items should be an array in your case. Initialize it outside the while loop

    $items = array();
    

    And then inside the while loop

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

    should work.

    Also look at fgetcsv as well as suggested.

    点赞 评论
  • duanlu6114 2011-09-14 20:11

    You need an extra dimenson to your array.

    $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], );
    
    }
    
    点赞 评论
  • dpp3047 2011-09-14 20:24

    You are overwriting the $items array. I would like to suggest a much simpler method to

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

    I think once you understand the structure of the captured data here, you can use the values as you wish in your output loop.

    code, pre and php tags added for convenience

    点赞 评论

相关推荐 更多相似问题