• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Syntax in function does not work

#11
(11-29-2014, 05:10 PM)Rufnex Wrote: You have called the row() method wrong, try this:


PHP Code:
function test() {
 
 $test $this->db->query("SELECT name FROM users");
 
 $name $test->row()->name;s e
  return $name
;


I apologise, I thought it correct, I wrote wrong.
This example does not work:

function test() {
$test = $this->db->query("SELECT name FROM users");
$name = $test->row()->name;
return $name;
}

echo test();

The error is same:

Fatal error: Using $this when not in object context in /folder/with/my/project/admin.php on line 248
Reply

#12
You have no class defined, so you cannot use the this keyword. This is the way to build a controller (but the db stuff should come from a model).

PHP Code:
class Example extends CI_Controller {

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

    public function 
index()
    {
       
$test $this->db->query("SELECT name FROM users"); // you should hava a where clause to fetch on dataset
       
echo $test->row()->name;                            // or call the result by a loop
    
}


You call this with yoursite.tld/example

Reply

#13
(11-29-2014, 05:25 PM)Rufnex Wrote: You have no class defined, so you cannot use the this keyword. This is the way to build a controller (but the db stuff should come from a model).


PHP Code:
class Example extends CI_Controller {

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

 
   public function index()
 
   {
 
      $test $this->db->query("SELECT name FROM users"); // you should hava a where clause to fetch on dataset
 
      echo $test->row()->name                           // or call the result by a loop
 
   }


You call this with yoursite.tld/example


Ou...yes, it works. But I need to use more queries and I want them to use in all files.
For example:

I have files called header.php, content.php, footer.php, function.php.
I want to use in all files function(). Without function() it works correctly.

Is it possible set up it globally?
Reply

#14
I dont understand exactly what you try to do. You have to go with the MVC pattern with CI. So all of your database stuff should organized in a model like

PHP Code:
class Example_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.


Then in all of your controller files you can call your model methods like that

PHP Code:
class Example extends CI_Controller {

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

    public function 
index()
    {
       
$this->example_model->get('users');
    }


Reply

#15
I apologise, I am beginner with CodeIgniter.

Your example works...but I must define a controller.
I try it clarify again.

1) Download current version CodeIgniter
2) Define your application/config/database.php
3) Define application/config/autoload
Change
$autoload['libraries'] = array();
..to..
$autoload['libraries'] = array('database');

4) Edit application/views/welcome_message.php and put the lines:

$this->load->database();

function test_codeigniter_syntax() {
      $test = $this->db->query("YOUR SIMPLY QUERY");
      echo $test->row()->yourColumn;
}

//Call function
test_codeigniter_syntax(); // IT DOES NOT WORK ME


Step 4 again and correct:

Edit application/views/welcome_message.php and put the lines:

$this->load->database();

function test_standard_syntax() {
      $test = mysql_query("YOUR SIMPLY QUERY");
      $test = mysql_result($test, "0", "yourColumn");
}

//Call function
test_standard_syntax(); // IT WORKS ME

My question is - why? I want to use CodeIgniter syntax.


Or.....Does it use function() in CodeIgniter else anyhow? For example over controller/model?
Reply

#16
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

#17
WOW ...This boy knows what says :-)
GREAT, I understand. Thanks very much.

..but I will be to have more questions later :-)

Thanks very much for best practise..
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2019 MyBB Group.