Page 1 of 2

Combobox

Posted: Thu Nov 18, 2010 5:17 am
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!

Re: Combobox

Posted: Thu Nov 18, 2010 7:43 am
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.

Re: Combobox

Posted: Thu Nov 18, 2010 7:52 am
by ursl40
Could You please give me an example with simple code?

Re: Combobox

Posted: Thu Nov 18, 2010 8:36 am
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>";

Re: Combobox

Posted: Thu Nov 18, 2010 11:35 am
by ursl40
OK, thanks, I'll try that tomorrow and I'll let You know how it worked out!

Re: Combobox

Posted: Fri Nov 19, 2010 12:58 am
by ursl40
OK, I did it! Variable $where was important, so when I correctly done that, it worked!

Thanks again!

Re: Combobox

Posted: Mon Nov 22, 2010 2:52 am
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>";
}
?>

Re: Combobox

Posted: Mon Nov 22, 2010 6:02 am
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>";
}

Re: Combobox

Posted: Mon Nov 22, 2010 6:14 am
by ursl40
OK, that worked, thanks!

Re: Combobox

Posted: Mon Nov 22, 2010 6:47 am
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

Re: Combobox

Posted: Mon Nov 22, 2010 6:58 am
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>;
}

Re: Combobox

Posted: Mon Nov 22, 2010 7:08 am
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...

Re: Combobox

Posted: Tue Nov 23, 2010 7:47 am
by ursl40
Any idea, please? I could maybe create default user...

Re: Combobox

Posted: Tue Nov 23, 2010 8:57 am
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.

Re: Combobox

Posted: Tue Nov 23, 2010 11:55 am
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.