douduikai0562 2010-04-24 21:32
浏览 18
已采纳

Codeigniter的CRUD问题

I have a database with blog entries in it. The desired output I am trying to acheive is 3 entrys displayed using the css3 multi paragraph and then another 3(or more) formatted with the codeigniter Character_limiter. So I need 3 displays formatted one way and 3+ formatted another way on the same page.

So far this is what I have, but i do not know how to format the sql to achieve what I want. I can call all posts in descending order like I want, but dont know how to separate the code to achieve my output Controller:

$this->db->order_by('id', 'DESC');
$this->db->limit('2');
$query = $this->db->get('posts');
if($query->result())
$data = array();
{
$data['blog'] = $query->result();
}
$data['title'] = 'LemonRose';
$data['content'] = 'home/home_content';
$this->load->view('template1', $data); 

View:

    <?php if (isset($blog)): foreach ($blog as $row): ?>
<span class="title"><?php echo $row->title; ?> </span>
<?php echo $row->date; ?>
<div class="split"><?php echo $row->post =$this->typography->auto_typography($row->post); ?></div>
<?php echo 'Post #',$row->id; ?>
<p> Trackback URL: <? echo base_url()."trackbacks/track/$row->id";?></p>    
<hr />
<?php endforeach; 
endif;
?>

The split class is multiple columns. I tried 2 different querys but dont know how to separate all the post displays. Also one query always overides the second and produces all split or all character limited paragraphs.

Im lost here lol. Thanks

  • 写回答

1条回答 默认 最新

  • dongtang1966 2010-04-25 03:55
    关注

    I need 3 displays formatted one way and 3+ formatted another way on the same page.

    This sounds like display logic, which means it's a job for your view. Your controller can simply do a single query getting all or some of the entries (say the most recent 10) and pass it off to the view to decided how to display the data.

    In your view you can simply use conditional logic in your foreach() loop to determine how to output the post data.

    
    
    $limit = 3; // how many posts should we show in full?
    $i     = 1; // count
    
    foreach ($blog as $row): 
    
    if( $i < $limit ) // we are under our limit
    {
      echo $row->post; // show full post   
    }   
    else // we are over our limit
    {
      echo $this->typography->auto_typography($row->post); // show truncated post 
    }
    
    $i++; // increase count
    
    endforeach; 
    unset($limit,$i,$row)
    
    
    

    This means your controller could pass the full data to any number of views, each having their own way to display the data, and the view above could be called by any controller and faithfully display the posts passed to it in the way prescribed.

    If this doesn't sit well with you, then you can manipulate the data in your controller or model (or create a helper function) with use of array_walk() and a helper function:

            // user function for array_walk  
        function truncate_post(&$value,$key,$limit)
        {
           if($key >= $limit)
           {
               $value->post = $this->typography->auto_typography($value->post);
           }
        }
    
        $query = $this->db->get('posts', 10); // get 10 posts
        $results = $query->result();
        array_walk($results, 'truncate_post', 3); // truncate all but top 3 posts
            $data['blog'] = $results;
            $this->load->view('template1', $data); 
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本