|
-
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>
-
Nov 28th, 2011, 01:32 PM
#2
Re: array column to drop down box
Have your csvToArr() function return to a variable, and then provide that result to your showOptionsDrop() function:
PHP Code:
<?php /* functions here */ $csvData = csvToArr("E0.csv"); ?> <select name="Teams"> <option value="0">Choose a Team</option> <?php showOptionsDrop($csvData, null, true); ?> </select> </body> </html>
Using $array when you define showOptionsDrop() is okay as you've specified that variable name as an argument; using $array when you're calling showOptionsDrop() won't work unless you've defined that variable in the code before calling.
Also your showOptionsDrop() function will need to change slightly. With the sample array data you've shown, then when you do this operation in the function:
PHP Code:
foreach($array as $k => $v){
$v is an array. So this line won't work in a useful manner:
PHP Code:
$string .= '<option value="'.$k.'"'.$s.'>'.$v.'</option>'."\n";
You'll need to specify a key with $v; possibly:
PHP Code:
$string .= '<option value="'.$k.'"'.$s.'>'.$v['HomeTeam'].'</option>'."\n";
-
Nov 28th, 2011, 01:53 PM
#3
Thread Starter
Hyperactive Member
Re: array column to drop down box
Thanks alot your changes worked perfectly.
I realised the 2d array probably wasnt being passed correctly to the function.
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
|