Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8580389
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 11, 20262026-06-11T20:49:10+00:00 2026-06-11T20:49:10+00:00

I have a simple drop down box on a web page. In it are

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-11T20:49:11+00:00Added an answer on June 11, 2026 at 8:49 pm

    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);       
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple drop down box on a page with the below code:
When I have simple drop down box <select id=fruits> <option value=apple>Apple</option> <option value=orange>Orange</option> <option
I have a simple interface set up using Tkinter.I have a drop down box,
I have a simple list-type php page, which lists items according to a mysql
View Here: http://174.132.101.73/~ree/header/ I have a drop down box. I am trying to get
I have a question regarding ajax, I have a category drop down box and
I want to have multiple drop box with around three on the page at
I have some divs, each with a radio button and a drop-down box. What
I have a simple dropdown menu and I'd like to have this menu close/hide
I have a simple metro app contains a button, a label and a drop

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.