I have a simple drop down box on a web page. In it are all the states in the US. When a user selects a state, I'd like to have a PHP script only return the companies that are in the state the user selected (as HTML).
I've a csv file that reads as follows:
stateId,company,contact,phone,when,addy1
3,abc mining,George,555-555-1234,june 6 2012,"Bronx,NY"
2,xyz mining,Fred,555-555-2345,june 7 2012,"Seattle,WA"
0,my company,Me,555-555-3456,june 8 2012,"Eugene,OR"
3,your company,You,555-555-4567,june 9 2012,"Bronx,NY"
and on and on and on....
Using the above, return via an HTML table, only the data (or lines in the csv) with a stateId = 3.
So... I've seen many ways to use fgetcsv, but how would I accomplish the above problem?
I was thinking you'd grab the selected index value of the drop-down, pass it as a conditional, iterate through each line, outputting only those with a state id of 3...
Ideally, I'd like to output them in a table, but a list or straight text will do.
Being relatively new to PHP (it's been 10+ years since I've even looked at it...) - I'd REALLY appreciate the help.
I know... a database would be easier - but I've complete control over the csv and would rather go this route for the time being.
<?php
echo "<table>\n";
$row = 0;
$handle = fopen("mycsvfile.csv", "r");
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
if ($row == 0) {
// column line
$num = count($data);
echo "<thead>\n<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<th>" . $data[$c] . "</th>";
}
echo "</tr>\n</thead>\n\n<tbody>";
} else {
// non-header lines
$num = count($data);
echo "<tr>";
$row++;
for ($c=0; $c < $num; $c++) {
echo "<td>" . $data[$c] . "</td>";
}
echo "</tr>\n";
}
}
fclose($handle);
echo "</tbody>\n</table>";
?>
I start to lose it after this point... This simply outputs all data - I still need it to show only data for the state the user selected, via a variable, and NOT output lines that are for other states not selected.
Do I use strpos(), explode(), split()... I'm a dotNet programmer with little to no experience in php - and am hoping to learn. This is my first project - so take it easy on me! I don't know and have not used php functions/methods - so I'm not familiar with them. ;)
I've XAMPP up and running, so I'm not completely hopeless.
fgetcsv.