开发者

Alphabetical lists in CodeIgniter

Wondering how I can do this with CodeIgniter?

Alphabetical lists in CodeIgniter

Any help is greatly app开发者_开发技巧reciated!

Thanks.


There's no built in function to do this. You'll have to loop through an sql query to do this...

Select * from table order by field asc

foreach ( $result as $thing ) {

    if ( $thing['name'][0] != $first_letter) {
        echo '<h2>' . $thing['name'][0] . '</h2>';
        $first_letter = $thing['name'][0];
    }
    echo $result['thing'];

}

this is just a rough example...never echo html like that.


  1. //in model public function alphaDirectory(){ // Query for data $sql = "SELECT SUBSTRING(column_name,1,1) AS letter, column_name,column_link FROM table ORDER BY column_name"; $query = $this->db->query($sql); $columnByName="";

foreach ($query->result() as $row) { $columnByName[$row->letter][] = array($row->column_name,$row->column_link); } return $columnByName; }

  1. //in controller call the model and pass the return array to view

  2. in view

      $V){ echo "
    • ".strtoupper($k)."
      • "; foreach($v as $links){ echo "
      • ".$links[0]."
      • "; } echo '
      ' } ?>

  3. use css to decorate to get exact match as you are looking

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