New to CI and MVC - Just have a few questions - El Forum - 09-23-2010
[eluser]wwendorf[/eluser]
Hi all,
I'm just getting started with CI. So far, I like the simplicity of the MVC concepts built into CI. I've had experience with some MVC projects in the past, and they weren't a pleasant experience at all. I think CI is going to change my opinion (finally).
That being said, I was wondering if there is some guidelines as to the amount of code that should be in a view. For example.
I was going thru Jeffrey Way's tutorials and I got to tut#5 where he went the CRUD methods. His update method was hard coded, so I decided to add an update link to each record in the list, and use the same form for adding new records to edit an existing record.
Essentially what I am doing is when the user clicks the "update" button, I add the ID value to the existing data array and i'm adding a "get_one_record" function to the model. I'm just trying to figure out how much coding is actually allowed in the view in order to try and use it for double duty.
I know what I'd do, but I don't know if it's the "right" way.
Thanks,
Wade
New to CI and MVC - Just have a few questions - El Forum - 09-23-2010
[eluser]InsiteFX[/eluser]
I would have a seperate method for each.
Makes it easier to maintain later on.
InsiteFX
New to CI and MVC - Just have a few questions - El Forum - 09-24-2010
[eluser]wwendorf[/eluser]
Below is the code for the program I was talking about with my changes in it to allow editing on the same form as is used for creating a record.
Any comments on the methods I used would be most appreciated.
Thank you,
Wade
Code: <?php
class Site extends Controller {
function index($inval = "") {
$data = array();
if ($query = $this->site_model->get_records()) {
$data['records'] = $query;
}
$this->load->view('options_view',$data);
}
function create() {
if ($this->input->post('pid')) {
$data = array(
'id' => $this->input->post('pid'),
'title' => $this->input->post('title'),
'contents' => $this->input->post('contents')
);
$this->site_model->update_record($data);
$this->index();
} else {
$data = array(
'title' => $this->input->post('title'),
'contents' => $this->input->post('contents')
);
$this->site_model->add_record($data);
$this->index();
}
}
function delete() {
$this->site_model->delete_row();
$this->index();
}
function update() {
$id_to_update = $this->uri->segment(3);
//echo $id_to_update;
$return_data = $this->site_model->get_one_record($id_to_update);
/*echo "<pre>";
print_r($return_data);
echo "</pre>";*/
$data['records'] = $this->site_model->get_records();
$data['update'] = $return_data[0];
/*echo "<hr><pre>";
print_r($data);
echo "</pre>";*/
$this->load->view('options_view', $data);
}
}
?>
Code: <?php
class Site_model extends Model {
function get_records() {
$this->db->order_by("id", "desc");
$query = $this->db->get("data");
return $query->result();
}
function add_record($data) {
$this->db->insert('data',$data);
return;
}
function update_record($data) {
/*
echo "<pre>";
print_r($data);
echo "</pre>";
*/
$this->db->where('id',$data['id']);
$this->db->update('data', $data);
}
function delete_row() {
$this->db->where('id', $this->uri->segment(3));
$this->db->delete('data');
}
function get_one_record($data) {
$this->db->where('id',$data);
$query = $this->db->get("data");
return $query->result();
}
}
?>
Code: <html>
<head>
<title>Untitled</title>
<link rel="stylesheet" href="<?php echo base_url();?>css/style.css" type="text/css" media="screen" charset="utf-8">
</head>
<?php
//echo "<pre>";print_r($update->title);echo "</pre>";
?>
<body>
<div id="enter_data">
<?php echo anchor("site/","<h2>Create</h2>");?>
<?php echo form_open('site/create');?>
<p>
<label for="title">Title:</label>
<input type="text" name="title" id="title" value="<?php if (isset($update->title)) {echo $update->title;} ?>" />
</p>
<p>
<label for="content">Contents:</label>
<textarea name="contents" id="contents" rows=10><?php if (isset($update->contents)) {echo $update->contents;} ?></textarea>
</p>
<p>
<input type=submit name="submit" value="Submit"/>
</p>
<?php if (isset($update->id)) { ?>
<input type=hidden name="pid" id="pid" value='<?php echo $update->id; ?>'>
<?php } ?>
<?php echo form_close();?>
</div>
<div id="title">
<h2>Read</h2>
</div>
<?php
if (isset($records)) {
$i = 1;
foreach ($records as $row) {
?><div id=display><?php
echo "<h2>[Post #$i]" . anchor("site/delete/$row->id", $row->title); $row->title . "</h2>";
echo "<div id='contents'>" . $row->contents . "<br />";
echo anchor("site/update/$row->id","[update]") . "</div>";
echo "</div>";
$i++;
}
} else {
echo "<h2>No Records Returned</h2>";
}
?>
<hr />
<div id="title">
<h2>Delete</h2>
</div>
<div id="display">
<p>To sample the delete method, simply click on one of the headings listed above.</p>
</div>
</body>
</html>
|