Jump to content

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;
} 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

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