Hello,

I am still having problem with this basic stuff in CI:

viewlatihanci.php

<button type="button" onClick="parent.location='cadd_latihan/'">Add Latihan</button><br><br>

routes.php

$route['default_controller'] = "latihanci";
$route['cadd_latihan'] = "cadd_latihan";
$route['scaffolding_trigger'] = "";

cadd_latihan.php

<?php
class Cadd_latihan extends Controller {

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

}
?>

I wonder why when someone press the button: Add Latihan, this error appears:

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.

------------------

cadd_latihan.php --> script suppose to work!

If you write:

onClick="parent.location='cadd_latihan/'"

your link is relative to the page in which this is clicked, so if you open it in /home/ the client asks for /home/cadd_latihan/, if you click it in /contact/ the client asks for /contact/cadd_latihan/. To fix it, add a leading slash to the url and it should work:

onClick="parent.location='/cadd_latihan/'"

@cereal: if I click the button, it carries me to this url - http://localhost/cadd_latihan/

and I still have:

Object not found!

The requested URL was not found on this server. The link on the referring page seems to be wrong or outdated. Please inform the author of that page about the error.

If you think this is a server error, please contact the webmaster.
Error 404
localhost
Apache/2.4.7 (Win32) OpenSSL/0.9.8y PHP/5.4.22

Ops! There's still an error, change:

class Cadd_latihan extends Controller

To:

class Cadd_latihan extends CI_Controller

@cereal: I think

class Cadd_latihan extends Controller

still correct, since I am still using the old version.

As long as my program works that's all I need.

The same error still remains though.

It means you're using version 1.7.2?

My opinion: since you're using CI it is highly convenient to switch to the last version, there are not much changes to do to controllers and models except adding the prefix CI_. Between 1.7.* and 2.2.* there is a huge list of improvements and bug fixes.

Here's the changelog:

If you were using something else, lets say Laravel 3.* then I would understand your point, the latest versions are really something else, not compatible at all.

/opinion

Now, regarding your problem I can only suggest to check if file name for this controller has the underscore and it's all lowercase, if this does not help, then try to remove the underscore from the file name and from the class name. Or simply try to replace this file with a new one.

This is strange I still have similar error.

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='/ci/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>
</tr>    
<?php endforeach; ?>
</table>
</body>

</html>

caddLatihan.php

<?php
class CaddLatihan extends Controller {

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

}
?>

viewlatihan2ci.php

<html>

    <head>
        <title>File View untuk Latihan 2 CI </title>
    </head>

    <body>

    <?php foreach($data as $row):?>
    <table>
    <tr>
    <td><h3><?php echo $row->judul;?></h3></td>
    <td><div><?php echo $row->konten;?></div></td>
    </tr>
    </table>
    <?php endforeach; ?>

    </body>

</html>

routes.php

$route['default_controller'] = "latihanci";
$route['cadd_latihan'] = "caddLatihan";
$route['scaffolding_trigger'] = "";

Davy:

  1. are you able to directly open this link?
  2. do you experience this problem just with these two controllers or it is a common issue to all controllers?

I see you're using a different link now:

/ci/caddLatihan/

If your CI installation is inside /ci/ then you should edit your .htaccess files in order to reflect the path.

Now, I only routes 2 controllers in my routes: ci and caddLatihan. (only ci works)

ci is my CI installation directory. How to edit my .htaccess?

Looks like I am using CI 1.7.2 temporarily.

where is the .htaccess located?

I try to insert:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

in order to delete the index.php

So you have a ci controller and a /ci/ directory in which you installed CodeIgniter, when you try to access your ci controller you do something like this:

http://website.tld/ci/ci/

Correct?

I only routes 2 controllers in my routes: ci and caddLatihan. (only ci works)

If it was the .htaccess then your ci controller should not work either, so do not change it. Are you sure caddLatihan is not considered a method of the ci controller?

Nevermind.

I migrate to the newest version of CI - CI3:

Now, I am having this error:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Latihanci::$db

Filename: core/Model.php

Line Number: 77

Backtrace:

File: C:\xampp\htdocs\ci3\application\models\modellatihanci.php
Line: 6
Function: __get

File: C:\xampp\htdocs\ci3\application\controllers\latihanci.php
Line: 6
Function: tangkapdb

