Jump to content

add a default select option with value "empty" this dropdownlist


antonyfal

Recommended Posts

Hi,

I need to call to this text to add a all with value "blank/empty" or "0" to the dropdownlist code below..

it should be inserted int the first position in the dropdownlist and have a value blank and call to this text from the language files:

$searchAll = gettext('_LANGUAGE_SEARCH_ALL',_LANGUAGE_SEARCH_ALL,false,false);

example is:

ratestitle=>0 gettext('_LANGUAGE_SEARCH_ALL',_LANGUAGE_SEARCH_ALL,false,false);

the words it calls is "ALL" when the user selects or leaves the "ALL" in the list the value

remains empty this allows the search to search everything..

basically im stuck how to get it in the dropdown list as the first default position:

can anyone help?

 

here is my code://this is a search function which calls to the field rate_title the text from the select box is matched to the database table and the search pages shows the search option is the search is successful.. I have 5 of these types of searches but cant give a broader search option to my users as the values must be selected to search--- i need the option "ALL" to have a "EMPTY" value or no value....

 

 

if (in_array("ratestitle",$searchOptions)&& $showSearchOptions )

{

if (empty($sch->filter['ratestitle']) )

$selectOption=$output['LANGUAGE_SEARCH_RATESTITLE'];

else

$selectOption=$sch->filter['ratestitle'];

$showButton=true;

$query = "SELECT DISTINCT rate_title FROM #__rates_table ORDER BY rate_title ASC";

$dropDownList ="<select class=\"inputbox\" name=\"ratestitle\">";

$ratesitles =doSelectSql($query);

foreach ($ratesitles as $ratetitle)

{

$selected="";

$rate_title=$ratetitle->rate_title;

if ($ratetitle==".$sch->filter['ratestitle'].")

$selected="selected";

$dropDownList .= "<option ".$selected." value=\"".$rate_title."\">".$rate_title."</option>";

}

$dropDownList.="</select>";

$output['RATESTITLE']=$dropDownList;

}

 

Link to comment
Share on other sites

This may or may not help, but what I do is test the value of 'key' for 0 and I know I have the first element

eg

foreach(arr as $key=>$val) {
   if ($key == 0) {
      // first item do whatever
      // set selected etc
   } else {
      // not first item
   }
}

May be very simplistic but it works.

Link to comment
Share on other sites

thanks ocpaul20.

Im playing with the code now-- but im not getting it right with th code above. Can you please help me get it into the equation above?

 

also note this is a search string of text values not (int)-- the droplist calls for a column in the table, there is no default "0" or "null" value.

i used something like this:

                        $searchAll= gettext(_LANGUAGE_SEARCH_ALL..................................)

$dropDownList ="<select class=\"inputbox\" name=\"ratestitle\">";

$ratesitles =doSelectSql($query);

  /*i added*/  $ratestitles[]=$searchAll;

 

 

 

ADDING THIS GIVE ME THE BLANK BUT AT THE BOTTOM OF THE LIST ITS NOT INCLUDED INTO THE ARRAY.

 

how can i apply your code?

best regards

Tony

Link to comment
Share on other sites

If I understand this correctly, you are making it way too complicated. Why dont you just add an option before you start the loop? That option would have a blank or all or whatever you need it to have and it can be selected or not. does that make sense?

 

The other thing is that you can always have a counter in the loop to count how many times you have performed it. $x++ kind of thing if you need a specific counter.

 

Also, your sql can return a count of how mant rows the query has returned so if you ever need to know the last one for example, you can just count up you x counter until you get to the last row to be processed.

 

Problems are always solved easier if you reduce them to the barest minimum code. I make loads of test programs with little bits of my code and then when it is working I slot it into the proper place in the proper program I want to use it in.

 

It is a bit of a suck-it-and-see approach but it works out in the end.

Link to comment
Share on other sites

;D all i got was the suck-it-and-see approach--

this little buggy code has got me in loops  :'( please can show me how to add the option for the blank into this code? or where to add what?

please ocpaul20.

 

this little fix and im done with 3 months work :D

Link to comment
Share on other sites

So you didn't do much sucking and seeing then?

it seems like you want me to do it for you rather than learn for yourself like everyone else has done. Well, you lose out in the end but you obviously want a quick-fix.

 

Anyway, I always use sprintf for html stuff as it is easier than using slashes

 

$dropDownList = sprintf('<select class="inputbox" name="ratestitle">');
$dropDownList .= sprintf(' <option %s value="something_or_nothing" >--- please select ---</option>', "selected");
$dropDownList .= sprintf(' <option selected value="something_or_nothing" >--- please select ---</option>'); // these both the same
...

 

Link to comment
Share on other sites

So you didn't do much sucking and seeing then?

