jookie7705 Posted October 27, 2008 Share Posted October 27, 2008 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!! Quote Link to comment https://forums.phpfreaks.com/topic/130331-solved-csv-file-trying-to-make-new-table-rows/ Share on other sites More sharing options...
Alt_F4 Posted October 27, 2008 Share Posted October 27, 2008 Hi, are the states always listed in alphabetical order? Quote Link to comment https://forums.phpfreaks.com/topic/130331-solved-csv-file-trying-to-make-new-table-rows/#findComment-676012 Share on other sites More sharing options...
jookie7705 Posted October 27, 2008 Author Share Posted October 27, 2008 Yes, the CSV will always be in alphabetical order... Quote Link to comment https://forums.phpfreaks.com/topic/130331-solved-csv-file-trying-to-make-new-table-rows/#findComment-676018 Share on other sites More sharing options...
Alt_F4 Posted October 27, 2008 Share Posted October 27, 2008 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; } Quote Link to comment https://forums.phpfreaks.com/topic/130331-solved-csv-file-trying-to-make-new-table-rows/#findComment-676025 Share on other sites More sharing options...
jookie7705 Posted October 29, 2008 Author Share Posted October 29, 2008 That did the trick! I thought it'd be something simple. A new set of eyes definitely helps. Thanks!!! Quote Link to comment https://forums.phpfreaks.com/topic/130331-solved-csv-file-trying-to-make-new-table-rows/#findComment-677452 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.