File: C:\xampp\htdocs\ci3\index.php
Line: 292
Function: require_once

Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\ci3\application\models\modellatihanci.php on line 6
A PHP Error was encountered

Severity: Error

Message: Call to a member function get() on a non-object

Filename: models/modellatihanci.php

Line Number: 6

Backtrace:

modellatihanci.php

<?php 

class Modellatihanci extends CI_Model {

    function tangkapdb() {
    $tangkap = $this->db->get("latihan");

    if($tangkap->num_rows() > 0) {

        foreach($tangkap->result() as $row) {
            $data[] = $row;
        }   
    return $data;   
    }   
    }
}


?>

line 6: $tangkap = $this->db->get("latihan");

The current stable version is 2.2.1, version 3 is still in development status, it is not stable and probably not even complete, but I'm not sure of this havent tried yet... for more information refer to their documentation:

ok, I re-migrate to CI 2.2.1

Here is my errors:

A PHP Error was encountered

Severity: Notice

Message: Undefined property: Latihanci::$db

Filename: core/Model.php

Line Number: 52

Fatal error: Call to a member function get() on a non-object in C:\xampp\htdocs\ci2\application\models\modellatihanci.php on line 6

modellatihanci.php

<?php 

class Modellatihanci extends CI_Model {

    function tangkapdb() {
    $tangkap = $this->db->get("latihan");

    if($tangkap->num_rows() > 0) {

        foreach($tangkap->result() as $row) {
            $data[] = $row;
        }   
    return $data;   
    }   
    }
}


?>

line 6: $tangkap = $this->db->get("latihan");

Good! Now, are you loading the database library in your application/config/autoload.php file? For example:

$autoload['libraries'] = array('database', 'session');

ok, I get that part done.

Now, another thing is I am trying to create news item:

controllers/caddLatihan.php

<?php
class CaddLatihan extends CI_Controller {

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

  public function create()
{
    $this->load->helper('form');
    $this->load->library('form_validation');

    $data['title'] = 'Create a news item';

    $this->form_validation->set_rules('title', 'Title', 'required');
    $this->form_validation->set_rules('text', 'text', 'required');

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

    }
    else
    {
        $this->news_model->set_news();
        $this->load->view('news/success');
    }
}

viewlatihan2ci.php

<html>

    <head>
        <title>File View untuk Latihan 2 CI </title>
    </head>

    <body>

    <?php foreach($records as $row):?>
    <h3><?php echo $row->id;?></h3>
    <div><?php echo $row->isi;?></div>
    <?php endforeach; ?>

    </body>

    <h2>Create a news item</h2>


<?php echo form_open('CaddLatihan/create') ?>

    <label for="title">Title</label>
    <input type="input" name="title" /><br />

    <label for="text">Text</label>
    <textarea name="text"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>


</html>

and still get this error:

Create a news item

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

-----------------------

How to fix the error?

Load the Form helper into the index() method, at the moment you're loading it only into the create() method:

public function index()
{
    $this->load->helper('form');
    ...
}

Or add the constructor to the controller, so you make it available for all the methods:

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

viewlatihan2ci.php

<html>

    <head>
        <title>File View untuk Latihan 2 CI </title>
    </head>

    <body>

    <?php foreach($records as $row):?>
    <h3><?php echo $row->id;?></h3>
    <div><?php echo $row->isi;?></div>
    <?php endforeach; ?>

    </body>

    <h2>Create Content</h2>


<?php echo form_open('CaddLatihan/create') ?>

    <label for="title">Id</label>
    <input type="input" name="id" /><br />

    <label for="text">Isi</label>
    <textarea name="isi"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>


</html>

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->news_model->set_news();
        $this->load->view('news/success');
    }
}

}
?>

This time, it is strange enough that afterI press the submit button it carries me to this url: http://localhost/ci2/index.php/caddLatihan/welcome/index.php/CaddLatihan/create

Instead of processing and saving the data.

It happens because you're using a relative path, instead of absolute, i.e. this:

echo form_open('CaddLatihan/create')

instead of an absolute path:

echo form_open('/CaddLatihan/create')

Some additional info you should really read:

Member Avatar for iamthwee

