[PHP]Can anyone help me with parsing a csv file?

Bridude

Veteran XV
This is my csv file:

Code:
"Subject","Start Date","Start Time","End Date","End Time","All day event","Reminder on/off","Reminder Date","Reminder Time","Meeting Organizer","Required Attendees","Optional Attendees","Meeting Resources","Billing Information","Categories","Description","Location","Mileage","Priority","Private","Sensitivity","Show time as"
"Basic Class","7/12/2010","12:00:00 AM","7/17/2010","12:00:00 AM","True","False","7/11/2010","6:00:00 AM",,,,,,,"
","01-10",,"Normal","False","Normal","3"
"Adv Class","7/12/2010","12:00:00 AM","7/31/2010","12:00:00 AM","True","False","7/11/2010","6:00:00 AM",,,,,,,"
","01-10",,"Normal","False","Normal","3"
"Basic Class","7/26/2010","12:00:00 AM","7/31/2010","12:00:00 AM","True","True","7/25/2010","6:00:00 AM",,,,,,,"This is a description for 02-10
","02-10",,"Normal","False","Normal","3"
"Basic Class","8/2/2010","12:00:00 AM","8/7/2010","12:00:00 AM","True","True","8/1/2010","6:00:00 AM",,,,,,,"
","03-10",,"Normal","False","Normal","3"
"Adv Class","8/2/2010","12:00:00 AM","8/28/2010","12:00:00 AM","True","True","8/1/2010","6:00:00 AM",,,,,,,"
","02-10",,"Normal","False","Normal","3"
"Med Class","8/16/2010","12:00:00 AM","8/21/2010","12:00:00 AM","True","True","8/15/2010","6:00:00 AM",,,,,,,"
","02-10",,"Normal","False","Normal","3"
"Med Class","7/19/2010","12:00:00 AM","7/24/2010","12:00:00 AM","True","True","7/18/2010","6:00:00 AM",,,,,,,"
","01-10",,"Normal","False","Normal","3"
"Basic Class","8/30/2010","12:00:00 AM","9/4/2010","12:00:00 AM","True","True","8/29/2010","6:00:00 AM",,,,,,,"
","04-10",,"Normal","False","Normal","3"

I only need the Subject, Start Date, End Date, Location, and Description columns.
In short I'd like to make it look like this in php

exprpt.jpg



I'm pretty sure I need a mutli dimensional array but I can't for the life of me get it to work.

I've only got this so far.

PHP:
<?php 
print("<TABLE>\n"); 
$row = 0; 
$handle = fopen("classcal.csv", "r"); 
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) { 
   $num = count($data); 
   for ($c=0; $c <= $row; $c++)
{ 
    
	echo("<TR>"); 
    echo("<TD>".$data[0]." </td>"); 
    echo("<TD>".$data[16]." </td>"); 
    echo("<TD>".$data[1]." </td>"); 
    echo("<TD>".$data[3]." </td>"); 
    echo("<TD>".$data[15]." </td>"); 
    echo("</TR>");  
	
} 
} 
fclose($handle); 
 
?>
 
I want to just overwrite the csv file to update the data on the page or better yet have someone other than me overwrite it.
 
yeah it seems to be pretty difficult to accomplish such a simple task. I think what I need is like example 2 but I'm not smart enough to get it to work.

Code:
<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

PHP: array_multisort - Manual
 
Code:
 <?php
    # Open the File.
    if (($handle = fopen("file.csv", "r")) !== FALSE) {
        # Set the parent multidimensional array key to 0.
        $nn = 0;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            # Count the total keys in the row.
            $c = count($data);
            # Populate the multidimensional array.
            for ($x=0;$x<$c;$x++)
            {
                $csvarray[$nn][$x] = $data[$x];
            }
            $nn++;
        }
        # Close the File.
        fclose($handle);
    }
    # Print the contents of the multidimensional array.
    print_r($csvarray);
?>
 
PHP:
<table>
<?
$handle = fopen("test.csv", "r");  
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    echo "\t<tr>\n";
    echo "\t\t<td>$data[0]</td>\n";
    echo "\t\t<td>$data[16]</td>\n";
    echo "\t\t<td>$data[1]</td>\n";
    echo "\t\t<td>$data[3]</td>\n";
    echo "\t\t<td>" . trim($data[15]) . "</td>\n";
    echo "\t</tr>\n";
}

fclose($handle);  
?>
</table>

Gives me:

