CodeIgniter Forums
Javascript array to codeigniter controller - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived General Discussion (https://forum.codeigniter.com/forum-21.html)
+--- Thread: Javascript array to codeigniter controller (/thread-12365.html)

Pages: 1 2


Javascript array to codeigniter controller - El Forum - 10-17-2008

[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 = '';
&lt;?php foreach($records as $record): ?&gt;
&lt;?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">&uarr; Top</a>'
        
        $c = $firstChar;
    }
?&gt;
    <div class="attendee">
        <span class="checkbox">
            &lt;input type="checkbox" name="attendees[]" value="&lt;?=$record['id'] ?&gt;" /&gt;
        </span>
        <span class="attendee-name">
            &lt;?=$lastname ?&gt;, &lt;?=$firstname ?&gt;
        </span>
    </div>
&lt;?php endforeach; ?&gt;