Page 1 of 1

Get Selected Value from Database

Posted: Wed Aug 05, 2009 5:12 pm
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.

Re: Get Selected Value from Database

Posted: Wed Aug 05, 2009 7:30 pm
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.

Re: Get Selected Value from Database

Posted: Wed Aug 05, 2009 8:28 pm
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 ...

Re: Get Selected Value from Database

Posted: Thu Aug 06, 2009 7:01 am
by drewrockshard
*bump*

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