0

我的意图是从数据库中检索数据并创建了以下函数,但收到未定义的函数、成绩、错误消息。代码如下:

      <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>

然后处理表单提交的PHP文件:

<?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.";
  }

   }

和查看页面:

     <?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>
4

1 回答 1

1

要访问类函数,请使用以下代码,

你必须像这样使用它$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>
于 2013-04-06T04:03:01.087 回答