Advertisement:

Author Topic: Cities dropdown based on Region Select  (Read 34447 times)

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #15 on: April 03, 2013, 12:58:21 am »
Hi,
you can try this form together with the javascript i will post to see if is working.
 I HAVE ONLY CATEGORY, NOT SUB CATEGORY, so may be you need to change the category part.
Also i don't have the text box where you put what you are looking for, but i think that's easy to add.

REMEMBER TO KEEP A ORIGINAL COPY OF INC.SEARCH.PHP BEFORE YOU MAKE ANY CHANGE.

FORM CODE
Code: [Select]
<?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
<?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >

    <input type="hidden" name="page" value="search" />
    <fieldset class="main">
   
   
                                      <select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>
                                   
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
   <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?>
     
  <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="Select a City">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>

<button type="submit"  onmouseover="this.style.cursor='pointer';" class="" style="height:26px;" >Search</button>
                   
                                   
    </fieldset>
</form>

JAVASCRIPT CODE
Code: [Select]
<script>
$(document).ready(function() {
   
        $("#sRegion").live("change",function(){
            var pk_c_code = $(this).val();
            <?php if($path=="admin") { ?>
                var url = '<?php echo osc_admin_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php } else { ?>
                var url = '<?php echo osc_base_url(true)."?page=ajax&action=cities&regionId="?>' + pk_c_code;
            <?php }; ?>

            var result = '';

            if(pk_c_code != '') {
               
                $("#sCity").attr('disabled',false);
                $.ajax({
                    type: "GET",
                    url: url,
                    dataType: 'json',
                    success: function(data){
                        var length = data.length;
                        if(length > 0) {
                            result += '<option selected value=""><?php _e("Select a city..."); ?></option>';
                            for(key in data) {
                                result += '<option value="' + data[key].pk_i_id + '">' + data[key].s_name + '</option>';
                            }

                            $("#city").before('<select name="sCity" id="sCity" ></select>');
                            $("#city").remove();
                        } else {
                            result += '<option value=""><?php _e('No results'?></option>';
                            $("#sCity").before('<input type="text" name="city" id="city" />');
                            $("#sCity").remove();
                        }
                        $("#sCity").html(result);
                    }
                 });
             } else {
                $("#sCity").attr('disabled',true);
             }
        });

        if( $("#sRegion").attr('value') == "")  {
            $("#sCity").attr('disabled',true);
        }

       
});
 </script>


The script select 2 is on github you can find some examples here

http://ivaynberg.github.com/select2/

Let me know if is working for you
« Last Edit: May 16, 2013, 05:22:49 am by cartagena68 »

geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #16 on: April 03, 2013, 01:16:08 am »
Thank you very much for sharing this code, i will report back with the results  8)

geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #17 on: April 03, 2013, 01:45:40 am »
Notice: Use of undefined constant sRegion - assumed 'sRegion' in /home/www/hosting/domain.com/www/oc-content/themes/modern/inc.search.php on line 129 Notice: Undefined index: sRegion in /home/www/hosting/domain.com/www/oc-content/themes/modern/inc.search.php on line 129

I have:
Line 129:<?php $regionId = $_GET[sRegion]; ?>
Line 130:<?php $conn = getConnection(); $aCities = $conn->osc_dbFetchResults("SELECT * FROM %s_t_city WHERE fk_i_region_id = '$regionId'", DB_TABLE_PREFIX); ?>

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #18 on: April 03, 2013, 01:53:41 am »
Hi,
 is been long time since i chanced this code, may be i have something different.
Try to change

<?php $regionId = $_GET[sRegion]; ?>

with

<?php $regionId = $region['pk_i_id']; ?>


geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #19 on: April 03, 2013, 02:42:14 am »
OMG Its working  :D :D :D :D :D :D :D :D

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #20 on: April 03, 2013, 02:43:36 am »
your site is on line to check it?

geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #21 on: April 03, 2013, 02:53:23 am »
I will try to publish it on Friday. I will pm you the link whenever i turn off the maintenance mode

Thanks again for the help... many ppl in the forum are asking for the same thing  :)

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #22 on: April 03, 2013, 02:56:07 am »
HI,
if is working well, you are free to post the solution to other people looking for this.

wakaka

  • Newbie
  • *
  • Posts: 9
Re: Cities dropdown based on Region Select
« Reply #23 on: April 04, 2013, 08:55:07 am »
Thanks for your code, i use it in header, it work in first search. After first search city not populate. You have to search other state then search wanted state again to make city appear. How to fix it? thanks.

geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #24 on: April 04, 2013, 05:58:40 pm »
Thanks for your code, i use it in header, it work in first search. After first search city not populate. You have to search other state then search wanted state again to make city appear. How to fix it? thanks.

