Pregunta

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!!!

¿Fue útil?

Solución 3

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;
        }  
    }
}

Edit:

You are calling db stuff in your view which is entirely not Codeigniter MVC.

This is what it might look like in MVC:

Controller

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;
        }  
    }
}

Model

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";
    }

}

View

<html>
<body>
<b><?php echo $test_text; ?></b>
</body>

Otros consejos

You should at least define this function as a method of your model (where you take this description from).

class TextModel extends CI_Model {
   ...
   public function getShortDescription(){
       $chars = 100;  
       $mytext = substr($this->mytext,0,$chars);  
       $mytext = substr($mytext,0,strrpos($mytext,' '));    
       return $mytext;
   }
}

And the in your controller smth along the lines of:

class Blog extends Controller {

    function index()
    {
            $this->load->model('TextModel');
        $data['short_description'] = $this->TextModel->getShortDescription();

        $this->load->view('blogview', $data);
    }
}

And at last, in your view:

echo '<td><p>'.$short_description.'</p></td>';

I'm not familiar with CodeIgniter, but I guess any data manipulation should be done in models, thus you will keep your controllers 'thin' and won't violate MVC paradigm.

Since you already have a working code, all you need to decide is where in the MVC paradigm of CI should this be transferred to. I would suggest putting this inside a helper. Helpers in CI are just what the name suggests, a simple php file with a bunch of functions that help you to get certain tasks done. Suppose you put this inside a file called 'utility.php' You could then include this file wherever you need using

$this->load->helper('utility');

and then call the truncate function.

Now, if you have a bunch of related tasks which you want to neatly organize in a class based structure you could create your own CI library. Suppose you created a library named 'truncate', then similar to helpers these are loaded as

$this->load->library('truncate');

Personally, for a bunch of utilities I would just put them all into a helper instead of extending core CI classes or creating a custom library.

The current answers explain that you can add the method to the model and also the controller. Functionally, this is no problem, as you said the function works perfectly wherever you place it.

What happens when you need to call the same method in another controller? Or with a different model?

It is most likley you will end up with some code duplication.

As a possible soultion, you could create a library of classes that allow you to keep all you string manipulation in one place. Simply add more methods to your class...

// example/libary/path/TextFormatter.php
class TextFormatter
{
  protected $_text = '';

  public function __construct($text)
  {
    $this->_text = $text;

    return $this;
  }

  public function truncate($maxChars = 100)
  {
    $truncated = substr($this->_text, 0, $maxChars);  
    $truncated = substr($truncated, 0, strrpos($truncated,' '));    

    return $truncated;
  }

  public function stripHtml()
  {
    ///.....
  }

  public function doSomethingElseWithText()
  {

  }

}

Then in the controller...

// Controller.php

class SomeController extends Controller
{

  public function index()
  {
    $formatter = new TextFormatter('Example text string');  
    $data['formattedText'] = $formatter->truncate();

  }


}

The only caveat is the TextFormatter class needs to be autoloaded/callable from wherever you use it.

Controller: create a controller truncate.php.

<?php
 Class Truncate extends CI_Controller {

 function __construct() {
  parent::__construct();
 }

 function truncate_text($mytext) {  
     //Number of characters to show
     $chars = 100;
     if(empty($mytext))
         echo '$mytext is empty.';
     $mytext = substr($mytext,0,$chars);  
     $data['mytext'] = substr($mytext,0,strrpos($mytext,' '));    
     $this->load->view('truncate_view',$data);
 }


View: create a view file truncate_view.php

<?php
    echo '<td><p>'.$mytext.'</p></td>';
   //Write your other view code if any are present.
?>
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top