我在填充下拉列表时遇到困难
这是 add.php视图
<?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>
这是javasript。我相信它用于刷新页面!!!!!
<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;?>
});
这是控制器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);
}
控制器biz.php 内的get_form_data()
private function get_form_data()
{
$biz=array(
'city_id'=>$this->input->post("city_id"),
'district_id'=>$this->input->post("district_id")
);
return $biz;
}
获取图书馆/ 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;
}
这两个是模型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;
}
文件位于https://github.com/leungxd/upble
由于
答案 0 :(得分:1)
我不完全理解你的问题,但看看代码,我看到做一个简单的任务很麻烦。我的建议:
1 - 使用表单助手
<?= form_dropdown('city_id', $cities, 'default') ?>
这将为您生成select和options html代码。只需确保它在form_open()函数内。
2 - 简化访问数据的方式......
以你的方法为例:
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;
}
不是从表中加载所有记录,而是在查询级别上按父级过滤:
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 - 不要在视图上添加逻辑
我建议在你的控制器中创建一个方法,在这个方法中返回一个json编码的对象,然后用jquery的ajax请求调用它,然后填充子下拉列表:
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;
}
因此,每当您调用yoururl / controller / get_children并通过帖子传递父ID时,您将以json编码的方式获取该城市的子项,您可以使用jquery来操作数据客户端而不是让数以千计的记录膨胀你的HTML。
阅读stackoverflow的指南,否则您将无法获得太多帮助,并尝试清楚您要解决的问题。祝你好运!
答案 1 :(得分:0)
我刚添加了一个子域名,它解决了这个问题。
感谢