Get Selected Value from Database

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Get Selected Value from Database

Post by drewrockshard »

Need some help again. It's been awhile...

I have a mySQL record of customers. And a form with a drop down. If there is no data in the database for this drop down (assigned customer), then I want it to let the user select a person to be assigned to that company. This part of the code works. However, if the data already exists in the database, I want to make the drop down already select that person as the assigned customer, however, I also want there to be an option to have that other person be selected at any given time. I also want this code to be as portable as possible, in regards to the fact that if I add another person to the database, then the drop down will populate with the new person as an option selection to assign a company to it.

So here's the code I have so far:

Code: Select all

function getAssignedCompany() {
        $custID = $_POST['customerID'];
        $query  = "SELECT AssignedCompanyID FROM customers WHERE CustomerID='$custID'";
        $result = mysql_query($query);
        $row = mysql_fetch_array( $result );
 
echo $row['AssignedCompanyID'];
        if (!$row['AssignedCompanyID'] || $row['AssignedCompanyID'] == NULL) { ?>
                <option>No Data Found, please select a company.</option>
                <option>Person One</option>
                <option>Person Two</option>
<?php
        }
        else { ?>
                        <option>Person One</option>
                        <option>Person Two</option>
        <?php } ?>
<?php }
The actual form:

Code: Select all

Assigned Company: <select>
                <?php
                        echo getAssignedCompany();
                ?>
                </select>
Let me know if additional information is required.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Re: Get Selected Value from Database

Post by califdon »

If I understand your objective, what you want to do is always have the complete list of possible assignees in the drop down list, but set "selected" on the one that is currently in the database, or a blank entry if the field is Null. This will require a little more logic in your function loop, and I'd have to give more thought to how to pick up a new name, and right now I don't have time to work on it. Sorry.
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Re: Get Selected Value from Database

Post by drewrockshard »

That is absolutely what I want. Well, if anyone has any time to help out - much help is appriciated.

Thanks for your time ...
drewrockshard
Forum Commoner
Posts: 37
Joined: Sat May 29, 2004 6:07 pm
Location: Dallas, Texas
Contact:

Re: Get Selected Value from Database

Post by drewrockshard »

*bump*

Anyone else with any "logic" or ideas that could point me into the right direction?
Post Reply