Combobox

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

ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Combobox

Post by ursl40 »

Hy!

I have php pages, three of them are for insertting, displaying and editting cars (table cars in DB - phpmyadmin mysql).

So, when I try to edit selected entry of a car (button edit), I also have a combobox for choosing which user (table users) belongs to that particular car (tables cars and users are connected through foreign key - cars.user_id=users.id - insert works normally). But when I click edit, I can't get the right user to be visible in the combobox as default value. So, by default the first value of combobox is selected, and if I don't edit this properly (select the right user again), the right user is replaced by that default value (other user).

How can I do that? Please help!Thanks!
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Combobox

Post by Celauran »

Are you selecting users.id when you get the info from cars? Store it in a variable then, when you're looping users to populate your combobox, you can compare the current user id with the one you had saved previously. If they match, you can add selected="selected" to that row and it will be selected by default.
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

Could You please give me an example with simple code?
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Combobox

Post by Celauran »

Actually, you won't even need users.id. You've already got the user id from cars.user_id.

Code: Select all

$sql = "SELECT * FROM cars WHERE $where";
$res = mysql_query($sql);

while ($row = mysql_fetch_array($res)
{
    ...
    ...
    $owner = $row['user_id'];
    ...
}

$sql = "SELECT * FROM users";
$res = mysql_query($sql);

echo "<select name=\"whatever\">";
while ($row = mysql_fetch_array($res))
{
    if ($row['id'] == $owner)
    {
        echo "<option value=\"{$row['id']}\" selected=\"selected\">{$row['something']}</option>";
    }
    else
    {
        echo "<option value=\"{$row['id']}\">{$row['something']}</option>";
    }
}
echo "</select>";
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

OK, thanks, I'll try that tomorrow and I'll let You know how it worked out!
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

OK, I did it! Variable $where was important, so when I correctly done that, it worked!

Thanks again!
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

Now I'm wondering, how can I get the first value of combobox to be the text, for example Choose user:, when inserting new computer (I can select user from combobox for that computer)?

This is my code for combobox:
<select name='id' id="combo" onchange="CBtoTB()">

<?php
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];

echo "<option value=\"{$id}\"";

if ( $id == $line7 ) {
echo " selected=\"selected\"";
}

echo ">$name $surname</option>";
}
?>
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Combobox

Post by Celauran »

"Choose User", I'm imagining, is going to be the default text at the beginning of the list? If so, manually create the option item for it before iterating through the database results. Also, if you want it to be selected by default, either mark it as selected, or have none of them marked as selected.

Code: Select all

<select name='id' id="combo" onchange="CBtoTB()">
<option value="">Choose User</option>
<?php
while ($row = mysql_fetch_array($query)) {
$id = $row['id'];
$name = $row['name'];
$surname = $row['surname'];

echo "<option value=\"{$id}\">$name $surname</option>";
}
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

OK, that worked, thanks!
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

I have another problem now, so, when I don't select user from combobox (default value is selected - Choose user:) and insert just car name and type (car can have no user at the beginning), it throws me an error, because foreign key isn't OK - I understand that, because users.id must be equal to cars.user_id (foreign key) and if I don't select user, exactly that happens.

How can I solve that problem? Thanks
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Combobox

Post by Celauran »

Really depends on how things are meant to behave. I assumed that user would have to be populated. If you've got a default user you insert when no user is specified, you could just check the value of user on POST and adjust accordingly.

Code: Select all

if (empty($_POST['user']))
{
    $_POST['user'] = <default value>;
}
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

When I insert car, I can choose to which user that car belongs (from combobox), users are already stored in DB, but it isn't necessary that a car have at the beginnig a user sleceted - it's a problem then with foreign key...
ursl40
Forum Commoner
Posts: 37
Joined: Thu Nov 18, 2010 5:01 am

Re: Combobox

Post by ursl40 »

Any idea, please? I could maybe create default user...
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Combobox

Post by Celauran »

ursl40 wrote:Any idea, please? I could maybe create default user...
If the DB insists each car have a user associated with it, then maybe a default user will solve your problem. It's worth a try.
User avatar
McInfo
DevNet Resident
Posts: 1532
Joined: Wed Apr 01, 2009 1:31 pm

Re: Combobox

Post by McInfo »

Side note: A <select> tag does not create a combo box, just a list. A "combo box" is so named because it is a combination of a list and a text box.
Post Reply