Link to home
Start Free TrialLog in
Avatar of burnedfaceless
burnedfaceless

asked on

Undefined Variable: loop_limit, Undefined Variable: number

I'm working out of PHP Patterns Objects and Practice, and I completed chapter 3 and thought I could try to design an OO program that finds the greatest common factor, even if my design was poor, I figured I could show myself the syntax.

Error message is as follows:
Notice: Undefined variable: loop_limit in /Users/brianabbott/Documents/OneDrive/Computer Stuff/GCF/numbers_class.php on line 33

Notice: Undefined variable: number in /Users/brianabbott/Documents/OneDrive/Computer Stuff/GCF/numbers_class.php on line 35

While I wait for responses to this I'm going to review Chapter 3, and keep moving on, this OOP is really tough, but I am motivated to learn it. On a side note how long does it take to learn it? Code posted below.

<!doctype html>
<html>
<head>
  <meta charset="utf-8">
  <title>Enter the number of numbers you need to determine the Greatest Common Factor and Least Common Denominator for</title>
  <script>   	
    
	  function generateHTML(number) {
      var master_markup = '<h3>Enter ' + number + ' numbers to find the Greatest Common Factor';
          master_markup += ' and Least Common Denominator.<br><br>';
      var html1 = '<input name="number[]';
      var html2 = '" type="text" size="3" id="number[]';
      var html3 = '">';
      var hidden_input = '<input type="hidden" name="quantity" value="' + number + '">'; 
      var submit_button = '<input type="submit" value="submit">';
      var i = 1; 
      while (i <= number) {
	      master_markup += html1 + html2 + html3;
	      if (i == number) {
  	      master_markup += hidden_input;
		      master_markup += submit_button;
	      }
	      i++;
      } 
      return master_markup;
	  } 
	
    function getNumber() {
      var number = document.getElementById('number');
	    var number_value = number.value;
	    return number_value;
    }
    
    function master(){
	    var number = getNumber();
	    if (Number(number) === parseInt(number)) {
	      if (2 > number) {
	        alert('The number must be two (2) or higher.');
	      }
	      else if (number > 10) {
	        alert('The number must be less than eleven (11).');
	      }
	      else {
	        var html = generateHTML(number);
		      document.getElementById('contentArea').innerHTML = html; 
	      }
      } else {
	        alert('You must enter a whole number/integer.');
        }
    }
  </script>
</head>
<body>
  <form name="theform" action="process.php" method="post" id="theform">
	<div id="contentArea">
    <h3>How many numbers do you want to computer the Greatest Common Factor and Least Common Denominator for?</h3> 
    <h3>Please enter a number between two (2) and ten (10).</h3>
    <input name="number" type="text" size="2" id="number">
    <button type="button" onclick="master()">Click Here</button>
	</div> 
  </form>
</body>
</html>

Open in new window


<?php
  function are_integers($input, $quantity, $loop_limit) {
    $number_of_integers = 0;
    for ($i = 0; $i <= $loop_limit; $i++) {
      $isInt = (is_numeric($input[$i]) && (int) $input[$i] == $input[$i]);
      if ($isInt == 1) $number_of_integers++;
    }
    if ($number_of_integers == $quantity) {
      return TRUE;
    } else {
      return FALSE;
    }
  }
    
    
  ?>

Open in new window


<?php 
  class numbers 
  {
    private $number = array();
    private $quantity;
    private $loop_limit;
    private $i;
    private $j;
    private $candidate;
    private $factors;

    
    function __construct($input) {
      $this->number = $input;
    }
    
    public function set_quantity($batch) {
      $this->quantity = $batch;
    }
    
    public function set_loop_limit($batch) {
      $this->loop_limit = $batch - 1;
    }
    
  
    
    public function get_quantity() {
      return $this->quantity;
    }
    
    
    public function generate_factors() {
      for ($i = 0; $i <= $loop_limit; $i++) {
        $j = 1;
        while ($j <= $number[$i]) {
          $candidate = ($number[$i] / $j);
          $isInt = (is_numeric($candidate) && (int) $candidate == $candidate);
          if ($isInt == 1) {
            $factors[$j] = $candidate;
          }
          $j++;
        }
       return $this->factors;
      }
    }
}

Open in new window


<?php
  require_once 'functions.php';
  require_once 'numbers_class.php';

  $input = (int) isset($_POST['number']) ? $_POST['number'] : array();
  $quantity = $_POST['quantity'];
  $quantity = (int)$quantity;
  $loop_limit = $quantity - 1;
  
  $input_integers = are_integers($input, $quantity, $loop_limit);    
    
  if ($input_integers == FALSE) {
    echo '
    <!doctype html>
    <html>
    <head>
      <meta charset="utf-8">
      <title>Enter whole numbers this time</title>
    </head>
    <body>';
    echo "<h3>Please enter $quantity whole numbers to find the Greatest Common Factor and Least Common Denominator.</h3>";
    for ($i = 0; $i <= $loop_limit; $i++) {
      echo '<input name="number[]" type="text" size="3" id="number[]">';
    } echo'<input type="submit" value="submit">
    </body>
    </html>';
  } else {
    $integers = new numbers($input);
    $integers->set_quantity($quantity);
    $integers->set_loop_limit($loop_limit);
    print_r($integers->generate_factors());
  } 
    
?>

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of burnedfaceless
burnedfaceless

ASKER

Thanks for the speedy response.

I know it's poor design, but if I can knock something out that at least uses the syntax that would be alright.
You are welcome.

Remember we all had to start somewhere - recognizing that you can improve the design already shows you are on the right track.