it seems like you want me to do it for you rather than learn for yourself like everyone else has done. Well, you lose out in the end but you obviously want a quick-fix.

 

Anyway, I always use sprintf for html stuff as it is easier than using slashes

 

$dropDownList = sprintf('<select class="inputbox" name="ratestitle">');
$dropDownList .= sprintf(' <option %s value="something_or_nothing" >--- please select ---</option>', "selected");
$dropDownList .= sprintf(' <option selected value="something_or_nothing" >--- please select ---</option>'); // these both the same
...

Awsome--! I still got to figure out where to place this but its should be a cinch from here, otherwise expect lots of private messages. ;)

Thanks ocpaul20.

regards

Tony

 

Link to comment
Share on other sites

So you didn't do much sucking and seeing then?

it seems like you want me to do it for you rather than learn for yourself like everyone else has done. Well, you lose out in the end but you obviously want a quick-fix.

 

Anyway, I always use sprintf for html stuff as it is easier than using slashes

 

$dropDownList = sprintf('<select class="inputbox" name="ratestitle">');
$dropDownList .= sprintf(' <option %s value="something_or_nothing" >--- please select ---</option>', "selected");
$dropDownList .= sprintf(' <option selected value="something_or_nothing" >--- please select ---</option>'); // these both the same
...

Awsome--! I still got to figure out where to place this but its should be a cinch from here, otherwise expect lots of private messages. ;)

Thanks ocpaul20.

regards

Tony

 

can the key and option be the same?

like so:

foreach( $options as $key=>$option )

 

can it be: foreach( $options as $name=>$name )?

all the examples ive seen bring up a unique id with a table name like: rates_uid, rate_title FROM xxxx

 

in this case i am only bringing up the rate_title--

 

so how do i right the foreach now?  the code you gave me just brings up equal amounts of "--- please select ---" amounts equal to the amount of words in column rate_title the html "URL" shows value=&next search function--

 

I am lost with this code im guessing my select is not correct.. The problem here is the sch->ratestitle[".ratestitle."], has no definite value.

 

So if you can show it to me in my code i can work it backwords :-[

 

PLEASE!

regards

Tony

Link to comment
Share on other sites

foreach( $arr as $key=>$val )

$arr is an array

$key is the index to the array item

$val is the value of the array item

 

Now we are learning programming not solving problems and you are pushing the limits. I am not here to do that - unless you want to pay contractor rates?

 

Link to comment
Share on other sites

foreach( $arr as $key=>$val )

$arr is an array

$key is the index to the array item

$val is the value of the array item

 

Now we are learning programming not solving problems and you are pushing the limits. I am not here to do that - unless you want to pay contractor rates?

 

Hey Paul--

I am trying to "--SUCKIT--AND --SEEING--" Thats why i am here :P

But i must have missed something in this step: Here's a little twist to my Query: I am using Pattemplates--

the droplist i am selecting from is just a column of names without unique a ID-- and is an "$output" not an "echo"

From all the examples on the net there are echo's and id's---

Please show me and explain how you got it? then i can see and learn. Please!

best regards (and thanks for the help this far i can use what you gave me in other solutions.)

Tony

 

Link to comment
Share on other sites

  • 1 month later...

Hi, Im sorry it took so long to post my solution.

Here it is:

 

To add an extra or multiple extra fields to the top of a Query select dropdown list i did the following:

 

 

 

$query = "SELECT DISTINCT rate_title FROM #xxxxx_rates ORDER BY rate_title ASC";

$ratestitles =doSelectSql($query);

      $dropDownList ="<select class=\"inputbox\" name=\"ratestitle\">";

 

/*i added this line*\    $dropDownList .= "<option value=\"\">All</option>";

 

                  foreach ($ratestitles as $ratetitle)

{

$selected="";

$rate_title=$ratetitle->rate_title;

$dropDownList .= "<option ".$selected." value=\"".$rate_title."\">".$rate_title."</option>";

}

$dropDownList.="</select>";

$output['RATESTITLE']=$dropDownList;

}

 

 

I also tested a theory and it worked: you can add multiple of the line like so:

 

                          $dropDownList .= "<option value=\"\">All</option>";

                          $dropDownList .= "<option value=\"I\">I</option>";

                          $dropDownList .= "<option value=\"Sucked\">Sucked</option>";

                          $dropDownList .= "<option value=\"And\">lAnd</option>";

                          $dropDownList .= "<option value=\"Now\">Now</option>";

                          $dropDownList .= "<option value=\"Im\">Im</option>";

                          $dropDownList .= "<option value=\"Sharing\">Sharing</option>";

                          $dropDownList .= "<option value=\"Thanks for the tips\">Thanks for the tips</option>";

 

be sure to set the order of these lines as as you would want them to appear in the dropdownlist above the mysql query select list.

 

Hope this helps you.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.