Page 1 of 1
combo with default value
Posted: Mon Sep 24, 2007 11:05 am
by bouncer
i have a drop down list where i have different quantities, but my problem is when i have a default value, how can i have a default quantitie selected without losing the other values. ex: defQtd=2, maxQtd=4, selected value ->2 other 1,3,4
Code: Select all
<?php
if ( $maxQtd > 0 ) {
echo "<select name='qtd[".$x."]' style='width: 40;' class='text10v'>";
for ( $i = 1 ; $i <= $maxQtd; $i++ ) {
if ( $arrProd[$z] == $i ) {
echo "<option value='".$i."' selected>".$i."x</option>";
} else {
echo "<option value='".$i."'>".$i."x</option>";
}
}
echo "</select>";
} else {
echo "<input type='hidden' name='qtd[".$x."]' value='1'>";
}
if ( $opcNone == 1 ) {
echo "<option value='0'>None</option>";
}
}
?>
thanks in advance
Posted: Mon Sep 24, 2007 12:15 pm
by feyd
I'm having trouble understanding your problem. Please explain further.
Posted: Mon Sep 24, 2007 2:27 pm
by califdon
What do you mean by "losing the other values"?? Are you talking about how it appears on the screen??
Posted: Tue Sep 25, 2007 8:50 am
by bouncer
i'm saying that, if i have a drop down list with different quantities, 1,2,3,4, and if i have select a default one like 2 i only see the option 2,3,4 the option 1 disappear. if i choose option 3 by default i only have 3 and 4.
i hope you understand my problem
thanks in advance
Posted: Tue Sep 25, 2007 9:26 am
by paulbm
Sorry, I'm still not 100% sure what you're after there.
But if you hide the 'default value' (do you mean currently selected value) how will the user know that that is the current default value?
When I populate a combo box I set the current value to 'selected' so the user can see it and is able to change it accordingly.
One other thing, your snippet... the second if statement, looks like it will output an <option></option> after the <select> was closed by the first if statement. Now I've probably confused you.

Posted: Tue Sep 25, 2007 9:49 am
by bouncer
paulbm wrote:Sorry, I'm still not 100% sure what you're after there.
When I populate a combo box I set the current value to 'selected' so the user can see it and is able to change it accordingly.
exactly, but in this case i'm populating the combo box with this values (1,2,3,4), if i dont choose any default value i will see the first option selected, in this case 1, but if i choose by default option 3, i see the option 3 selected but my option 1 and 2 doesnt appear in the combo only option 3 by default and 4. is the same if i use this in the above code:
Code: Select all
for ( $i = 1 ; $i <= $maxQtd; $i++ ) { // populate the combo with values 1,2,3,4 and see option 1 by default
instead i want to use something like this, but i want to see the default value, for instance option 3, and see option 1,2 and 4
Code: Select all
for ( $i = $defQtd ; $i <= $maxQtd; $i++ ) { // this will populate the combo with values (if the default is 3) 3 and 4
thanks in advance
Posted: Tue Sep 25, 2007 9:53 am
by aceconcepts
You don't seem to have any backslashes in you HTML when you echo!
Posted: Tue Sep 25, 2007 10:03 am
by feyd
~bouncer, you want to see the default value as the first (physical) value in the box? Or do you want to keep the order the same, where the default value is selected?
~aceconcepts, due to the use of varying quotes, escaping isn't required.
Posted: Tue Sep 25, 2007 10:17 am
by aceconcepts
Superb, I didn't know that - evidently

Posted: Tue Sep 25, 2007 10:22 am
by bouncer
feyd wrote:~bouncer, you want to see the default value as the first (physical) value in the box? Or do you want to keep the order the same, where the default value is selected?
exactly

, i'm having problems doing that i only can keep the values > than the default.
thanks in advance
Posted: Tue Sep 25, 2007 10:38 am
by feyd
bouncer wrote:exactly

, i'm having problems doing that i only can keep the values > than the default.
I asked two different questions. Which is "exactly"?
Posted: Tue Sep 25, 2007 10:40 am
by bouncer
sorry, the second one
thanks in advance
Posted: Tue Sep 25, 2007 10:50 am
by feyd
Okay. The loop will work just like you start counting at 1. You will add a comparison of $i with the selected/default value. When they match, output a "selected" option tag instead of a basic option tag.
Posted: Tue Sep 25, 2007 11:00 am
by paulbm
Code: Select all
if ( $maxQtd > 0 )
{
echo "<select name='qtd[".$x."]' style='width: 40;' class='text10v'>";
if ( $opcNone == 1 )
{
echo "<option value='0'>None</option>";
}
for ( $i = 1 ; $i <= $maxQtd; $i++ )
{
if ( $arrProd[$z] == $i ) // is $arrProd[$z] the currently selected value?
{
echo "<option value='".$i."' selected>".$i."x</option>"; // this line should do what you want, selected
}
else
{
echo "<option value='".$i."'>".$i."x</option>";
}
}
echo "</select>";
}
else
{
echo "<input type='hidden' name='qtd[".$x."]' value='1'>";
}
Is this what you mean? Moving the none value inside the <select> and the line that compares the value of $i sets selected if they match.
If it doesn't work, then it looks like your values aren't correct. Temporarily add $arrProd[$z] to all the options so you can check it.
Code: Select all
else
{
echo "<option value='".$i."'>".$i."x (".$arrProd[$z].")</option>";
}
It'll be the same on each <option> but at least you'll see the actual value of $arrProd[$z]. Sometimes these bugs are due to data not being what you expect.
Posted: Tue Sep 25, 2007 11:03 am
by bouncer
thanks feyd, and others, for the help, now is working
regards