CodeIgniter 连接表
Posted
技术标签:
【中文标题】CodeIgniter 连接表【英文标题】:CodeIgniter join table 【发布时间】:2018-04-26 21:34:50 【问题描述】:This is my output // As you can see the Name is not the exact name
我找到了解决这个问题的代码,但我不确定我应该使用哪种编码。你能帮忙吗? 这是一组函数编码:
<?php
$rn= "0001123456";
$rn2= "A123456";
$dob = "1986-10-25";
$dreg = "1986-10-26";
echo "<br/>".$rn;
echo "<br/>".numberToAlpha($rn);
echo "<br/>".alphaToNumber($rn2);
echo "<br/>cur_age : ".calculateCurrentAge($dob);
echo "<br/>reg_age : ".calculateRegisterAge($dob, $dreg);
/*** End Contoh Penggunaan ***/
function numberToAlpha($rn)
if (strcmp('0000', substr($rn,0,4)) == 0)
$rn = 'A'.substr($rn, 4, 6);
else
$rn = 'B'.substr($rn, 4, 6);
return $rn;
function alphaToNumber($rn)
if (strcmp('A', substr($rn,0,1)) == 0)
$rn = '0000'.substr($rn, 1, 6);
else
$rn = '0001'.substr($rn, 1, 6);
return $rn;
function calculateCurrentAge($dob)
$from = DateTime::createFromFormat('Y-m-d', $dob);
$to = new DateTime('today +1 day');
$age = $from->diff($to);
return $age->format('%y years, %m months, %d days');
function calculateRegisterAge($dob, $dreg)
$from = DateTime::createFromFormat('Y-m-d', $dob);
$to = DateTime::createFromFormat('Y-m-d', $dreg);
$to = $to->modify('+1 day');
$age = $from->diff($to);
return $age->format('%y years, %m months, %d days');
您能否告诉我是否要将名称更改为正确的名称,我应该使用哪种编码以及在哪里添加这些编码?
这是我的控制器'Search.php':
<?php
class Search extends CI_Controller
public function __construct()
parent::__construct();
$this->load->helper('form');
$this->load->model('Search_model');
public function index()
$this->load->view('search_form');
public function execute_search()
// Retrieve the posted search term.
$search_term = $this->input->post('search');
// Use a model to retrieve the results.
$data['results'] = $this->Search_model->get_results($search_term);
// Pass the results to the view.
$this->load->view('search_results',$data);
这是模型'Search_model.php':
<?php
class Search_model extends CI_Model
public function get_results($search_term='default')
// Use the Active Record class for safer queries.
$this->db->select('rn,nama,tarikhlahir,jantina,agama,bangsa');
$this->db->from('pesakit');
$this->db->like('rn',$search_term);
$this->db->limit(100);
// Execute the query.
$query = $this->db->get();
// Return the results.
return $query->result_array();
这是搜索“search_form.php”的视图:
<?php
echo form_open('search/execute_search');
echo form_input(array('name'=>'search'));
echo form_submit('search_submit','Submit');
?>
这是结果视图'search_results.php':
<div>
<?php
// List up all results.
foreach ($results as $val)
echo 'RN : '; echo $val['rn']; echo "<br/>";
echo 'Name : '; echo $val['nama']; echo "<br/>";
echo 'Date.of.Birth : '; echo $val['tarikhlahir']; echo "<br/>";
echo 'Gender : '; echo $val['jantina']; echo "<br/>";
echo 'Race : '; echo $val['bangsa']; echo "<br/>";
echo 'Religion : '; echo $val['agama'];
?>
</div>
【问题讨论】:
首先在控制器中检查你的结果。您可以使用 print_r($data['results']);打印结果。 您能不能试试$query = $this->db->get(); echo $this->db->last_query();die;
并在phpmyadmin 中运行输出(检查查询是否有效。)??
【参考方案1】:
我可以看到您的模型中有错误
请更换
$this->db->select('jantina','bangsa','agama');
与
$this->db->select('jantina,bangsa,agama');
在你看来
<div>
<?php
// List up all results.
foreach ($results as $val)
echo $val['jantina'].'<br>';
echo $val['bangsa'].'<br>';
echo $val['agama'];
?>
</div>
【讨论】:
嗨,我想加入表格,但我应该把编码放在哪里 $this->db->join('jantina','jantina.kod = jantina.nama') @flac 请发布一个新问题并给我链接。 :) @flac 我无法理解【参考方案2】:您可以在视图页面中回显它。
<div>
<?php
// List up all results.
foreach ($results as $val)
echo $val['jantina']; echo ',';
echo $val['bangsa']; echo ',';
echo $val['agama'];
?>
</div>
【讨论】:
未定义索引:bangsa 和未定义索引:agama 你在控制器中的结果是什么?打印出来。 如何打印@HananAshraf 在你的控制器之后$data['results'] = $this->Search_model->get_results($search_term);
添加print_r($data['results']);exit;
输出是 'Array ( [0] => Array ( [jantina] => 003 ) )' ,请问为什么得到 'array' ??以上是关于CodeIgniter 连接表的主要内容,如果未能解决你的问题,请参考以下文章