开发者

How to use PHP fgetcsv to create an array for each piece of data in csv file?

I'm trying to import data from a csv file to some html code to use it in a graph I have already coded. I'm trying to use PHP and fgetcsv to create arrays for each separate piece of data to use PHP to put it into the html code. I know how to open the csv file and to print it using PHP, and how to print each separate row, but not each piece of data separated by a comma. Is there a way to do this?

If this helps, this is the csv data I am trying to import.

May 10,72,12,60
May 11,86,24,62
May 12,67,32,34
May 13,87,12,75
May 14,112,23,89
May 17,69,21,48
May 18,98,14,84
May 19,115,18,97
May 20,101,13,88
May 21,107,32,75

I hope that makes sense.


I'm trying to get the values into the following html code:

<dd class="pVALUE3"><span><b>VALUE3</b></span></dd>
<dd class="sub pVALUE2" ><span><b>VALUE2</b></span></dd>

so far my PHP code is:

    <?PHP 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
        $date[$c] = $data[0]
        echo "<dd class=\"p" . $data[2] . echo "\"><span><b>" . $data[2] . echo "</b></span></dd>
        <dd class=\"sub p" . $data[3] .  echo "\" ><span><b>" . $data[3] . echo "</b></span></dd>"
     开发者_开发百科   }
                    ?>

But it's returning that there is an unexpected T_ECHO ...


I've managed to solve this using the following code:

 <?PHP
/* Open CSV file */

$handle = fopen("defects.csv", "r");
$c = 0;
/* gets data from csv file */
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
/* stores dates as variable $date */
        $date[$c] = $data[0];
        $c++;
        /* inserts defect data into html code */
        echo "<div class=\"bar\"><dd class=\"p" . $data[2] . "\"><span><b>" . $data[2] . "</b></span></dd></div>";
        echo "<div class=\"subbar\"<dd class=\"sub p" . $data[3] . "\" ><span><b>" . $data[3] . "</b></span></dd></div>";
}

echo "</dl>";
echo "<ul class=\"xAxis\">";
/* X AXIS */
/* inserts date data into html code for x axis */
for ($d=0; $d < $c; $d++) {
    echo "<li>" . $date[$d] . "</li>";
}
?>


I take it you have got this far?

while (($row = fgetcsv($handle, null, ",", '"')) !== false) {
        print_r($row);

After that you can

foreach($row as $field){
   ...

no?

For the import data you supplied you will have to:

preg_match('/^(\w+) (\d+)$/i', $row[0], $bits);
$month = $bits[1];
$row[0] = $bits[2];

To get the month name.

As thetaiko commented, paste your code and maybe there is a bug to squash.


I assume you want every item in the row as a separate piece of data, right? So 'May 10', '72', '12' etc. are all separate?

Using fgetcsv you should be able to do this:

while (($data = fgetcsv('file.csv')) !== FALSE)
{
    foreach ($data as $element)
    {
        echo $element.' ';
    }
    echo '<br />';
}

Each time fgetcsv reads a line (until FALSE is returned denoting the end of file) you can loop through the $data array (providing it's a valid array, may want to check for that...) each $element is now one item of data that was between commas.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