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>
答案 0 :(得分:5)
我可以在模型中看到错误
请替换
$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>
答案 1 :(得分:0)
您可以在视图页面中回显它。
<div>
<?php
// List up all results.
foreach ($results as $val)
{
echo $val['jantina']; echo ',';
echo $val['bangsa']; echo ',';
echo $val['agama'];
}
?>
</div>