combo with default value

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

Post Reply
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

combo with default value

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

I'm having trouble understanding your problem. Please explain further.
User avatar
califdon
Jack of Zircons
Posts: 4484
Joined: Thu Nov 09, 2006 8:30 pm
Location: California, USA

Post by califdon »

What do you mean by "losing the other values"?? Are you talking about how it appears on the screen??
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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 :roll:

thanks in advance
paulbm
Forum Newbie
Posts: 5
Joined: Tue Sep 25, 2007 8:13 am

Post 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. :)
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

You don't seem to have any backslashes in you HTML when you echo!
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
User avatar
aceconcepts
DevNet Resident
Posts: 1424
Joined: Mon Feb 06, 2006 11:26 am
Location: London

Post by aceconcepts »

Superb, I didn't know that - evidently :D
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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"?
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

sorry, the second one :roll:

thanks in advance
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
paulbm
Forum Newbie
Posts: 5
Joined: Tue Sep 25, 2007 8:13 am

Post 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.
bouncer
Forum Contributor
Posts: 162
Joined: Wed Feb 28, 2007 10:31 am

Post by bouncer »

thanks feyd, and others, for the help, now is working :D

regards
Post Reply