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

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


Messages In This Thread
Javascript array to codeigniter controller - by El Forum - 10-16-2008, 07:15 AM
Javascript array to codeigniter controller - by El Forum - 10-16-2008, 08:31 AM
Javascript array to codeigniter controller - by El Forum - 10-16-2008, 08:52 AM
Javascript array to codeigniter controller - by El Forum - 10-16-2008, 06:35 PM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 01:52 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 01:59 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 02:06 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 02:11 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 06:41 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 06:42 AM
Javascript array to codeigniter controller - by El Forum - 10-17-2008, 07:08 AM



Theme © iAndrew 2016 - Forum software by © MyBB