1

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.

7
  • 2
    Do you have a question? We're not going to write the code for you. It sounds like you have enough of an approach formulated to start writing code, so why don't you do that? Commented Aug 2, 2012 at 20:43
  • Post the code that you've attempted so far. Also, I see two stateIds of "3," is that intended? Commented Aug 2, 2012 at 20:44
  • that csv data should go in to a db. it will make this a lot easier and faster. Commented Aug 2, 2012 at 20:44
  • Try fgetcsv. Commented Aug 2, 2012 at 20:46
  • Explosion Pills - Yes, I only want those two "rows" of data returned as HTML. Commented Aug 2, 2012 at 20:47

3 Answers 3

2

here's what I ended up doing...

    <?php
   $selectedSt = $_GET["states"];// get method - should clean the data...
$row = 0;
$handle = fopen("mycsv.csv", "r");//open file
while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {
    if ($row == 0) {//skip first row (headers)
                $row++;} 
                else {
        $stAbbrev = $data[0];
        if ($stAbbrev == $selectedSt){//show only selected state
            $num = count($data);
            $row++;
            for ($c=0; $c < $num; $c++) {
                if ($c==0){}
                else{
                    if ($data[$c]==""){}//if empty line
                    else{echo $data[$c]."<br />";}}}//write array item
                    echo "<br />";}}}//end it all with some white space
    fclose($handle);       
?>
Sign up to request clarification or add additional context in comments.

Comments

1

I'm going to assume your data is in an array. Exercise of how to do that is left to the reader.

$state_data = array( 
  array( 
    'id' => '3',
    'company' => 'abc mining',
    'contact' => 'George',
    'phone' => '555-555-1234',
    'when' => 'june 6 2012',
    'addy' => 'Bronx,NY' ),
  array(
    'id' => '2',
    'company' => 'xyz mining',
    'contact' => 'Fred',
    'phone' => '555-555-2345',
    'when' => 'june 7 2012',
    'addy' => 'Seattle,WA' ),

 ...

);

Then I just need to filter out all sub-arrays that have ID of 3.

function filter_out( $var ) {
  return ('3' == $var['id']);
}

$my_states = array_filter( $state_data, "filter_out" );

Finally, output:

<table summary="state data">
<tr>
  <th>ID</th>
  <th>Company</th>
  <th>Contact</th>
  <th>Phone</th>
  <th>When</th>
  <th>Addy</th>
</tr>

<?php
foreach( $my_states as $my_state ) {
?>
<tr>
  <td><?php echo( $my_state['id'] ); ?></td>
  <td><?php echo( $my_state['company'] ); ?></td>
  <td><?php echo( $my_state['contact'] ); ?></td>
  <td><?php echo( $my_state['phone'] ); ?></td>
  <td><?php echo( $my_state['when'] ); ?></td>
  <td><?php echo( $my_state['addy'] ); ?></td>
</tr>
<?php } ?>

</table>

2 Comments

gonna plug away at this, Bryan. Thank you for the post - I'll let you know in a bit...
Thanks, Brian. Got sidetracked on this project... and ended up finishing it today. I ended up using your idea - and modified it to fit the appplication.
0

You need a loop to read the file by row as described here. In the loop each row will be returned as an array with the field values. Test for the stateId the user has selected and, if matched, store the array in another array. As for the output, loop again, this time through the result array, wrapping the html markup around each element as you see fit and concatenate that markup into a string. Send this string to the client.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.