简体   繁体   中英

CodeIgniter join table

This is my output // As you can see the Name is not the exact name

And I found a code to solve this problem but I'm not really sure about which coding should I have to use. Can you please help?
This is a set of function coding:

<?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');
}

Can you tell me if I want to change the Name to a proper Name which coding should I use and where to add these coding?

This is my controllers '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);
    }
}

This is model '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();
    }
}

This is views for search 'search_form.php':

<?php
    echo form_open('search/execute_search');

    echo form_input(array('name'=>'search'));

    echo form_submit('search_submit','Submit');

?>

This is results views '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>

I can see an error in your model

Please replace

$this->db->select('jantina','bangsa','agama');

With

$this->db->select('jantina,bangsa,agama');

And in your view

<div>
    <?php
        // List up all results.
        foreach ($results as $val)
        {
            echo $val['jantina'].'<br>';
            echo $val['bangsa'].'<br>';
            echo $val['agama'];
        }
    ?>
</div>

You might echo it in the view page.

<div>
    <?php
        // List up all results.
        foreach ($results as $val)
        {
            echo $val['jantina']; echo ',';
            echo $val['bangsa']; echo ',';
            echo $val['agama'];
        }
    ?>
</div>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM