CodeIgniter Forums
Having problems with creating a CRUD.. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Having problems with creating a CRUD.. (/showthread.php?tid=8925)

Pages: 1 2 3


Having problems with creating a CRUD.. - El Forum - 06-10-2008

[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="&lt;?php if($i%2 == '0'): echo "alt"; else: echo "row"; endif; ?&gt;">

<td class="td-center">&lt;?php echo $row->fname;?&gt;</td>
<td class="td-center">&lt;?php echo $row->lname;?&gt;</td>
<td class="td-center">&lt;?php echo $row->email; ?&gt;</td>
<td class="td-center">&lt;?php echo $row->comment;?&gt;</td>
<td class="td-center"><a href="&lt;?=site_url("contact/update/$row->fname");?&gt;">Edit</a>&nbsp;<a href="&lt;?=site_url("contact/delete/$row->fname");?&gt;">Delete</a></td>
</tr>
&lt;?php
endforeach;
endif;
?&gt;


Having problems with creating a CRUD.. - El Forum - 06-10-2008

[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


Having problems with creating a CRUD.. - El Forum - 06-10-2008

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


Having problems with creating a CRUD.. - El Forum - 06-10-2008

[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


Having problems with creating a CRUD.. - El Forum - 06-10-2008

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


Having problems with creating a CRUD.. - El Forum - 06-10-2008

[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)
{
    $this->load->helper('form');
    $this->load->model('comments');

    // Grab the comment with $id from the db
    $data['c'] = $this->comments->get_comment($id);

    // Validation Rules
    $rules['comment']    =>    'trim|required';
    $this->validation->set_rules($rules);
    
    // Validation Fields
    $fields['comment'] = 'Comment';
    $this->validation->set_fields($fields);
    
    // Fire
    if ( $this->validation->run() == FALSE )
    {
        // Show it
        $this->load->view('comments/edit', $data);
    }
    else
    {
        // Form posted & no errors - update
        $this->comments->update($id);
        $this->session->set_flashdata('msg', 'Comment Updated');
        redirect('');
    }
}

View:
Code:
&lt;?=form_open('comment/edit/'.$c->comment_id)?&gt;

&lt;textarea name="comment" rows="8" cols="40"&gt;&lt;?=($this->validation->comment) ? $this->validation->comment : $c->comment?&gt;&lt;/textarea&gt;

&lt;?=form_close()?&gt;

And the update function of the model - I trust you can do the get part.

Model:
Code:
function update($id)
{    
    $fields['comment_body'] = $this->input->post('comment');
    
    $this->db->set($fields);
    $this->db->where('comment_id', $id);
    $this->db->update('comment_table');
}

If you want something more in depth - http://ellislab.com/forums/viewthread/81725/]request it Wink .


Having problems with creating a CRUD.. - El Forum - 06-11-2008

[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);
}
}
}
************************************************************
&lt;?= form_open('name/update/'.$name->id); ?&gt;
First Name
&lt;input type="text" name="name" maxlength="20" value="&lt;?=$name-&gt;name?&gt;">
<br><br>
&lt;input type="submit" name="submit" value="submit"&gt;
&lt;/form&gt;
&lt;?=form_close()?&gt;


Having problems with creating a CRUD.. - El Forum - 06-11-2008

[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).


Having problems with creating a CRUD.. - El Forum - 06-12-2008

[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----&gt;&lt;?= form_open('name/update/'.$name->id); ?&gt;

Does anyone know why im getting this error?


Having problems with creating a CRUD.. - El Forum - 06-12-2008

[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-guide/database/results.html
read this