CodeIgniter Forums

Full Version: View/Edit view file
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Samuurai[/eluser]
Hi Everyone,

This isn't a codeigniter question specifically, but I'm hoping someone can give me some ideas regardless...

I have a view which displays a list of database entries. You can then click on one to show it's details.

I want to be able to view the details in an un-editable way, but if the person has enough rights, they will be able to edit it.

What is the best approach with the view... should I create two views.. one for editing and one for viewing, or should I create one view which functions in both ways with a flag setting or something?

Thanks!

Beren

El Forum

[eluser]renownedmedia[/eluser]
I'd put them in one view and send a flag to enable or disable.

controller:
Code:
$data['admin'] = true;

view:
Code:
foreach($items AS $item) {
echo "<tr>";
if ($admin)
  echo "<td>Edit, Delete</td>";
else
  echo "<td>&nbsp;</td>";
echo "<td>{$item['name']}</td>";
}

Of course, in your controller for performing actions, you'd make a permission check in case they happened to know the url format for deleting.

El Forum

[eluser]Samuurai[/eluser]
Brilliant.. That's great, thanks!

Beren

El Forum

[eluser]Samuurai[/eluser]
I'm having some issues with this approach... my code is currently like this:
Code:
foreach($result as $row)
echo "<h1>Foo is </h1>";
{
    if(isset($new)) {
        echo '&lt;input type="text" name="foo"&gt;';
    }
    elseif(isset($edit)) {
        echo '&lt;input type="text" name="foo" value="$row[foo]"&gt;';
    }
    else {
        echo $row->foo;
    }
}
This code is deeply flawed! If I want to add a new record, no queries will be performed so $result will not be set, so it errors.

How should I be doing this?

El Forum

[eluser]Jondolar[/eluser]
In your controller, when you want to add a record (instead of edit) create an array called $result with one blank "record" and pass it to the view just like you would if you got the data from the model.

$result = array();
$result[0] = '';

Also, you may want to read up on MVC and not use "echo" to echo out your html code in either your controller or your view. Just a suggestion.

El Forum

[eluser]Samuurai[/eluser]
Thanks for your reply That worked perfectly! My code wasn't that broken after all!

Yeah, I only used echo's for this example.. I usually use the &lt;?=$row->blah=> syntax.

Thanks once again CodeIgniter forums for sorting out my problem!