简体   繁体   中英

Check field and rewrite to array Codeigniter

I need to truncate string and rewrite it back to array

I have got a function where I get data from data base

 $data['about_text_list'] = $this->about_text_model->get_array();

I get these fields from data base : id, num, header, text, language

I need to strip_tags and truncate text with function word_limiter

        foreach ($data['about_text_list'] as $items)
        {
            $data['about_text_list']['text'] = word_limiter($items['text'], 100);

            $data['about_text_list']['text'] = strip_tags($items['text']);
        }

in view I do foreach

<? foreach ($about_text_list as $line) : ?>

    <td><?=$line['text']?></td>

<? endforeach; ?>

But I get error, please tell me how to do correct things like this...

In the loop in your controller, you're limiting the word count, then setting that to the value in the array. Then, you're overwriting that value with the strip_tags function. You're using both functions on the same value instead of using the altered values. (And I would strip the tags first, then limit the word count.)

You're also just overwriting the $data['about_text_list']['text'] value each iteration. I'm assuming this needs to be an array of 'text' values? I would create a new array with the updated content and merge your $data['about_text_list'] array with the new array.

Change that loop to this:

$newarray = array();
foreach ($data['about_text_list'] as $key => $value)
{
    $item_text = $value['text'];
    $altered = strip_tags($item_text);
    $newarray[$key]['text'] = word_limiter($altered, 100);
}
$data['about_text_list'] = array_merge($data['about_text_list'], $newarray);

// here, you create a new empty array,
// then loop through the array getting key and value of each item
// then cache the 'text' value in a variable
// then strip the tags from the text key in that item
// then create a new array that mirrors the original array and set 
//   that to the limited word count
// then, after the loop is finished, merge the original and altered arrays
//   the altered array values will override the original values

Also, I'm not sure what your error is (as you haven't told us), but make sure you're loading the text helper to give you access to the word_limiter function:

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

Of course, this all depends on the structure of your array, which I'm guessing at right now.

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