Welcome Guest, Not a member yet? Register   Sign In
Syntax in function does not work
#16

(This post was last modified: 12-01-2014, 03:08 PM by Rufnex.)

Ok, understand your question. You get an error because you have not a instance from CI. This must be done like that and is used normaly in helpers or libraries:

PHP Code:
<?php
    
#$this->load->database(); <-- you dont need this
    
function test_codeigniter_syntax()
    {
    
$CI =& get_instance(); // <-- here you get an instance for CI objects
    
$test $CI->db->query("select * from users where id = 1");
    echo 
$test->row()->id;
    }

    
//Call function
    
test_codeigniter_syntax(); // --IT DOES NOT WORK ME-- NOW it works 
?>

But again, this is not the way to use CI and its a realy bad practise. You should go with the MVC way as described now.

To step 3 is all well done. Now we come to step 4.

MVC
- You need a Controller (/application/controllers/). In your example the Welcome.php Controller
PHP Code:
class Welcome extends CI_Controller {

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

    public function 
index()
    {
       
$data['users'] = $this->example_model->get('users'1);
       
$this->load->view('welcome'$data);
    }


In this example you catch the row from database "users" with the id 1 and give the data to the welcome-View. In the view the data will be parsed and then displayed.


- You need a Model (/application/models/). In your example it could be Welcome_model.php
PHP Code:
class Welcome_model extends CI_Model {
    function 
__construct()
    {
    }

    public function 
get($table$id false)
    {
        if (
$id)
        {
            
$this->db->where('id'$id);
        }
        
$query $this->db->get($table);
        if(
$query->num_rows() > 0)
        {
            return 
$query->result_array();
        }
        return 
false;
    }

    
// etc.


This example Model has one method calles get() witch you can fetch data from a given table.

- You need the View (/application/views/). In you example the welcome.php View.
PHP Code:
...
<
h1>My Simple View</h1>
<?
php foreach($users as $user): ?>
    <?php echo $user['lastname']; ?><br />
<?php endforeach; ?>
... 

In the view you can make your template structure for the given data from the controller.

So if you have loaded the database class in your autoload.php you dont need to write $this->load->database(), beacuse its already loaded with the database configured in the database.php.

Reply


Messages In This Thread
Syntax in function does not work - by lkudlacek - 11-29-2014, 09:23 AM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 10:26 AM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 04:36 PM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 04:50 PM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 04:54 PM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 05:10 PM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 05:25 PM
RE: Syntax in function does not work - by Rufnex - 11-29-2014, 05:45 PM
RE: Syntax in function does not work - by Rufnex - 12-01-2014, 03:08 PM



Theme © iAndrew 2016 - Forum software by © MyBB