dppx9253 2011-10-30 14:20
浏览 35
已采纳

php返回数组[关闭]

Using codeigniters mvc, I have created a model for my search query and I would like to return it as an array.

I am a bit lost, I have created a foreach statement so that I can perform a function on each of the strings. (Not sure if it is the right way to go, and would now like to pass it as an array to my $data variable.

Could somebody point me in the right direction and highlight what I have done wrong:

<?php

class Search_model extends CI_Model {

            function search($searchquery)
    {

                function supertruncate($text, $searchquery, $characters_before, $characters_after){
                  $pos = strpos($text, $searchquery);
    $start = $characters_before < $pos ? $pos - $characters_before : 0;
    $len = $pos + strlen($word) + $characters_after - $start;
                   $text =  substr($text, $start, $len);
                   $last = strrpos($text, ' ');

                  return  str_ireplace($word, '<span class="highlight" style="background: #E6E6E6;">' . $word . '</span>', $text);
}

     $this->db->from('content');
$this->db->like('title', $data);
$this->db->or_like('content', $data);
$query = $this->db->get();

$characters_before="120";
 $characters_after="120";

foreach ($query ->result() as $row)
{
    $title =  strip_tags($row->title);
   $text = supertruncate(strip_tags($row->content), $searchquery, $characters_before, $characters_after);

}

    return $data;

    }

}
  • 写回答

1条回答 默认 最新

  • dongshao1156 2011-10-30 14:40
    关注

    model:

    ...
    $results = Array();
    foreach ($query->result() as $row)
    {
        $results[] = Array(
            'title' => strip_tags($row->title),
            'text' => upertruncate(strip_tags($row->content), $searchquery, $characters_before, $characters_after)
        );
    }
    return $results;
    ...
    

    controller:

    $this->load->model('Search');
    $data['search_results'] = $this->Search->Search('Invest');
    $this->load->view('template', $data);
    

    view:

    <div id="search_results">
        <?php foreach($search_results as $result): ?>
        <h1><?php echo $result['title']; ?></h1>
        <div><?php echo $result['text']; ?></div>
        <?php endforeach; ?>
    </div>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 keil的map文件中Image component sizes各项意思
  • ¥30 BC260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)