Alphabetical lists in CodeIgniter
Wondering how I can do this with 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.
- //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; }
//in controller call the model and pass the return array to view
in view
-
$V){
echo "
- ".strtoupper($k)."
- ".$links[0]." "; } echo '
- ";
foreach($v as $links){
echo "
use css to decorate to get exact match as you are looking
精彩评论