Double combo in PHP/Javascript search form, data from MySQL

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
manarak
Forum Newbie
Posts: 2
Joined: Sat Sep 23, 2006 5:39 am

Double combo in PHP/Javascript search form, data from MySQL

Post by manarak »

feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]


Hi 

I try to make a double combobox in php/javascript.
I already have a working version of this that uses PHP+Templates, but I do not want to use Templates.

What I have today:

This Script in <Head> section:
[syntax="html"] <script language="Javascript"> 

var cats = new Array(); 

<!--Begincats--> 
cats["{region_de}"] = "{region_id}"; 
<!--Endcats--> 

function set_subcat(fform,llb1,llb2,ccats) { 
lb1 = document.all[llb1]; 
lb2 = document.all[llb2]; 

index = lb2.selectedIndex; 
if ( index<0 ) index = null; 
if (lb2.options[index] != null) { 
val = lb2.options[index].value; 
} else { 
val = null; 
} 

key=lb1.options[lb1.selectedIndex].value.toString().split('#')[0]; 

for (i=document.forms[fform].elements[llb2].options.length; i >=0 ; i--) { 
document.forms[fform].elements[llb2].options[i]=null; 
} 
var i = 1 ; 
document.forms[fform].elements[llb2].options[i] = new Option("","",false,false); 
for (iter in ccats) { 
tmp = ccats[iter.toString()].split("#")[1]; 
tmp2 = ccats[iter.toString()].split("#")[0]; 
if (tmp==key) { 
if (tmp2==val) { 
document.forms[fform].elements[llb2].options[i] = new Option(iter.toString().split("#")[0],ccats[iter.toString()],true,true); 
document.forms[fform].elements[llb2].options[i].selected = true; 
} 
else{ 
document.forms[fform].elements[llb2].options[i] = new Option(iter.toString().split("#")[0],ccats[iter.toString()],false,false);} 
i++; 
} 
} 
} 

function set_subcat2 () { 
set_subcat("Search","s_entry_country","s_entry_region",cats); 
} 

/*document.Search.s_entry_country.options[0]=null;*/ 
document.Search.s_entry_country.onchange = set_subcat2; 

set_subcat("Search","s_entry_country","s_entry_region",cats); 
</script> 
Then the following is the begin of the searchform, with the two selects of the double combobox:[/syntax]

Code: Select all

<form method="GET" action="<?= $sActionFileName ?>" name="Search">
    <input type="hidden" name="FormName" value="Search"><input type="hidden" name="FormAction" value="search">
    <table class="FormTABLE">
     <tr>
      <td class="FormHeaderTD" colspan="13"><a name="Search"><font class="FormHeaderFONT"><?=$sFormTitle?></font></a></td>
     </tr>
     <tr>
      <td class="FieldCaptionTD"><font class="FieldCaptionFONT">Land</font></td>
      <td class="DataTD"><select size="1" name="s_entry_country">
<?
    echo "<option value=\"\">" . $ss_entry_countryDisplayValue . "</option>";
    $lookup_s_entry_country = db_fill_array("select country_id, country_de from ts_countries order by 2");

    if(is_array($lookup_s_entry_country))
    {
      reset($lookup_s_entry_country);
      while(list($key, $value) = each($lookup_s_entry_country))
      {
        if($key == $flds_entry_country)
          $option="<option SELECTED value=\"$key\">$value";
        else 
          $option="<option value=\"$key\">$value";
        echo $option;
      }
    }
    
?></select></td>
     </tr>
     <tr>
      <td class="FieldCaptionTD"><font class="FieldCaptionFONT">Region</font></td>
      <td class="DataTD"><select name="s_entry_region">
<?
    echo "<option value=\"\">" . $ss_entry_regionDisplayValue . "</option>";
    $lookup_s_entry_region = db_fill_array("select region_id, region_de from ts_regions order by 2");

    if(is_array($lookup_s_entry_region))
    {
      reset($lookup_s_entry_region);
      while(list($key, $value) = each($lookup_s_entry_region))
      {
        if($key == $flds_entry_region)
          $option="<option SELECTED value=\"$key\">$value";
        else 
          $option="<option value=\"$key\">$value";
        echo $option;
      }
    }
    
?></select></td>
Now this still doesn't work.

In the templated version, there is this function, which I suppose to be populating the second listbox:

Code: Select all

$db->query("select region_id,country_id,region_de from ts_regions order by region_de");

   $tpl->set_var("region_id","");
   $tpl->set_var("region_de","Alle");
   $tpl->parse("cats",true);

while ($db->next_record())  {
   $tpl->set_var("region_id",$db->f("region_id")."#".$db->f("country_id"));
   $tpl->set_var("region_de",$db->f("region_de")."#".$db->f("region_id"));
   $tpl->parse("cats",true);
}
I suppose this last bit is reponsible for populating the second listbox through "cats", and all I need to do is integrate this part somewhere in the form?

Could someone help me to do that please?


feyd | Please use

Code: Select all

,

Code: Select all

and [syntax="..."] tags where appropriate when posting code. Your post has been edited to reflect how we'd like it posted. Please read:  [url=http://forums.devnetwork.net/viewtopic.php?t=21171]Posting Code in the Forums[/url] to learn how to do it too.[/color]
manarak
Forum Newbie
Posts: 2
Joined: Sat Sep 23, 2006 5:39 am

Post by manarak »

Solved

I just needed to replace the line

Code: Select all

cats["{region_de}"] = "{region_id}";
by some PHP

Code: Select all

<?
$cats_hack = db_fill_array("select concat(region_de,'#',region_id), concat(region_id,'#',country_id) from ts_regions order by region_de");
echo 'cats["Alle"] = "";';
    if(is_array($cats_hack))
    {
      reset($cats_hack);
      while(list($key, $value) = each($cats_hack))
      {
echo 'cats["'.$key.'"] = "'.$value.'";';
      }
    }
?>
Post Reply