• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
" Undefined variable" message when I am trying to create custom library.

#2
[eluser]TWP Marketing[/eluser]
[quote author="rajender jakka" date="1344351668"]This is my library
Code:
class Testing extends CI_Loader {

    function __construct() {

function randomtxt($n){
   $codelenght = $n; // spelling: $codelength
  $newcode_length = 0; // define initial value for $newcode_length
  while($newcode_length < $codelenght) { // spelling: $codelength
  $x=1;
  $y=3;
  $part = rand($x,$y);
  if($part==1){$a=48;$b=57;}  // Numbers
  if($part==2){$a=65;$b=90;}  // UpperCase
  if($part==3){$a=97;$b=122;} // LowerCase
  $code_part=chr(rand($a,$b));
  $newcode_length = $newcode_length + 1;
  $newcode = $newcode.$code_part;
  }
  return $newcode;
}
}
I am trying to call in my controller..
$this->load->library('testing');
echo $this->testing->randomtxt(12);
I am gettig fallowing error:
Message: Undefined variable: newcode_length
Please help me..
Thank you.

[/quote]
First, enclose your code in the [ code] brackets to make it easier to read.

The problem is that you have not defined your var: $newcode_length before using it in the while statement. Assign a default value before the while(). I set it to zero as shown above.

Also, beware of the spelling, it will come back to bite you later.


Messages In This Thread
" Undefined variable" message when I am trying to create custom library. - by El Forum - 08-07-2012, 10:35 AM

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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