I have this function on my View Page, which truncates some data to present it on my table.
function truncate($mytext) {
//Number of characters to show
$chars = 100;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
return $mytext;
}
I set a local variable for my dynamic text:
$mydescription = $value['PROBLEM_DESCRIPTION']
On the same page, I have this:
echo '<td><p>' .truncate($mydescription). '; ?></p><</td>
And it works perfect, so my question is how can apply this on an MVC architecture using Codeigniter? If somebody has an idea let me know, thanks!!!
You could do it in the controller:
class Something extends CI_Controller{
public function index(){
$data['mytext'] = $this->_truncate('some text to truncate');
//truncated text will be available in the view as $mytext
$this->load->view('some_view', $data);
}
private function _truncate($text = NULL){
if($text){
$chars = 100;
$mytext = substr($text,0,$chars);
$mytext = substr($text,0,strrpos($text,' '));
return $mytext;
}
}
}
You are calling db stuff in your view which is entirely not Codeigniter MVC.
This is what it might look like in MVC:
class Something extends CI_Controller{
public function index(){
$test_text = $this->my_model->get_text();
$data['test_text'] = $this->_truncate($test_text);
//truncated text will be available in the view as $test_text
$this->load->view('some_view', $data);
}
private function _truncate($text = NULL){
if($text){
$chars = 100;
$mytext = substr($mytext,0,$chars);
$mytext = substr($mytext,0,strrpos($mytext,' '));
return $mytext;
}
}
}
class My_Model extends CI_Model{
public function get_text(){
//$this->db->get_where....or other db functions
return "testing text... this is only a test";
}
}
<html>
<body>
<b><?php echo $test_text; ?></b>
</body>