0

My intention is to retrieve data from a database and have created the following functions but receive an undefined function, grades, error message. The code is as follows:

      <form action="processor.php" method="post">
      <h4>Question # 1</h4>
      <p>What grade are you in?</p>
      <label class="checkbox"><input type="checkbox" name="grade" value="1"> Freshmen</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="2"> Sophomore</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="3"> Junior</label>
      <label class="checkbox"><input type="checkbox" name="grade" value="4"> Senior</label>

      <h4>Question # 2</h4>
      <p>What is your current GPA?</p>
      <select name="gpa">
      <option value="4">3.5 or above</option>
      <option value="3">3.0-3.4</option>
      <option value="2">2.5-2.9</option>
      <option value="1">2.0-2.4</option>
      <option value="0">Lower</option>
      </select>

      <h4>Question # 3</h4>
      <p>Where do you excel the most academically?</p>
      <select name="school" multiple="multiple">
      <option value ="1">Mathematics</option>
      <option value ="2">Literature</option>
      <option value ="3">History</option>
      <option value ="4">Humanities</option>
      <option value ="5">Science</option>
      </select>

Then the PHP file that processes the form submission:

<?php
require_once('processor.php');
class Processor {
    #code

function get_response($id) {
$dsn = "mysql:host=localhost;dbname=user";
$username = "root"; // database username
$password = "stewie12"; // database password
try {
    $enter = new PDO($dsn, $username, $password);
    $sql = "SELECT response FROM recommendations WHERE id = ? ";
    $new_item = $enter->prepare($sql);
            $new_item->setFetchmode(PDO::FETCH_ASSOC);
    $new_item->execute(array($id));
    foreach($new_item as $nw) {
        return $nw['response'];
    }
} catch (Exception $e) {
    echo $e->getMessage();
    exit;
}
return "";
    }



 function GetWeird () {
    $santiago="1992";
if ($santiago="1992") {
        $id = 1;
    } else {
        $id = 2;
}
        echo $this->get_response($id);
    }


 function Grades () {
 $grade = $_POST['grade'];
 if ($grade =="1") {
    echo "You're a freshmen";
} elseif ($grade == "2") {
    echo "You're a sophomore";
} elseif ($grade == "3") {
    echo "You're a junior.";
} elseif ($grade == "4") {
     echo "You're a senior.";
 } else {
    echo "Something is wrong.";
 }

 }

 function Gpa () {
 $gpa = $_POST['gpa'];
 if ($gpa =="1") {
     echo "You strongly need to up your GPA.";
 } elseif ($gpa == "2") {
     echo "You're an average student.";
 } elseif ($gpa == "3") {
     echo "You're an above average student.";
 } elseif ($gpa == "4") {
    echo "You're an excellent sudent.";
 } else {
    echo "Something is wrong.";
  }

  }

 function School () {
 $school = $_POST['school'];
 if ($school =="1") {
 echo "You're into Math";
 } elseif ($school == "2") {
   echo "You're into Lit";
  } elseif ($school == "3") {
  echo "You're into history.";
 } elseif ($school == "4") {
    echo "You're into humanities.";
 } elseif ($school == "5") {
   echo "You're into science.";
  } else {
   echo "Something is wrong.";
  }

   }

And the view page:

     <?php $processor = new Processor(); ?>
     <h4>Question # 1</h4>
     <p><?php Grades($grade); ?></p>
     <h4>Question # 2</h4>
     <p><?php Gpa($gpa); ?></p>
      <h4>Question # 3</h4>
     <p><?php School($school); ?></p>

1 Answer 1

1

To access the class function use below code,

You have to use it like this $class_variable->function_name()

<?php $processor = new Processor(); ?>
 <h4>Question # 1</h4>
 <p><?php $processor->Grades($grade); ?></p>
 <h4>Question # 2</h4>
 <p><?php $processor->Gpa($gpa); ?></p>
  <h4>Question # 3</h4>
 <p><?php $processor->School($school); ?></p>
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.