0

ok, I revise my code into this:

controllers/caddLatihan.php

<?php
class CaddLatihan extends CI_Controller {

  public function __construct()
  {
      parent::__construct();
      $this->load->helper('form');
  }

  public function index() {
    $this->load->model('modellatihanci');
    $data['records'] = $this->modellatihanci->tangkapdb();
    $this->load->view('viewlatihan2ci', $data);
  }

  public function create()
  {

    $this->load->library('form_validation');

    $data['title'] = 'Create content';

    $this->form_validation->set_rules('id', 'id', 'required');
    $this->form_validation->set_rules('isi', 'isi', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('viewlatihan2ci', $data);

    }
    else
    {
        $this->load->model('News_model');
        $this->load->view('success');
    }
}

}
?>

News_model.php

<?php

class News_model extends CI_Model
{
    public $id;
    public $isi;

  public function __construct()
    {
    parent::__construct();
    }

  public function set_news()
    {
    $this->id = $this->input->post('id');
    $this->isi = $this->input->post('isi');
    return $this->db->insert('latihan', $this);
    }

}

?>  

database: ci

table: latihan

id  isi
1   contoh latihan pertama
2   contoh latihan kedua

I still do not see the row being added even after I add a new row by filling in the form.

0

In your controller change:

else
{
    $this->load->model('News_model');
    $this->load->view('success');
}

To:

else
{
    $this->load->model('News_model');

    $this->News_model->set_news();  # <- you have to call this

    $this->load->view('success');
}
0

Thanks I get that part done. Now, I am trying to add delete function but have not get it to work.

News_model.php

<?php

class News_model extends CI_Model
{
    public $id;
    public $isi;

  public function __construct()
    {
    parent::__construct();
    }

  public function set_news()
    {
    $this->id = $this->input->post('id');
    $this->isi = $this->input->post('isi');
    return $this->db->insert('latihan', $this);
    }

  public function delete_news()
    {
    $this->id = $this-delete->post('id');
    $this->isi = $this-delete->post('isi');
    return $this->db->delete('latihan', $this); 
    }

}

?>  

cdelete.php

<?php
class Cdelete extends CI_Controller {

  public function __construct()
  {
      parent::__construct();
      $this->load->helper('form');
  }

  public function index() {
    $this->load->model('modellatihanci');
    $data['records'] = $this->modellatihanci->tangkapdb();
    $this->load->view('viewlatihan2ci', $data);
  }

  public function delete()
  {

    $this->load->library('form_validation');

    $data['title'] = 'Create content';

    $this->form_validation->set_rules('id', 'id', 'required');
    $this->form_validation->set_rules('isi', 'isi', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('viewlatihan2ci', $data);

    }
    else
    {
        $this->load->model('News_model');
        $this->News_model->delete_news(); # <- you have to call this
        $this->load->view('success');
    }
}

viewlatihanci.php

<html>

<head>
    <title>File view untuk menampilkan data</title>
</head>

<body>
    File view sudah bisa ditampilkan dengan baik.
<br><br>


<button type="button" onClick="parent.location='/ci2/index.php/caddLatihan/'">Add Latihan</button><br><br>


<table border="1px"> 
<?php foreach($records as $row): ?>
<tr>
    <td><?php echo $row->id; ?></td>
    <td><?php echo $row->isi; ?></td>
    <td><button type="button" onClick="parent.location='/ci2/index.php/cdelete/'">Delete</button></td>
</tr>    
<?php endforeach; ?>
</table>



</body>

</html>

After I press the Delete button, I wonder why there is no data being deleted?

Also, does $this refers to --> the database ? I wonder

0

Here there is an error:

$this->id = $this-delete->post('id');
$this->isi = $this-delete->post('isi');

You're trying to access the input, so you must use the input class:

$this->input->post('field_name');

So, change the line 22 and 23 in your delete() method model to:

$this->id = $this->input->post('id');
$this->isi = $this->input->post('isi');

Also, does $this refers to --> the database ? I wonder

$this is a pseudo-variable used to access parent and self methods and properties, in CodeIgniter it gives you the ability to access the shared resources (libraries, helpers) loaded at each request. For more information read this:

Edited by cereal

0

I try that. Still my database remains the same. It seems like it doesn't want to delete the row that I choose.

0

Yes, it happens because of your view page, you are not sending the id through any form, in order to work you have to change few things:

  • add a form for each delete button;
  • save the article id into an hidden input field;
  • modify the delete_news() method model to receive only the id, since isi seems to be a text field you need only the id.

So this:

<?php foreach($records as $row): ?>
<tr>
    <td><?php echo $row->id; ?></td>
    <td><?php echo $row->isi; ?></td>
    <td><button type="button" onClick="parent.location='/ci2/index.php/cdelete/'">Delete</button></td>
</tr>    
<?php endforeach; ?>

Becomes:

<?php foreach($records as $row): ?>
<tr>
    <td><?php echo $row->id; ?></td>
    <td><?php echo $row->isi; ?></td>
    <td>
        <?php echo form_open('/cdelete/delete'); ?>
        <input type="hidden" name="id" value="<?php echo $row->id; ?>" />
        <button type="submit">Delete</button>
        </form>
    </td>
</tr>    
<?php endforeach; ?>

Note: I added the controller method to the url, you were pointing the index, that way the request will never work, the updated version will call the delete() method of the controller:

/cdelete/delete

The model:

public function delete_news()
{
    $this->id = $this-delete->post('id');
    $this->isi = $this-delete->post('isi');
    return $this->db->delete('latihan', $this); 
}

Becomes:

public function delete_news()
{
    $this->id = $this-delete->post('id');
    return $this->db->delete('latihan', $this); 
}

And don't forget to fix the validation rules in your controller, you don't need to check for isi anymore, so remove line 24 in your controller:

$this->form_validation->set_rules('isi', 'isi', 'required');
0

this error appears: Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\ci2\application\views\viewlatihanci.php on line 21

which replace the delete button.

0

@davy this is the same error of last week, are you loading the form helper in the Cdelete controller? It returns undefined function because it does not read the resource.

Besides, it was late when I posted the fixes and I made a mistake in the model, change the line marked wrong:

public function delete_news()
{
    $this->id = $this-delete->post('id'); # <- wrong
    return $this->db->delete('latihan', $this); 
}

With this:

$this->id = $this->input->post('id'); # <- correct
0

viewlatihanci.php

<html>

<head>
    <title>File view untuk menampilkan data</title>
</head>

<body>
    File view sudah bisa ditampilkan dengan baik.
<br><br>


<button type="button" onClick="parent.location='/ci2/index.php/caddLatihan/'">Add Latihan</button><br><br>


<table border="1px"> 
<?php foreach($records as $row): ?>
<tr>
    <td><?php echo $row->id; ?></td>
    <td><?php echo $row->isi; ?></td>
    <td>
    <?php echo form_open('/cdelete/delete'); ?>
    <input type="hidden" name="id" value="<?php echo $row->id; ?>" />
    <button type="submit">Delete</button>
    </form>
    </td>
</tr>    
<?php endforeach; ?>
</table>



</body>

</html>

News_model.php

<?php

class News_model extends CI_Model
{
    public $id;
    public $isi;

  public function __construct()
    {
    parent::__construct();
    }

  public function set_news()
    {
    $this->id = $this->input->post('id');
    $this->isi = $this->input->post('isi');
    return $this->db->insert('latihan', $this);
    }

  public function delete_news()
    {
    $this->id = $this-input->post('id');
    $this->isi = $this-input->post('isi');
    return $this->db->delete('latihan', $this); 
    }

}

?>  

I already fix that part.

Fatal error: Call to undefined function form_open() in C:\xampp\htdocs\ci2\application\views\viewlatihanci.php on line 21

line 21: <?php echo form_open('/cdelete/delete'); ?>

Edited by davy_yg

1

@davy this is the same error of last week

Heh heh. 40 posts later. Going for the record?

Edited by diafol

0

Heh heh. 40 posts later. Going for the record?

Heh! just trying! :D

@davy can you show us your Cdelete controller?

0

cdelete.php

<?php
class Cdelete extends CI_Controller {

  public function __construct()
  {
      parent::__construct();
      $this->load->helper('form');
  }

  public function index() {
    $this->load->model('modellatihanci');
    $data['records'] = $this->modellatihanci->tangkapdb();
    $this->load->view('viewlatihan2ci', $data);
  }

  public function delete()
  {

    $this->load->library('form_validation');

    $data['title'] = 'Create content';

    $this->form_validation->set_rules('id', 'id', 'required');
    $this->form_validation->set_rules('isi', 'isi', 'required');

    if ($this->form_validation->run() === FALSE)
    {
        $this->load->view('viewlatihan2ci', $data);

    }
    else
    {
        $this->load->model('News_model');
        $this->News_model->delete_news(); # <- you have to call this
        $this->load->view('success');
    }
}

}
?>
0

It seems correct to me.

Are you loading other helpers? Are you still using CaddLatihan controller? There you have a form to add the articles, which uses the same helper to open the form tag (which also adds a hidden csrf field). Is this still working?

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.