[eluser]Nick Husher[/eluser]
The following code selects a list of names out of a table, orders them by their last name in alphabetical order and passes it to the view.
Code:
// myController.php
$this->db->select('firstname, lastname, id');
$this->db->from('mytable');
$this->db->order_by('lastname asc');
$records = $this->db->get();
$data['records'] = $records->result_array();
$this->load->view('myView',$data);
The view starts by listing a bunch of in-page anchors to various IDs in the page with the form "alpha-{LETTER}", where letter is a letter of the alphabet. It then iterates through the list, creating a large header wherever the first letter of the last name changes. It attaches an ID to this header in a form that matches the above in-page anchors, so you can easily locate a name in a generally-accepted format.
Code:
// myView.php
<a href="#alpha-a">A</a>
<a href="#alpha-b">B</a>
<a href="#alpha-c">C</a>
...
<a href="#alpha-z">Z</a>
<div id="top"></div>
$c = '';
<?php foreach($records as $record): ?>
<?php
$firstChar = substr($record['lastname'],0,1);
if($firstChar != $c) {
echo '<h1 class="alphabetical-heading" id="alpha-'.$firstChar.'">';
echo $firstChar;
echo '</h1>';
// create a link back up to the top
echo '<a href="#top" class="top-link">↑ Top</a>'
$c = $firstChar;
}
?>
<div class="attendee">
<span class="checkbox">
<input type="checkbox" name="attendees[]" value="<?=$record['id'] ?>" />
</span>
<span class="attendee-name">
<?=$lastname ?>, <?=$firstname ?>
</span>
</div>
<?php endforeach; ?>