Jump to content

Using Foreach to loop data in array todisplay it on a table with 4 or so columns


sniesler08

Recommended Posts

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,,,,,,,,,,,,,,,,,,,

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.