Yes you are right... the value is not passed in the search page. I have also used the code in the search sidebar but only the region field is passed

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #25 on: April 04, 2013, 09:51:37 pm »
Hi,
i try to fix that with no luck.
You can try with select2 script, i attach a zip with select2 folder and inc.search.php , this is working for me.
Just download the zip and unzip it.
Load the select2 folder in your main directory.
Load the inc.search.php in your theme folder (always keep a copy of your original inc.search.php, just in case)

This is working for me, i'm using osclass 3.0.2, i don't know if they change something in version 3.1.

Hope this wil work for you also.

lemike

  • Newbie
  • *
  • Posts: 11
Re: Cities dropdown based on Region Select
« Reply #26 on: April 08, 2013, 01:28:49 am »
@geo78

It's getting closer and closer!! Have you find a way to pass the city to the search results page? Did you get any luck with Select2? Same problem on my side, even with Select2 uploaded...

Thanks!

geo78

  • Full Member
  • ***
  • Posts: 100
Re: Cities dropdown based on Region Select
« Reply #27 on: April 08, 2013, 05:13:59 am »
@geo78

It's getting closer and closer!! Have you find a way to pass the city to the search results page? Did you get any luck with Select2? Same problem on my side, even with Select2 uploaded...

Thanks!

No luck so far.. i also abandoned select2 due to the fact that the category dropdown does not show subcategories

wakaka

  • Newbie
  • *
  • Posts: 9
Re: Cities dropdown based on Region Select
« Reply #28 on: April 08, 2013, 06:52:27 am »
Find the solution  :)

the code:

Code: [Select]
<?php  $conn getConnection(); $aStates $conn->osc_dbFetchResults("SELECT * FROM %st_city "DB_TABLE_PREFIX); ?>
      <?php if (count($aStates) >= ) {?>
      <?php  foreach($aStates as $state) { ?>
<?php $paese $state['fk_c_country_code']; ?> 
     <?php ?><?php ?>
<form name="modulo" action="<?php echo osc_base_url(true) ; ?>" method="get" class="search" >

    <input type="hidden" name="page" value="search" />
    <fieldset class="main">
   
   
                                      <select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>
                                   
                               
        <?php $aRegions Region::newInstance()->getByCountry($paese); ?>
<?php  if(count($aRegions) >= ) { ?>
    <select id="sRegion" name="sRegion"  style="width: 280px;" class="">
      <option value="">Select a State</option>
      <?php  foreach($aRegions as $region) { ?>
      <option id="idregioni"  value="<?php echo $region['pk_i_id']; ?>"<?php if(Params::getParam('sRegion') == $region['pk_i_id']) { ?>selected<?php ?>><?php echo $region['s_name'] ; ?></option>
<?php ?>
  </select><?php ?>
     
   <?php $regionId $_GET[sRegion]; ?>
     <?php  $conn getConnection(); $aCities $conn->osc_dbFetchResults("SELECT * FROM %st_city WHERE fk_i_region_id = '$regionId'"DB_TABLE_PREFIX); ?>
     <?php if (count($aCities) >= ) {?>
     
  <select  style="width:280px;"  name="sCity" id="sCity" class="">
      <option value="">Select a State First</option>
     
   <?php  foreach($aCities as $city) { ?>     
        <option value="<?php echo $city['pk_i_id']; ?>"<?php if(Params::getParam('sCity') == $city['pk_i_id']) { ?>selected<?php ?>><?php echo $city['s_name'] ; ?></option>
<?php ?>
   </select>
<?php ?>

<button type="submit"  onmouseover="this.style.cursor='pointer';" class="" style="height:26px;" >Search</button>
                   
                                   
    </fieldset>
</form>

@cartagena68 or anybody,
How to add subcategory?
« Last Edit: April 08, 2013, 07:39:00 am by wakaka »

cartagena68

  • issues
  • Hero Member
  • *
  • Posts: 1175
Re: Cities dropdown based on Region Select
« Reply #29 on: April 08, 2013, 08:38:16 am »
Hi,
you can try to change this

Code: [Select]
<select style="width:300px;"  name="sCategory" id="sCategory" class="" >
                                      <option value="">Select a category</option>
                                            <?php // RESET CATEGORIES IF WE USED THEN IN THE HEADER ?>
                                            <?php osc_goto_first_category() ; ?>
                                            <?php while(osc_has_categories()) { ?>
              <option id="cat<?php echo osc_category_id(); ?>" value="<?php echo osc_category_id(); ?>"<?php if(Params::getParam('sCategory') == osc_category_id()) { ?>selected<?php ?>><?php echo osc_category_name(); ?></option>
                                               
                                            <?php ?>
                                       </select>

with this

Code: [Select]
<?php  if ( osc_count_categories() ) { ?>
            <?php osc_categories_select('sCategory'null__('Select a category''modern')) ; ?>
        <?php  ?>

that is the original category select