Never use absolute paths, it's a nightmare to migrate or if your directory changes structure and suddenly needs to be placed within another folder.

When I see code that uses them I cry a little as a small kitten has just died somewhere.

@iamthwee

hi!

in relation to URL links an absolute path is relative to the domain, so you can migrate to another domain without problems:

<form action="/send/email" method="post">

The above will be the same under http://domain.old/ or http://domain.new/.

In relation to the system directories, instead, you can use the DOCUMENT_ROOT so nothing really changes:

include $_SERVER['DOCUMENT_ROOT'] . '/common/navigation.php';

For backend scripts, which will run on command line, cron & co., I use configuration files because the scrips are outside the document root and will run separated by the Apache/Nginx instance, something like:

<?php

    return array(

        'uploads' => '/srv/path/to/uploads/',

    );

And on script:

<?php

    $paths = require '/config/paths.php';
    echo $paths['uploads'];

Even after I change the path into:

viewlatihan2ci.php

<html>

    <head>
        <title>File View untuk Latihan 2 CI </title>
    </head>

    <body>

    <?php foreach($records as $row):?>
    <h3><?php echo $row->id;?></h3>
    <div><?php echo $row->isi;?></div>
    <?php endforeach; ?>

    </body>

    <h2>Create Content</h2>


<?php echo form_open('/CaddLatihan/create') ?>

    <label for="title">Id</label>
    <input type="input" name="id" /><br />

    <label for="text">Isi</label>
    <textarea name="isi"></textarea><br />

    <input type="submit" name="submit" value="Create news item" />

</form>


</html>

It still carries me to this url after I press submit: http://localhost/ci2/index.php/caddLatihan/welcome/index.php/CaddLatihan/create

@davy what is the output of this?

<?php echo form_open('/CaddLatihan/create'); ?>

It should be:

http://localhost/ci2/CaddLatihan/create

If not, then it can depend on the configuration of the base_url in your /application/config/config.php file:

$config['base_url']

which should be the domain name, or as in your case:

$config['base_url'] = 'http://localhost/ci2/';

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->news_model->set_news();
        $this->load->view('news/success');
    }
}

}
?>

An Error Was Encountered

Unable to load the requested file: news/success.php

I wonder how to fix the error? Basically what I am trying to do is that when the user insert the form create content like Id and Isi, I expect something being added to the database.

The error refers to line 34 or your controller code, it means it does not find the declared view so, create application/views/news/success.php and you solve the problem.

Okay, I successfully create the success message for success.php - Now, I am still wonder how to create a system to record the new data into the database (id & isi)?

I try this:

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('modelBlogModel');
        $this->load->view('success');
    }
}

}
?>

models/modelBlogModel.php

<?php


class Blogmodel extends CI_Model {

    var $title   = '';
    var $content = '';
    var $date    = '';

    function __construct()
    {
        // Call the Model constructor
        parent::__construct();
    }

    function get_last_ten_entries()
    {
        $query = $this->db->get('entries', 10);
        return $query->result();
    }

    function insert_entry()
    {
        $this->title   = $_POST['title']; // please read the below note
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->insert('entries', $this);
    }

    function update_entry()
    {
        $this->title   = $_POST['title'];
        $this->content = $_POST['content'];
        $this->date    = time();

        $this->db->update('entries', $this, array('id' => $_POST['id']));
    }

}


?>

Fatal error: Class 'Modelblogmodel' not found in C:\xampp\htdocs\ci2\system\core\Loader.php on line 304

It's easy, create application/models/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('news', $this);
    }

}

Then in the controller load the news model and add the method, in practice change line 33 with this:

$this->load->model('News_model', 'news');
$this->news->set_news();

The insert method will return boolean TRUE on success. You can find more information about models and active record here:

Ok, it's almost fine, just call the method to insert the data, as in my above example. In your case, replace line 33 of your controller with:

$this->load->model('Blogmodel');
$this->Blogmodel->insert_entry();

And it should work fine. Also: use $this->input->post() instead of $_POST, CI will sanitize the data through the input class.

Last: don't use var $abc to declare a property (variable) in a class, the documentation refers to old PHP versions, you should declare the visibility of the property, so:

public $title = '';
public $content = '';
public $date = '';
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.