Code:
<table>
	<tr>
		<td>Subject</td>
		<td>Location</td>
		<td>Start Date</td>
		<td>End Date</td>
		<td>Description</td>
	</tr>
	<tr>
		<td>Basic Class</td>
		<td>01-10</td>
		<td>7/12/2010</td>
		<td>7/17/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Adv Class</td>
		<td>01-10</td>
		<td>7/12/2010</td>
		<td>7/31/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Basic Class</td>
		<td>02-10</td>
		<td>7/26/2010</td>
		<td>7/31/2010</td>
		<td>This is a description for 02-10</td>
	</tr>
	<tr>
		<td>Basic Class</td>
		<td>03-10</td>
		<td>8/2/2010</td>
		<td>8/7/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Adv Class</td>
		<td>02-10</td>
		<td>8/2/2010</td>
		<td>8/28/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Med Class</td>
		<td>02-10</td>
		<td>8/16/2010</td>
		<td>8/21/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Med Class</td>
		<td>01-10</td>
		<td>7/19/2010</td>
		<td>7/24/2010</td>
		<td></td>
	</tr>
	<tr>
		<td>Basic Class</td>
		<td>04-10</td>
		<td>8/30/2010</td>
		<td>9/4/2010</td>
		<td></td>
	</tr>
	<tr>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
		<td></td>
	</tr>
</table>

Dunno why you're iterating through each column when you only want 4 columns.

Edit: Oh you want to sort based on class type.
 
Last edited:
Code:
 <?php
    # Open the File.
    if (($handle = fopen("file.csv", "r")) !== FALSE) {
        # Set the parent multidimensional array key to 0.
        $nn = 0;
        while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
            # Count the total keys in the row.
            $c = count($data);
            # Populate the multidimensional array.
            for ($x=0;$x<$c;$x++)
            {
                $csvarray[$nn][$x] = $data[$x];
            }
            $nn++;
        }
        # Close the File.
        fclose($handle);
    }
    # Print the contents of the multidimensional array.
    print_r($csvarray);
?>

I've tried that it just spits out the data on the page like this:

