Jump to content

[SOLVED] CSV File: Trying to make new TABLE rows


jookie7705

Recommended Posts

Ok, so I can't seem to figure this out.  I have a CSV file that contains a list of states with cities.  There's multiple instances of each state:

 

Illinois, Chicago

Illinois, Springfield

Illinois, Kanakakee

Missouri, Columbia

Missouri, Kansas City

Missouri, St. Louis

 

I'm using the CSV to an HTML TABLE which  I've done successfully.  What I'm having difficulty in figuring out is how to insert a TABLE ROW with each new state.  For example:

 

<table>

<tr>

<th>State</th><th>City</th>

</tr>

<tr><td>Illinois</td><td>Chicago</td></tr>

<tr><td>Illinois</td><td>Springfield</td></tr>

<tr><td>Illinois</td><td>Kanakakee</td></tr>

<tr>

<th>State</th><th>City</th>

</tr>

<tr><td>Missouri</td><td>Columbia</td></tr>

<tr><td>Missouri</td><td>Kansas City</td></tr>

<tr><td>Missouri</td><td>St. Louis</td></tr>

</table>

 

There's only three of each in the above example, but there could be 35 of one, 5 of another... any number of combinations.  I just need to insert the new TABLE ROW each time the state is different.

 

Any ideas?  Thanks!!

 

OK, then assuming that you are using a loop of some sort to iterate through the rows of the csv and output them, i would do something like this:

 

declare a variable before the loop eg.

 

$newState = '';

 

then put the below code where you want the table row to appear

 

if($stateFromCSV != $newState) {
	echo '<tr><th>'.$stateFromCSV.'</th><th>'.$cityFromCSV.'</th></tr>';
	$newState =  $stateFromCSV;
} 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.