Welcome Guest, Not a member yet? Register   Sign In
Javascript array to codeigniter controller

[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.

// myController.php
$this->db->select('firstname, lastname, id');
$this->db->order_by('lastname asc');

$records = $this->db->get();

$data['records'] = $records->result_array();

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.

// 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;
    $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;
    <div class="attendee">
        <span class="checkbox">
            &lt;input type="checkbox" name="attendees[]" value="&lt;?=$record['id'] ?&gt;" /&gt;
        <span class="attendee-name">
            &lt;?=$lastname ?&gt;, &lt;?=$firstname ?&gt;
&lt;?php endforeach; ?&gt;

Theme © iAndrew 2016 - Forum software by © MyBB