Array ( [0] => Array ( [0] => Subject [1] => Start Date [2] => Start Time [3] => End Date [4] => End Time [5] => All day event [6] => Reminder on/off [7] => Reminder Date [8] => Reminder Time [9] => Meeting Organizer [10] => Required Attendees [11] => Optional Attendees [12] => Meeting Resources [13] => Billing Information [14] => Categories [15] => Description [16] => Location [17] => Mileage [18] => Priority [19] => Private [20] => Sensitivity [21] => Show time as ) [1] => Array ( [0] => Basic Class [1] => 7/12/2010 [2] => 12:00:00 AM [3] => 7/17/2010 [4] => 12:00:00 AM [5] => True [6] => False [7] => 7/11/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 01-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [2] => Array ( [0] => Adv Class [1] => 7/12/2010 [2] => 12:00:00 AM [3] => 7/31/2010 [4] => 12:00:00 AM [5] => True [6] => False [7] => 7/11/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 01-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [3] => Array ( [0] => Basic Class [1] => 7/26/2010 [2] => 12:00:00 AM [3] => 7/31/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 7/25/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => This is a description for 02-10 [16] => 02-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [4] => Array ( [0] => Basic Class [1] => 8/2/2010 [2] => 12:00:00 AM [3] => 8/7/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 8/1/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 03-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [5] => Array ( [0] => Adv Class [1] => 8/2/2010 [2] => 12:00:00 AM [3] => 8/28/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 8/1/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 02-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [6] => Array ( [0] => Med Class [1] => 8/16/2010 [2] => 12:00:00 AM [3] => 8/21/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 8/15/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 02-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [7] => Array ( [0] => Med Class [1] => 7/19/2010 [2] => 12:00:00 AM [3] => 7/24/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 7/18/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 01-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) [8] => Array ( [0] => Basic Class [1] => 8/30/2010 [2] => 12:00:00 AM [3] => 9/4/2010 [4] => 12:00:00 AM [5] => True [6] => True [7] => 8/29/2010 [8] => 6:00:00 AM [9] => [10] => [11] => [12] => [13] => [14] => [15] => [16] => 04-10 [17] => [18] => Normal [19] => False [20] => Normal [21] => 3 ) )
 
Dunno why you're iterating through each column when you only want 4 columns.

Edit: Oh you want to sort based on class type.

I get this with that code

\n"; echo "\t\t\n"; echo "\t\t\n"; echo "\t\t\n"; echo "\t\t\n"; echo "\t\t\n"; echo "\t\n"; } fclose($handle); ?>
$data[0] $data[16] $data[1] $data[3] " . trim($data[15]) . "
 
I've tried that it just spits out the data on the page like this:

Code:
   # Print the contents of the multidimensional array.
    print_r($csvarray);


that's because it's printing the multidimensional array. just comment out the print_r. you have to format it yourself but all the data is in $csvarray

run array_multisort on it
 
that's because it's printing the multidimensional array. you have to format it yourself but all the data is in $csvarray

run array_multisort on it

ah well that makes sense. Unfortunately I don't exactly know how to run the array.
 
This will get you 95% of the way there, you just need to make it look pretty.

PHP:
<?php

function SortBySubject($a, $b)
{
	return strcmp($a['Subject'], $b['Subject']);
}

// Open file
$handle = fopen("data.csv", "r");
if (!$handle)
	die("Failed to open file");

// Read Data
$headers = null;
while (($data = fgetcsv($handle)) !== false) {
	// Setup header row (first row)
	if ($headers === null) {
		$headers = $data;
		continue;
	}
	
	// Parse row into key/value pairs
	$row = array();
	for($i = 0; $i < sizeof($headers); $i++)
		$row[$headers[$i]] = $data[$i];
	
	// Add this row to our record list
	$records[] = $row;
}

// Records is now a key/value pair of rows
// Sort them according to Subject name
usort($records, SortBySubject);

// Render our sorted records to the table
$lastRecord = null;
$tableHeaders = array("Subject", "Location", "Start Date", "End Date", "Description");

?>
<table>
	<tr>
		<?php
		// Print each header we are interested in
		foreach($tableHeaders As $header)
			echo "<th>$header</th>\n";
		?>
	</tr>
	<?php
	// Print our every record in the CSV file
	foreach($records As $record) {
		// If we haven't yet printed any subject headers, or our subject has now changed - print a new heading
		if ($lastRecord == null || $lastRecord['Subject'] != $record['Subject']) {
			// Subject has changed, print a header row
			echo sprintf("<tr><td colspan='%d'><b>%s</b></td></tr>\n", sizeof($tableHeaders), $record['Subject']);
		} else {
			// Print this record row
			echo "<tr>\n";
			
			// Loop over our headers again, but print a blank space for our Subject value for the record
			foreach($tableHeaders As $header)
				echo sprintf("<td>%s</td>\n", $header == 'Subject' ? '' : $record[$header]);

			echo "</tr>\n";
		}
		
		// Keep track of the previous record printed
		$lastRecord = $record;
	}
	?>
</table>
 
This will get you 95% of the way there, you just need to make it look pretty.

PHP:
<?php

function SortBySubject($a, $b)
{
	return strcmp($a['Subject'], $b['Subject']);
}

// Open file
$handle = fopen("data.csv", "r");
if (!$handle)
	die("Failed to open file");

// Read Data
$headers = null;
while (($data = fgetcsv($handle)) !== false) {
	// Setup header row (first row)
	if ($headers === null) {
		$headers = $data;
		continue;
	}
	
	// Parse row into key/value pairs
	$row = array();
	for($i = 0; $i < sizeof($headers); $i++)
		$row[$headers[$i]] = $data[$i];
	
	// Add this row to our record list
	$records[] = $row;
}

// Records is now a key/value pair of rows
// Sort them according to Subject name
usort($records, SortBySubject);

// Render our sorted records to the table
$lastRecord = null;
$tableHeaders = array("Subject", "Location", "Start Date", "End Date", "Description");

?>
<table>
	<tr>
		<?php
		// Print each header we are interested in
		foreach($tableHeaders As $header)
			echo "<th>$header</th>\n";
		?>
	</tr>
	<?php
	// Print our every record in the CSV file
	foreach($records As $record) {
		// If we haven't yet printed any subject headers, or our subject has now changed - print a new heading
		if ($lastRecord == null || $lastRecord['Subject'] != $record['Subject']) {
			// Subject has changed, print a header row
			echo sprintf("<tr><td colspan='%d'><b>%s</b></td></tr>\n", sizeof($tableHeaders), $record['Subject']);
		} else {
			// Print this record row
			echo "<tr>\n";
			
			// Loop over our headers again, but print a blank space for our Subject value for the record
			foreach($tableHeaders As $header)
				echo sprintf("<td>%s</td>\n", $header == 'Subject' ? '' : $record[$header]);

			echo "</tr>\n";
		}
		
		// Keep track of the previous record printed
		$lastRecord = $record;
	}
	?>
</table>

dude, I could kiss you right now. You fucking rock.

thanks everyone for the all input it definitely helped me get a better grasp on the stuff
 
Back
Top