HomeWatched TopicsRegisterSearchDirectory
FAQMemberlistUsergroupsLog inStoresItemsBank

Reply to topic Page 1 of 1
PHP script to select state
Message  

Reply with quote
Post PHP script to select state 
I have a database table called contacts with a table in it called addresses. The fields in the addresses table are firstname, lastname, address, city, state, zip, areacode and phonenumber. (The state field is a 2 character text field.) I want to ask the user for a state and then I want to display all the records for that state only. Can anyone help me?

Thanks,

Andrew

View user's profile Send private message Send e-mail

Reply with quote
Post PHP script to select state 
The script belowe show menu with states, from which user can select the state

<?php
//connection script

function db_cnn()
{
    
    if (@ !$cnndb = mysql_connect("hostname", "username", "password"))//name of your host, your user name and password
    {
        return false;
    }
    $db = mysql_select_db("contacts");//your database name
    if (!$db)
        return false;
    return $db;
}

function prekinivezusabazom($db)
{
    mysql_close($db);
}

function states() // make menu with values from state field
{
    $dba = db_cnn();
    if ($dba)
    {
        echo '<select name="state_select">';
        $query = "select distinct state from addresses order by state";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 0)
        {
            echo '<option selected value="">Error - empty table!</option>';
            echo '</select>';
            return false;
        }
        else
        {
            $num_rows = mysql_num_rows($result);
            for ($i = 0; $i < $num_rows; $i++)
            {
                $row = mysql_fetch_assoc($result);
                $state_in = stripslashes($row['state']);
                echo '<option value="'.$state_in.'">'.$state_in.'</option>';
            }
        }
        echo '</select>';
        mysql_free_result($rezultat);
        prekinivezusabazom($dba);
        return true;
    }
    else
    {
        echo 'Error connect database!';
    }
}

// show form with menu
echo '<form action="all_for_state.php" method="post">';
if (states())
{
    echo '<input type="submit" name="state" value="Show all for selected state" />';
}
echo '</form>';

?>


Second script show all rows for selected state

<?php
    require_once('dbcnn.php');
    
    $state = $_POST['state_select'];
    // name of select tag in dbcnn.php, in states() function which get the value of
    // selected state from menu
    
    // display all records for state selected from menu
    $dba = db_cnn();
    if ($dba)
    {
        $query = "select * from addresses where state = '$state'";
        $result = mysql_query($query);
        if (mysql_num_rows($result) == 0)
        {
          echo '<h3>Error!';
          exit;
       }
       else
       {
          $num_rows = mysql_num_rows($result);
          for ($i = 0; $i < $num_rows; $i++)
          {
             $row = mysql_fetch_assoc($result);
                echo stripslashes($row['firstname']);
                echo stripslashes($row['lastname']);
                echo stripslashes($row['address']);
                echo stripslashes($row['city']);
                echo stripslashes($row['state']);
                echo stripslashes($row['zip']);
                echo stripslashes($row['areacode']);
                echo stripslashes($row['phonenumber']).'<br />';
          }
       }
    }  
    else
    {
        echo 'Error connect database!';
    }

?>

I hope that it will help you to solw your problem and make your own scripts.


_________________
"The only problem to drink water from a cactus is ... to find a tap."
View user's profile Send private message
Display posts from previous:
Reply to topic Page 1 of 1
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum
  




FireFox! The PHP Forum Loans and Credit
Panama Web Design for Hire Web Hosting
Web Hosting and Dedicated Servers Advertise Here $10 a Month Designer Children
Insurance HGH Domain name registration
Affordable web-hosting Dedicated Server Management

Web Design by PlatinumShore.com & Web Hosting by TradeWebHosting.com