views:

416

answers:

6

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.

A: 

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.

Question Mark
A: 

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.

jlindenbaum
for fgetcsv - "handle - A valid file pointer to a file successfully opened by fopen(), popen(), or fsockopen()."
Question Mark
A: 

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

Olivia
@Olivia: you can edit your original question to add information like this
nickf
lol i realised that afterwards.
Olivia
A: 

I have a very closely related question -- with a slightly different application.

I'm importing a series of files. Each file has four columns of data and a header at the top of each column. I only care about the data beneath the header in the second column. My goal is to run a function on each piece of data being pulled from the second column.

Like Olivia, I know how to open the file and print the data. But I need help isolating the data from that second column and then running my function on it over and over again until I get to the end of the file.

Here's an example of the data I'm importing:

COMPANY,URL,CITY,STATE
Acme,http://www.acme.com/,City of Industry,CA
Massive Dynamic,http://www.massivedynamic.com/,Boston,MA
The Daily Planet,http://www.dailyplanet.com/,New York,NY
jaboi
+2  A: 

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>";
}
?>
Olivia
A: 

Hi,

we use for our user management, which contains foreing key relations to group and company, the www.dbTube.org tool. It is a PHP script (big) and a ajax designer for the import definition file. It is working well and no coding required - perfect for me :-)

greetings

Mark

=======================

int getRandomNumber()
{
  // chosen by fair dice roll
  // guaranteed to be random
  return 4;
}
Tim Burton