Posts

Showing posts with the label Global

Change Button Color OnClick

Answer : There are indeed global variables in javascript. You can learn more about scopes, which are helpful in this situation. Your code could look like this: <script> var count = 1; function setColor(btn, color) { var property = document.getElementById(btn); if (count == 0) { property.style.backgroundColor = "#FFFFFF" count = 1; } else { property.style.backgroundColor = "#7FFF00" count = 0; } } </script> Hope this helps. 1. function setColor(e) { var target = e.target, count = +target.dataset.count; target.style.backgroundColor = count === 1 ? "#7FFF00" : '#FFFFFF'; target.dataset.count = count === 1 ? 0 : 1; /* () : ? - this is conditional (ternary) operator - equals if (count === 1) { target.style.backgroundColor = "#7FFF00"; target.dataset.count = 0; } else { ...

CodeIgniter - Best Place To Declare Global Variable

Answer : There is a file in /application/config called constants.php I normally put all mine in there with a comment to easily see where they are: /** * Custom defines */ define('blah', 'hello mum!'); $myglobalvar = 'hey there'; After your index.php is loaded, it loads the /core/CodeIgniter.php file, which then in turn loads the common functions file /core/Common.php and then /application/constants.php so in the chain of things it's the forth file to be loaded. I use a "Globals" class in a helper file with static methods to manage all the global variables for my app. Here is what I have: globals_helper.php (in the helpers directory) <?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); // Application specific global variables class Globals { private static $authenticatedMemberId = null; private static $initialized = false; private static function initialize() { ...

Alternative For Define Array Php

Answer : From php.net... The value of the constant; only scalar and null values are allowed . Scalar values are integer, float, string or boolean values. It is possible to define resource constants, however it is not recommended and may cause unpredictable behavior. But You can do with some tricks : define('names', serialize(array('John', 'James' ...))); & You have to use unserialize() the constant value (names) when used. This isn't really that useful & so just define multiple constants instead: define('NAME1', 'John'); define('NAME2', 'James'); .. And print like this: echo constant('NAME'.$digit); This has changed in newer versions of PHP, as stated in the PHP manual From PHP 5.6 onwards, it is possible to define a constant as a scalar expression, and it is also possible to define an array constant .