0

I have difficulty with populating dropdown

This is the add.php view

  <?php
  echo form_open('',$attributes);?>

  <table cellpadding="0" cellspacing="0" class="user_table biz_table">
<tr>
  <th>City:</th>
    <td>
     <select id="city_id" name="city_id">
    <?php foreach($cities as $c){ ?>

         <option value="<?php echo $c->id; ?>" <?php if ($biz['city_id']===$c->id){ 
             >selected="selected"<?php }?> ><?php echo $c->name?></option>

    <?php }?>

     </select>
    <tr>    
     <th>Neighborhood:</th>
  <td>
  <select id="district_id" name="district_id">
    <option value=""></option>
     <?php foreach($districts as $d){ ?>

      <option value="<?php  echo $d->id; ?>" <?php if($biz['district_id']===$d->id){?
          >selected<?php }?>><?php  echo $d->name?></option>
         <?php }?>

  </select>
<span style="color: red;"><?php echo form_error('district_id'); ?></span>
 </td>
</tr> 

This is the javasript. I believe it was used to refresh the page!!!!!

<script type="text/javascript">

var cities = [];
<?php foreach($cities as $city):?>
   cities[<?php echo $city->id ?>] = '<?php echo $city->name?>';
<?php endforeach;?>

$(function(){
   $('#city_id').change(function(){
  city_id=$('#city_id').val();
  Utils.loadAction('#district_id','/biz/get_children/'+city_id+'/city');
});

$('#catid_1').change(function(){
  catid_1=parseInt($('#catid_1').val());
  Utils.loadAction('#subcat','/biz/get_children/'+catid_1+'/category');
});

<?php if(isset($biz['rating']) && $biz['rating']>0):?>
  var biz_rating=parseInt('<?php $biz['rating']?>');
  if(biz_rating>0)
  {
    $('#rating').val(biz_rating);
    $('.star-'+biz_rating).addClass('active-star');
    $(".rating-hint").html($(".star-"+biz_rating).attr("title"));
  }
<?php endif;?>

}); 

This is a controller biz.php

     Class Biz extends CI_controller
     {

     function __construct()
      {
    parent::__construct();
      }



     public function add()
     {

    if(!$this->tank_auth->is_logged_in())
    {
        redirect('/ucp/login/');
    }
    $this->load->helper('form');
    $biz=$this->get_form_data();
    $with_review=1;
    if(!empty($_POST)&&!isset($_POST['with_review']))
    {
        $with_review=0;
    }




    //validating
    $this->load->library('form_validation');
    $this->form_validation->set_error_delimiters('','');
    $this->form_validation->set_rules('city_id', 'City',  
          'trim|required|intval|max_length[20]|callback_city_check');
    $this->form_validation->set_rules('district_id', 'District', 
          'trim|intval|max_length[20]|callback_city_check');

    if($this->form_validation->run())
    {
        //get validated data
        $biz=$this->get_form_data();


        }


        //save business
        $this->load->model('bizs');
        $bizid = $this->bizs->add($biz);



        redirect('/biz/'.$bizid);

    }

    //get cities
    $this->load->model('catsAndCities','cc');
    $this->cc->set_table_name('city');
    $data['cities'] = $this->cc->get_top();

    if(!$biz['city_id'])
    {
        //$city=$data['cities'][0];
        $biz['city_id'] = 0;
        if($this->tank_auth->get_user_city())
            $biz['city_id']=$this->tank_auth->get_user_city()->id;

    }

    $data['districts']=$this->cc->get_children($biz['city_id']);


            //$data['districts']=$this->cc->get_children($biz['city_id']);

    //$data['districts']=$this->cc->get_children();
    $data['biz']=$biz;
    $data['heading']="Add A Business";

    $this->load->view('biz/add',$data);

          }

get_form_data() inside controller biz.php

private function get_form_data()
{
$biz=array(
  'city_id'=>$this->input->post("city_id"),
  'district_id'=>$this->input->post("district_id")
    );
return $biz;
}

get User city in libraries/tank_auth.php

function get_user_city()
{

    $this->ci->load->model('catsAndCities','cc');
    $this->ci->cc->set_table_name('city');
    $this->ci->load->helper('cookie');
    if($cookie_v = get_cookie($this->ci->config-
        >item('default_city_cookie_name'),TRUE))
    {

        if($city = $this->ci->cc->get($cookie_v,'slug'))
        {
            if($city->parent_id == 0)
            {
                $this->city = $city;
                return $city;
            }
        }
    }
    $city = array_shift($this->ci->cc->get_top());

    $this->city = $city;
    return $city;
     }

These two are in model catsandcities.php

public function get_all()
{


    $data=array();
    $this->db->order_by('parent_id','asc');
    $this->db->order_by('`order`','asc');
    $query=$this->db->get($this->table_name);
    foreach($query->result() as $row)
    {

        $data[$row->id]=$row;
    }

    return $data;

}

public function get_children($parent_id)
{
    $children=array();
    if($items=$this->get_all())
    {
        foreach($items as $i)
        {
            if($i->parent_id === $parent_id)
            {
                $children[]=$i;
            }
        }
    }
    return $children;
}

the files are located here https://github.com/leungxd/upble

Thanks

3
  • 1
    See stackoverflow.com/help/mcve. Please put in a little effort to narrow down your problem - you might find that in the process, you answer your own question. Commented Feb 12, 2014 at 23:25
  • Well, I realized that. But people ask for information. So, I tried to post all related functions. Commented Feb 12, 2014 at 23:36
  • Apparently, it works just fine when I put the script in root directory. And it doesn't when it's in folder. What's going on? Commented Feb 12, 2014 at 23:37

2 Answers 2

1

I don't fully understand your question but looking at the code I see a lot of hassle to do a simple task. My advice:

  1. Use the form helper to generate the dropdowns
  2. Simplify the way you access your data and delegate the controller to only retrieve such data
  3. Put as little as you can in the views, don't do any logic there

1 - use the form helper

<?= form_dropdown('city_id', $cities, 'default') ?>

This will generate the select and options html code for you. Just make sure it is inside the form_open() function.

2 - Simplify the way to access data...

Take for example your method:

public function get_children($parent_id)
{
    $children=array();
    if($items=$this->get_all())
    {
        foreach($items as $i)
        {
            if($i->parent_id === $parent_id)
            {
                $children[]=$i;
            }
        }
    }
    return $children;
}

Instead of loading all the records from your table, filter by the parent on a query level:

public function get_children($parent_id)
{
    $this->db->where('id_parent', $parent_id);
    $query = $this->db->get($this->table_name);

    $result = $query->result();
    return $result;
}

3 - Don't put logic on the views

I would suggest to create a method in your controller where you return a json encoded object and then call it by an ajax request with jquery and then populate the children dropdown:

public function get_children($parent_id)
{
    $this->db->where('id_parent', $parent_id);
    $query = $this->db->get($this->table_name);

    $result = $query->result();

    if(empty($result) == FALSE) {
        return json_encode($result);
    }

    return NULL;
}

So whenever you call yoururl/controller/get_children and pass the parent id by a post you'll get the children for that city in a json encoded way that you can get with jquery to manipulate data client-side, instead of having thousands of records bloating up your html.

Read the guidelines of stackoverflow or you won't get much help, and try to be clear on what problem you are trying to solve. Good luck!

Sign up to request clarification or add additional context in comments.

Comments

0

I just added a subdomain, and it solved the problem.

thanks

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.