Welcome Guest, Not a member yet? Register   Sign In
trying to run function just once
#1

(This post was last modified: 06-21-2021, 09:38 AM by richb201.)

I have a function that needs to run just once. I thought that setting $var=1 and using if (isset($var)){} would work but it doesn't. How can I run a function just one time in CI3?
proof that an old dog can learn new tricks
Reply
#2

use flash data
Reply
#3

This should work for you, not tested.

PHP Code:
protected static $runOnce true;

if (
$runOnce === true)
{
    // run your code
}
else
{
    static::$runOnce false;

What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#4

(This post was last modified: 06-21-2021, 11:29 PM by richb201.)

(06-21-2021, 08:44 PM)InsiteFX Wrote: This should work for you, not tested.

PHP Code:
protected static $runOnce true;

if (
$runOnce === true)
{
    // run your code
}
else
{
    static::$runOnce false;

Thanks. Where does protected static $runOnce true; go? Can I put this in _init()?
proof that an old dog can learn new tricks
Reply
#5

(This post was last modified: 06-22-2021, 12:06 AM by John_Betong.)

@richb201 ,

I have not used static before and decided to give it a try... discovered it ONLY works on Class Methods(...).

The following KLUDGE should work which I have tried in:

CI4 -> app/Common.php -> function fred(...)

Code:
//==================================================
function fred($val='Nothing set???', mixed $title='NO TITLE')
: string
{
  if( defined('CALL_ONCE_ONLY') ) :
    echo '<br> PREMATURE FUNCTION RETURN AND NOT USED';
    return '';
  endif;
  define('CALL_ONCE_ONLY', 'pretty please');


  $results = print_r($val, TRUE);

  echo $tmp = <<< ____EOT
    <div> <br> </div>
      <dl class="w88 mga fss bgl bd1 p42" >
        <dt> <b> $title </b> ==> </dt>
        <dd>
          <pre class="tal wrp bga">
            <b> $title </b> ==>
            <br>            
            $results
          </pre>
        </dd>  
      </dl>  
____EOT;

  return $result=(string) '';
}//
Reply
#6

If you are not inside a class context, you can use static variables instead.
PHP Code:
static $hasRun;

if (
$hasRun !== true)
{
    
$hasRun true;

    
// run your function here

Reply
#7

Its amazing topic


function foo()
{
static $foo_called = false;
if (!$foo_called) {
$foo_called = true;
// etc.
}
}
Enlightenment  Is  Freedom
Reply
#8

(This post was last modified: 06-22-2021, 02:16 PM by richb201.)

(06-22-2021, 02:03 AM)paulbalandan Wrote: If you are not inside a class context, you can use static variables instead.
PHP Code:
static $hasRun;

if (
$hasRun !== true)
{
    $hasRun true;

    // run your function here
}

I tried thisI tried placing $hasRun above class Configure extends CI_Controller but I get undefined variableThen I placed it between 
class Configure extends CI_Controller
{
    static $hasRun;    <<<<<<<<here
    
public function __construct()

And 
i still get undefined variable.
I put the if ($hasRun !== truein the __construct() 

(06-22-2021, 04:19 AM)paliz Wrote: Its amazing topic


function foo()
{
    static $foo_called = false;
    if (!$foo_called) {
        $foo_called = true;
        // etc.
    }
}
The problem is that foo runs each time.

(06-22-2021, 12:04 AM)John_Betong Wrote: @richb201 ,

I have not used static before and decided to give it a try... discovered it ONLY works on Class Methods(...).

The following KLUDGE should work which I have tried in:

CI4 -> app/Common.php -> function fred(...)

Code:
//==================================================
function fred($val='Nothing set???', mixed $title='NO TITLE')
: string
{
  if( defined('CALL_ONCE_ONLY') ) :
    echo '<br> PREMATURE FUNCTION RETURN AND NOT USED';
    return '';
  endif;
  define('CALL_ONCE_ONLY', 'pretty please');


  $results = print_r($val, TRUE);

  echo $tmp = <<< ____EOT
    <div> <br> </div>
      <dl class="w88 mga fss bgl bd1 p42" >
        <dt> <b> $title </b> ==> </dt>
        <dd>
          <pre class="tal wrp bga">
            <b> $title </b> ==>
            <br>           
            $results
          </pre>
        </dd> 
      </dl> 
____EOT;

  return $result=(string) '';
}//

Following your lead I stuck this in the bottom of _init()
if(defined('RUNONCE')) {
}
else {
    $this->get_from_locker();  //bring down this guy's images
    define ('RUNONCE','YES');
}


Unfortunately it keeps running get_from_locker() each time. 
proof that an old dog can learn new tricks
Reply
#9

Once per what? Once per day? Once per page load? What are you trying to accomplish? If you don't need to run it more than one time, in which context is it called repeatedly?
CodeIgniter 4 tutorials (EN/FR) - https://includebeer.com
/*** NO support in private message - Use the forum! ***/
Reply
#10

The script I supplied is PHP.

Where or what is _init()
Reply




Theme © iAndrew 2016 - Forum software by © MyBB