Welcome Guest, Not a member yet? Register   Sign In
Repetive html with based on dynamic data - view partial vs helper
#1

[eluser]msteudel[/eluser]
Hi All,

I have your table listing out a bunch of items to that can be edited/deleted

e.g.

Name | Actions
Mark | Edit - Delete
Bill | Edit - Delete
etc.

I have the following view:

Code:
<table class="data">
    <tr>
    <th>Id</th>
    <th>District</th>
    <th>State</th>
    <th>Actions</th>
    </tr>
    &lt;?php foreach( $districts as $district ): ?&gt;
    <tr>
    <td>&lt;?php echo $district['id'] ?&gt;</td>
    <td>&lt;?php echo $district['district_name'] ?&gt;</td>
    <td>&lt;?php echo $district['state_name'] ?&gt;</td>
    <td>&lt;?php $this->load->view( 'admin/district/format_actions') ?&gt;</td>
    </tr>
    &lt;?php endforeach ?&gt;
</table>

My thought of how to generate the actions (Edit - Delete) was to either use another view or use a helper. I initially went with the view idea and have this:

Code:
&lt;?php echo anchor( '/admin/district/update/' . $district['id'], 'Edit') ?&gt; |
&lt;?php echo anchor( '/admin/district/delete/' . $district['id'], 'Delete') ?&gt;

But $district isn't available to the format_actions view. I'm not sure if there is an easy way to pass a variable into a "sub-view". If there isn't then I guess I'll go for a helper ...

Any thoughts?

TIA, Mark
#2

[eluser]danmontgomery[/eluser]
View 1:
Code:
&lt;?php foreach( $districts as $district): ?&gt;
    <tr>
        <td>&lt;?php echo $district['id']; ?&gt;</td>
        <td>&lt;?php echo $district['district_name']; ?&gt;</td>
        <td>&lt;?php echo $district['state_name']; ?&gt;</td>
        <td>&lt;?php $this->load->view( 'admin/district/format_actions', $district ); ?&gt;</td>
    </tr>
&lt;?php endforeach ?&gt;

View 2:
Code:
&lt;?php echo anchor( '/admin/district/update/' . $id, 'Edit'); ?&gt; |
&lt;?php echo anchor( '/admin/district/delete/' . $id, 'Delete'); ?&gt;
#3

[eluser]Mischievous[/eluser]
Whats your controller look like?
#4

[eluser]msteudel[/eluser]
Oh I suppose another option would be to just use a normal include ... though it'd be nice to keep things consistent with CI methods
#5

[eluser]msteudel[/eluser]
[quote author="Mischievous" date="1272070398"]Whats your controller look like?[/quote]

Here's my controller:

Code:
function __construct()
    {
    parent::Controller();
    $this->load->library('pearloader');
    $this->load->model( 'district_model' );
    $this->load->model( 'state_model' );
    }

    function index()
    {
    $data['title'] = ' Admin > Districts | Math Monster Mystery';
    $data['districts'] = $this->district_model->get_districts();
    
    $this->load->view( 'main_template', $data );
    }

// more unrelated methods

}
#6

[eluser]msteudel[/eluser]
Oh an here's the main_teplate:

Code:
&lt;?php
$this->load->view( 'includes/header' );

$uri = explode( '/', uri_string() );

switch( $uri[3] ) {
    case "":
    $this->load->view( $_SERVER['REQUEST_URI'] . '/index' );
    break;
    default:
    $this->load->view( $_SERVER['REQUEST_URI'] );
}

$this->load->view( 'includes/footer' );
?&gt;
#7

[eluser]msteudel[/eluser]
[quote author="noctrum" date="1272070316"]View 1:
Code:
&lt;?php foreach( $districts as $district): ?&gt;
    <tr>
        <td>&lt;?php echo $district['id']; ?&gt;</td>
        <td>&lt;?php echo $district['district_name']; ?&gt;</td>
        <td>&lt;?php echo $district['state_name']; ?&gt;</td>
        <td>&lt;?php $this->load->view( 'admin/district/format_actions', $district ); ?&gt;</td>
    </tr>
&lt;?php endforeach ?&gt;

View 2:
Code:
&lt;?php echo anchor( '/admin/district/update/' . $id, 'Edit'); ?&gt; |
&lt;?php echo anchor( '/admin/district/delete/' . $id, 'Delete'); ?&gt;
[/quote]

Oh ... I bet that's in the documentation ... thanks!




Theme © iAndrew 2016 - Forum software by © MyBB