|
-
Nov 28th, 2011, 12:55 PM
#1
Thread Starter
Hyperactive Member
array column to drop down box
Hi I have a large array that is generated from a csv file. The data is football results from last year, and my code so far converts the csv into a multi dimensional array
That part works well, and it stores the entry under the correct headers for each entry.
Here is a snippet of one entry of the array printed to the screen.
Array
(
[0] => Array
(
[Div] => E0
[Date] => 13/08/2011
[HomeTeam] => Blackburn
[AwayTeam] => Wolves
....
I want two drop down boxes, one for all the home teams, and one for all the away teams. Im reasonably new to php, and after reading tutorials I think I am close. I can get a drop down box to appear, but Im not populating it correctly. Please can someone have a look.
Thanks:
Code:
<html>
<body>
<?php
function showOptionsDrop($array, $active, $echo=true){
$string = '';
foreach($array as $k => $v){
$s = ($active == $k)? ' selected="selected"' :
'';
$string .= '<option value="'.$k.'"'.$s.'>'.
$v.'</option>'."\n";
}
if($echo) echo $string;
else return $string;
}
//converts csv to array in php:
$filepath = ("E0.csv");
function csvToArr($filepath)
{
$headings = array();
// Array of headings
$filedata = array();
// Data Array
// Get file handle
$handle = fopen($filepath, "r");
// Get field headings
$fileline = fgetcsv($handle, 5000, ",");
foreach( $fileline as $field ) {
$headings[] = $field;
}
// Get data
$line = 0;
while (($fileline = fgetcsv($handle, 5000, ",")) !== FALSE)
{
foreach( $headings as $k => $v )
{
// Store data against field headings
$filedata[$line][$v] = $fileline[$k];
}
$line++;
}
return $filedata;
}
// Show the data
echo "<pre>";
print_r(csvToArr("E0.csv"));
echo "</pre>";
?>
<select name="Teams">
<option value="0">Choose a Team</option>
<?php showOptionsDrop($array, null, true); ?>
</select>
</body>
</html>
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|