国家下拉不填充数据codeigniter,任何机构可以帮助我,我想要查看国家列表,以便我可以选择国家。当我在视图中添加选择代码进行下拉时,它会隐藏列表并保存按钮。
我的模特
public function get_Country() {
$this->db->select('code, name')->from("country");
$query = $this->db->get();
return $query->result_array();
}
我的控制器
public function get_Country() {
$ctry = $this->job_post_model->get_Country();
// debug($ctry);
}
我的观看代码
<div class="row">
<div class="span8">
<div class="about-heading">
<div class="head-menu"> </div>
</div>
<div class="about-section">
<div class="about-content rigs-content">
<form name="jobpost" action=" <?php echo base_url('employer/job_post/post_job'); ?>" method="post">
<h2>Your Job Detail :</h2>
<div class="invest-form">
<ul>
<li class="pull-right">
<label>Job Category:</label>
<input type="text" name="jobcat" value="" placeholder="Enter Job Category*" />
</li>
<li class="pull-right">
<label>Job Title:</label>
<input type="text" name="jobtitle" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Job Type:</label>
<input type="text" name="jobtype" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Preffered Age:</label>
<input type="text" name="age" value="" placeholder="Enter Job Title*" />
</li>
<li>
<label>Preffered Gender:</label>
<input type="text" name="gender" value="" placeholder="Enter Job Type*" />
</li>
<li>
<label>Job Description:</label>
<input type="text" name="desc" value="" placeholder="Enter Job Type*" />
</li>
<li class="pull-right">
<label>Location:</label>
<input type="text" name="location" value="" placeholder="Enter the Country*" />
</li>
<li>
<label>Post Code:</label>
<input type="text" id="post" rel="popover" data-trigger="hover" name="postcode" placeholder="Enter the City*" />
</li>
<li class="pull-right">
<label>Salary:</label>
<input type="text" id="salaries" name="salary" placeholder="Enter the Salary*" />
</li>
<li>
<li>
<label>Qualification:</label>
<input type="text" id="qualification" rel="popover" data-trigger="hover" name="qualification" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Category:</label>
<input type="text" id="category" class="span12" required name="category" placeholder="Enter Job Tags" />
</li>
<label>Benifits:</label>
<input type="text" id="benefits" rel="popover" data-trigger="hover" name="benefits" placeholder="Enter Benifits*" />
</li>
<li class="pull-right">
<label>Job Tags:</label>
<input type="text" id="jobtags" class="span12" required name="jobtag" placeholder="Enter Job Tags" />
</li>
<li class="pull-right">
<label>Career Level:</label>
<input type="text" id="career" class="span12" required name="career" placeholder="Enter Job Tags" />
</li>
<li>
<label>Country:</label>
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country->name ?>" </option>
<?php }
?>
</select>
</li>
<li class="pull-right">
<table id="question">
<th>Add Killer Questions</th>
<tr>
<td><input type="text" id="questions" name="question[]"/></td>
</tr>
</table>
<td><input type="button" id="btnAdd" class="button-add" onClick="insertTextBox()" value="Add More"></input></td>
</li>
<br>
<input type="submit" value="Save As Draft" class="button-next" />
</div>
</form>
<div class="wid-social">
</div>
</div>
</div>
</div>
<script>
var index = 1;
function insertTextBox(){
var text = document.getElementById("question");
var row=text.insertRow(text.rows.length);
var cell1=row.insertCell(0);
var t1=document.createElement("input");
t1.id = "question"+index;
t1.name = "question[]";
cell1.appendChild(t1);
var cell2=row.insertCell(1);
index++;
}
</script>
答案 0 :(得分:0)
模特:
Repalce
返回$ query-&gt; result_array();
使用
返回$ query-&gt; result();
在视图中:
Repalce
<?php $countries = get_Country(); ?> <select name="country"> <option>Select your country*</option> <?php foreach ($countries as $country) { ?> <option value="<?php echo $country->name ?>" </option> <?php } ?> </select>
使用
<?php $countries = get_Country(); ?> <?php echo form_dropdown('country',$countries, 0,'required'); ?>
在Codeigniter中使用Codeigniter从下拉列表
答案 1 :(得分:0)
你的控制器没有return
任何东西,所以需要修改控制器,如下所示。
控制器:
public function get_Country() {
return $ctry = $this->job_post_model->get_Country();
}
以及您的VIEW将结果作为数组返回,您需要在$country['name']
的位置使用$country->name
。
查看:
<?php $countries = get_Country(); ?>
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country) { ?>
<option value="<?php echo $country['code'] ?>"><?php echo $country['name'] ?></option>
<?php }
?>
</select>
答案 2 :(得分:0)
您的模型是正确的,但您确实需要决定是以array
还是object
来访问数据。这将决定您如何在View中访问您的数据。
$query->result_array()
会返回array
$query->result()
会返回object
在您的控制器中,您需要使用您的模型。
// Load your model into your controller
$this->load->model('your_model');
// use your_model method to set $data['countries']
$data['countries'] = $this->your_model->get_Country();
// You will access $countries in your view by passing it to your view
$this->load->view('your_view'), $data);
在您看来,如果您在模型中选择$query->result()
:
<select name="country">
<option>Select your country*</option>
<?php foreach ($countries as $country): ?>
<option value="<?php echo $country->name ?>"><?php echo $country->name ?></option>
<?php endforeach; ?>
</select>