sniesler08 Posted May 4, 2011 Share Posted May 4, 2011 Now I am stumped this is far any help would be awesome This is what I have gotten so far, this is what it does right now is display the first column of the array as a header for each foreach loop then i've got the data displayed under each header in the loop but what i am trying to understand here is i am trying to get under each header it to have 4 columns or a number of my choosing. Heres an example of what i am trying to do. A airlie beachAndergroveAlexandraArmstrong Beach Alligator Creek B BucasiaBlacks BeachBeaconsfieldBakers Creek BalberraBloomsburyBreadalbaneBall Bay Belmunda This is an example of how it is being displayed right now A Airlie Beach Andergrove Alexandra Armstrong Beach Alligator Creek B Bucasia Blacks Beach Beaconsfield Bakers Creek Balberra Bloomsbury Breadalbane Ball Bay Belmunda C Cannonvale Calen Crystal Brook Cremorne Chelona Campwin Beach Cape Hillsborough Conway Heres The Code i have so far. <?php $file = "widget.csv"; @$fp = fopen($file, "r") or die("Could not open file for reading"); $outputArray = array(); while(($shop = fgetcsv($fp, 1000, ",")) !== FALSE) { $outputArray[array_shift($shop)] = array_filter($shop); } print_r($outputArray); echo "\n<br />"; //display echo "<table>"; foreach ($outputArray as $alphabetical=>$value){ echo "<th><b>" . $alphabetical ."</b></th>"; echo "<tr>"; foreach ($value as $key){ echo "<td>" . $key . "</td>"; } echo "</tr>"; } echo "</table>"; echo "\n<br />"; ?> Heres my Csv File which i am using as an array in the code A,Airlie Beach,Andergrove,Alexandra,Armstrong Beach,Alligator Creek,,,,,,,,,,,,,, B,Bucasia,Blacks Beach,Beaconsfield,Bakers Creek,Balberra,Bloomsbury,Breadalbane,Ball Bay,Belmunda,,,,,,,,,, C,Cannonvale,Calen,Crystal Brook,Cremorne,Chelona,Campwin Beach,Cape Hillsborough,Conway,,,,,,,,,,, D,Dows Creek,Dumbleton,Dolphin Heads,,,,,,,,,,,,,,,, E,Eimeo,Eton,Erakala,,,,,,,,,,,,,,,, F,Foulden,Foxdale,Flametree,Farleigh,Freshwater Point,,,,,,,,,,,,,, G,Glen Isla,Glenella,,,,,,,,,,,,,,,,, H,Homebush,Hampden,,,,,,,,,,,,,,,,, I,,,,,,,,,,,,,,,,,,, J,Jubilee Pocket,,,,,,,,,,,,,,,,,, K,Kinchant Dam,Kolijo,Koumala,Kuttabul,,,,,,,,,,,,,,, L,Laguna Quays,,,,,,,,,,,,,,,,,, M,McEwens Beach,Mackay,Mackay Harbour,Mandalay,Marian,Mia Mia,Middlemount,Midge Point,Mirani,Moranbah,Mount Charlton,Mount Jukes,Mount Julian,Mount Marlow,Mount Martin,Mount Ossa,Mount Pelion,Mount Pleasant,Mount Rooper N,Narpi,Nebo,Nindaroo,North Eton,,,,,,,,,,,,,,, O,Oakenden,Ooralea,,,,,,,,,,,,,,,,, P,Palmyra,Paget,Pindi Pindi,Pinevale,Pioneer Valley,Pleystowe,Preston,Proserpine,,,,,,,,,,, Q,,,,,,,,,,,,,,,,,,, R,Racecourse,Richmond,Riordanvale,Rosella,Rural View,,,,,,,,,,,,,, S,St Helens Beach,Sandiford,Sarina,Seaforth,Slade Point,Shoal Point,Shute Harbour,Shutehaven,Strathdickie,Sugarloaf,Sunnyside,,,,,,,, T,Te Kowai,The Leap,,,,,,,,,,,,,,,,, U,,,,,,,,,,,,,,,,,,, V,Victoria Plains,,,,,,,,,,,,,,,,,, W,Wagoora,Walkerston,Woodwark,,,,,,,,,,,,,,,, X,,,,,,,,,,,,,,,,,,, Y,Yalboroo,,,,,,,,,,,,,,,,,, Z,,,,,,,,,,,,,,,,,,, Quote Link to comment https://forums.phpfreaks.com/topic/235515-using-foreach-to-loop-data-in-array-todisplay-it-on-a-table-with-4-or-so-columns/ Share on other sites More sharing options...
vicodin Posted May 4, 2011 Share Posted May 4, 2011 Your gonna want to create a loop which counts to 4 and when it hits 4, put in a </tr><tr><td>.$key.</td> <?php $file = "widget.csv"; @$fp = fopen($file, "r") or die("Could not open file for reading"); $outputArray = array(); $count = 0; while(($shop = fgetcsv($fp, 1000, ",")) !== FALSE) { $outputArray[array_shift($shop)] = array_filter($shop); } print_r($outputArray); echo "\n<br />"; //display echo "<table>"; foreach ($outputArray as $alphabetical=>$value){ echo "<th><b>" . $alphabetical ."</b></th>"; echo "<tr>"; foreach ($value as $key){ if ($count == 4){ echo '</tr><tr><td>'.$key.'</td>'; $count = 0; }Else{ echo "<td>" . $key . "</td>"; } $count++; } echo "</tr>"; $count = 0; } echo "</table>"; echo "\n<br />"; ?> That should do what you need but I haven't tested it. Quote Link to comment https://forums.phpfreaks.com/topic/235515-using-foreach-to-loop-data-in-array-todisplay-it-on-a-table-with-4-or-so-columns/#findComment-1210441 Share on other sites More sharing options...
sniesler08 Posted May 5, 2011 Author Share Posted May 5, 2011 Thank you very much appreciate it works perfectly Quote Link to comment https://forums.phpfreaks.com/topic/235515-using-foreach-to-loop-data-in-array-todisplay-it-on-a-table-with-4-or-so-columns/#findComment-1210722 Share on other sites More sharing options...
vicodin Posted May 5, 2011 Share Posted May 5, 2011 My pleasure Quote Link to comment https://forums.phpfreaks.com/topic/235515-using-foreach-to-loop-data-in-array-todisplay-it-on-a-table-with-4-or-so-columns/#findComment-1210733 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.