CodeIgniter Forums
Weird problem with... php - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Weird problem with... php (/thread-11886.html)



Weird problem with... php - El Forum - 09-26-2008

[eluser]lopetzi[/eluser]
Hello,

I have a piece of code (controller):

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {
    
    function index()
    {    
        function check_txn_id($txnid)
            {
                $i = $this->db->select('id')->from('table')->where('txnid', $txnid)->get();
                return $var = ($i->num_rows() > 0) ? true : false;
            }
            
        if (check_txn_id('inexistent_id') == true) echo 'The function should return true'; else echo 'The function should return false';

        echo 'This should be echoed anyway... but it\'s not.';
    }

}

I built a function (check_txn_id) to check if I have an ID in the database. If the ID exists I want it to return false, otherwise it should return true.

The weird thing is that it doesn't return anything, no matter if the ID exists or not.
It just shows a blank page and the script stops.
I don't know where the problem is... I'm not using the "if" correctly ?

Even if I do:

Code:
function check_txn_id($txnid)
            {
                return true
            }

It won't work... so I guess the function is not the problem... I don't know.

Please help me! Smile

Thanks!

PS: the database library is autoloaded


Weird problem with... php - El Forum - 09-26-2008

[eluser]Colin Williams[/eluser]
Functions cannot be defined inside a method (at least not reliably, or for any reason). Instead of a function, write a method:

Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Test extends Controller {
    
    function index()
    {    
            
        if ($this->_check_txn_id('inexistent_id') == true) echo 'The function should return true'; else echo 'The function should return false';

        echo 'This should be echoed anyway... but it\'s not.';
    }

}

function _check_txn_id($txnid)
            {
                $i = $this->db->select('id')->from('table')->where('txnid', $txnid)->get();
                return $var = ($i->num_rows() > 0) ? true : false;
            }

Better yet, have this method in a Model, since it deals with stored data


Weird problem with... php - El Forum - 09-26-2008

[eluser]lopetzi[/eluser]
It works now.

Thank you!