Combobox
Moderator: General Moderators
Combobox
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!
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
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
Could You please give me an example with simple code?
Re: Combobox
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
OK, thanks, I'll try that tomorrow and I'll let You know how it worked out!
Re: Combobox
OK, I did it! Variable $where was important, so when I correctly done that, it worked!
Thanks again!
Thanks again!
Re: Combobox
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>";
}
?>
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
"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
OK, that worked, thanks!
Re: Combobox
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
How can I solve that problem? Thanks
Re: Combobox
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
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
Any idea, please? I could maybe create default user...
Re: Combobox
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.ursl40 wrote:Any idea, please? I could maybe create default user...
Re: Combobox
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.