Having problems with creating a CRUD.. |
[eluser]ealonw[/eluser]
Hi guys... Im still having the same problem... can anyone give me more insight on what im doing wrong... take a look at the update function and the view thats called... *************************************8 function update() { $this->load->helper('form'); // $id = $this->uri->segment(3); $query = $this->db->getwhere('usercomment', array('id' => $id)); $data['id'] = $query->result(); if ( count($_POST) > 0 ) { $this->db->where('id', $id); $this->db->update('usercomment', $_POST); redirect(''); } else { $this->load->view('update_contact_view', $data); } } ****************************************************** the expected outcome is to grab the mysql data based on the selected EDIT row which it doesnt do it gives me an empty form... See index VIEW below ******************************************************* <?php $i = 1; if ($query->num_rows() > 0): foreach ($query->result() as $row): ?> <tr class="<?php if($i%2 == '0'): echo "alt"; else: echo "row"; endif; ?>"> <td class="td-center"><?php echo $row->fname;?></td> <td class="td-center"><?php echo $row->lname;?></td> <td class="td-center"><?php echo $row->email; ?></td> <td class="td-center"><?php echo $row->comment;?></td> <td class="td-center"><a href="<?=site_url("contact/update/$row->fname");?>">Edit</a> <a href="<?=site_url("contact/delete/$row->fname");?>">Delete</a></td> </tr> <?php endforeach; endif; ?>
[eluser]crumpet[/eluser]
ok heres what you need to do first of all your table comments needs to have a field called id and that needs to be the primary key. It also needs to be set to auto increment. your controller needs to have a few different functions: viewComments(){ //retrieves all comments from the database //then loads a view called viewComments or something which takes this data and displays it } editComment($ID){ //loads a view file called editComment that displyas a form which submits to //do_editComment($ID) //this form needs to have hidden field which has the value of $ID } do_editComment(){ //takes looks form $_POST('ID') in the comments table and updates it with the rest of hte //information supplied (the rest of the $_POST array) } .. look at the video tutorials for code igniter. he shows how to do hidden fields in the blog one
[eluser]ealonw[/eluser]
Okay here is my complete controller.... ************************************************ class Contact extends Controller { function contact() { parent::Controller(); $this->load->library('database'); $this->load->helper(array('form', 'url')); $this->load->scaffolding( 'usercomment'); } function index() { // Produces: SELECT * FROM mytable $query = $this->db->get('usercomment'); //return $query; $this->load->view('contact_view',array('query'=>$query)); } function insert() { $data = array( 'fname' => $this->input->post('fname', FALSE), 'lname' => $this->input->post('lname', FALSE), 'email' => $this->input->post('email', FALSE), 'comment' => $this->input->post('comment', FALSE), ); $this->db->insert('usercomment', $data); /** Produces: INSERT INTO usercomment (fname, lname, email, comment) VALUES ('First Name', 'Last Name', 'Email', 'Comment')**/ $this->load->view('insert_contact_view'); } function update() { error_reporting(0); $this->load->helper('form'); // $id = $this->uri->segment(3); $query = $this->db->getwhere('usercomment', array('id' => $id)); $data['id'] = $query->result(); if ( count($_POST) > 0 ) { $this->db->where('id', $id); $this->db->update('usercomment', $_POST); redirect(''); } else { $this->load->view('update_contact_view', $data); } } function delete($id = -1) { $this->db->delete('usercomment', array('id' => $id)); *************************************************************** Yes my database has id set to auto... But still dont see the problem... my form open seems to fail...
[eluser]crumpet[/eluser]
put form helper in autoload so you know its loaded then you can see if you have the right syntax remember you need a function to display the form to put in a new post and a seperate function to actually put the information in the database i recommend insert() //displays insert form do_insert() //puts the form data in the database and then redirects to the view page
[eluser]ealonw[/eluser]
right.... thats what i was trying to do with this $this->load->view(’update_contact_view’, $data); that would take the data and load it into that view... after that i would have to work on updating that data to the database again but i cant get passed getting the previously posted data back to the form... HELP....
[eluser]Pascal Kriete[/eluser]
[ code ] tags encourage more meaningful replies. There is a really easy solution to this. Here's the general idea. Controller: Code: function edit($id) View: Code: <?=form_open('comment/edit/'.$c->comment_id)?> And the update function of the model - I trust you can do the get part. Model: Code: function update($id) If you want something more in depth - http://ellislab.com/forums/viewthread/81725/]request it .
[eluser]ealonw[/eluser]
Much progress!! thank you guys so much... one problem left though.. whn click the EDIT link it repopulates the form with only the first row of the database and not the row I select... strange... here's updated the code.. ***************************************** function update($id=NULL) { $this->load->helper('form'); // $id = $this->uri->segment(2); $query = $this->db->getwhere('myname', array('id', $id)); $data['name'] = $query->row(); if ( count($_POST) > 0 ) { $this->db->where('name', $name); $this->db->update('myname', $_POST); redirect(''); } else { $this->load->view('update_name_view', $data); } } } ************************************************************ <?= form_open('name/update/'.$name->id); ?> First Name <input type="text" name="name" maxlength="20" value="<?=$name->name?>"> <br><br> <input type="submit" name="submit" value="submit"> </form> <?=form_close()?>
[eluser]crumpet[/eluser]
sounds like it could be one of two things 1.The function isn't receiving the right id 2. your database has multiple entries with the same id try running die(print_r($query->result()); to see if you are getting only one row (row() only returns the first row of the query).
[eluser]ealonw[/eluser]
Array ( [0] => stdClass Object ( [id] => 22 [name] => eeeeeee ) [1] => stdClass Object ( [id] => 23 [name] => wwwwwww ) ) okay it echoed out all rows... but when i change the query->row() to query->result() I get this error: Message: Trying to get property of non-object Filename: views/update_name_view.php Line Number: 10 this line here----><?= form_open('name/update/'.$name->id); ?> Does anyone know why im getting this error?
[eluser]crumpet[/eluser]
when you use result() you need to use a foreach statement result() returns multiple rows so it doesn't know which query you are talkng about http://ellislab.com/codeigniter/user-gui...sults.html read this |
Welcome Guest, Not a member yet? Register Sign In |