0

my goal of this is to put the first index (first value of each line in the csv) into a dropdown html select list.

test.csv

mark, blue, tall,
mike, black, short

index.php

<?php
$handle = fopen("csv/food.csv", "r");

while (($data = fgetcsv($handle, 5000, ",")) !== FALSE) {
    echo "<pre>";
    print_r($data);
    echo "<pre>";
}
?>

output

Array
(
    [0] => mark
    [1] => blue
    [2] => tall
    [3] => mike
    [4] => black
    [5] => short
)

output if I remove comma after tall

Array
(
    [0] => mark
    [1] => blue
    [2] => tall
mike
    [3] => black
    [4] => short
)

desired output

Array
(
    [0][0] => mark
    [0][1] => blue
    [0][2] => tall
    [1][0] => mike
    [1][1] => black
    [1][2] => short
)
5
  • thats mean there is no actual new lines in your csv file. try to remove comma after "Tall," in the first line. Commented Jan 2, 2013 at 12:34
  • I tried that, but then it just puts tall mike into [2]. I'll update OP with output from what you suggested. Commented Jan 2, 2013 at 12:35
  • possible duplicate of how to extract data from csv file in php Commented Jan 2, 2013 at 12:37
  • And you downvoted me for it? Gee, thanks buddy. Commented Jan 2, 2013 at 12:40
  • Welp, that thread solved my problem. Commented Jan 2, 2013 at 12:41

1 Answer 1

2

Got it working:

<select name="list" > 
<?php 
$file = fopen("food.csv", "r"); 

while (!feof($file) ) { 
    $lines = fgetcsv($file, 1024);?> 
    <option value="<?php print $lines[0] ?>"> <?php print $lines[0] ?> </option> 
<?php } ?>
</select>
<?php 
fclose($file); 
?>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.