首先我要道歉,因为我刚刚学习了 Codeigniter,我在使用选择选项显示数据库中的数据时遇到问题,没有错误但数据没有出现,供您参考,我已经连接了 3 个表。
这是我的控制器
class Harga extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('m_harga');
$this->load->helper('url');
$this->load->database();
}
function index(){
$this->load->helper('form');
$data['tabel_harga'] = $this->m_harga->tampil_data();
$this->load->view('v_harga',$data);
}
这是我的模型
class M_harga extends CI_Model{
function tampil_data(){
$this->db->order_by('id_harga','ASC');
return $this->db->from('tabel_harga')
->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
->get()
->result();
}
这是我的观点
<select class="form-control">
<option value="">All</option>
<?php
foreach($tabel_harga as $u)
{
echo '<option value="'.$u['id_vendor'].'">'.$u['nama_vendor'].'</option>';
}
?>
</select>
如果你帮助我,我将不胜感激,谢谢你们。
数据没有出现可能是因为您正在使用返回object
的result()
,并且您正在view
中获取array
数据。
型
class M_harga extends CI_Model{
function tampil_data(){
$this->db->select('*');
$this->db->from('tabel_harga');
$this->db->join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor', 'INNER');
$this->db->join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari', 'INNER');
$this->db->order_by('id_harga','ASC');
$query = $this->db->get()->result_array(); // use result_array() instead of result() as you're getting value as an array in your view.
return $query;
}
}
另外,请确保检查$tabel_harga
中的view
值,即
<select class="form-control">
<option value="">All</option>
<?php
if(!empty($tabel_harga)){
foreach($tabel_harga as $u){
?>
<option value="<?php echo $u['id_vendor']; ?>"><?php echo $u['nama_vendor']; ?></option>
<?php
}
}
?>
</select>
希望这对你有帮助。
试试这个
视图
<select class="form-control">
<option value="">All</option>
<?php
foreach($tabel_harga as $u)
{
echo '<option value="'.$u->id_vendor.'">'.$u->nama_vendor.'</option>';
}
?>
</select>
模型
class M_harga extends CI_Model{
function tampil_data(){
$this->db-join('tabel_vendor','tabel_vendor.id_vendor=tabel_harga.id_vendor')
$this->db-join('tabel_hari','tabel_hari.id_hari=tabel_harga.id_hari')
$this->db->order_by('id_harga','ASC');
$sql = $this->db->get('tabel_harga');
return $sql->result(); // returns an array of objects
}
控制器
class Harga extends CI_Controller{
function __construct(){
parent::__construct();
$this->load->model('M_harga');
$this->load->helper(array('url','form'));
}
function index(){
$data['tabel_harga'] = $this->M_harga->tampil_data();
$this->load->view('v_harga',$data);
}
db->select("name, value");
$this->db->from('settings');
$query = $this->db->get();
if ($query->num_rows())
{
foreach ($query->result_array() as $row)
{ // Your data is coming from multiple rows, so need to loop on it.
$siteData[$row['name']] = $row['value'];
}
}
return $siteData;
}