0

mysqli_result :: fetch_objectメソッドを使用してデータベースに格納されているユーザーをPersonオブジェクトにロードしてから、配列をHTMLテーブルとして出力できるように、それらを配列に出力しようとしています。

84行目の非オブジェクトエラーでメンバー呼び出し関数を取得し続けています。$rowでvar_dumpを実行すると、NULLが出力されます。誰かが私が間違っていることを教えてもらえますか?

index.php

<!DOCTYPE html>
<html>
    <head>
        <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
        <link href ="Stylesheet.css" rel="stylesheet" type="text/css">
        <title>Assignment 3</title>
    </head>
    <body>
        <?php
        require ('Person.php');

        //Making an empty array to store Person objects
            $empty = array();


        //Defining parameters for mysqli connect functio

            $p1 = '2011.ispace.ci.edu';
            $p2 = 'username';
            $p3 = 'password';
            $p4 = 'dbname';

        //Connecting to the database and checking to see if it fails        

            $db = new mysqli($p1, $p2, $p3, $p4);
                if ($db->connect_error) 
                {
                    echo "Error Connecting:" . " " . $db->connect_error;
                }

        //Querying the database to get the information from the Person table  

                else 
                {
                $result = $db->query(
                        "SELECT * 
                        FROM Person"
                        );

                        if($result === false){
                            printf($db->error);
                        }

                           var_dump($result);

         //Looping through the rows and storing them in the array

                $totalRows = $result->num_rows;

                if ($totalRows > 0)
                {
                    $emp = 'Person';
                    while($row = $result->fetch_object($emp));
                    {

                        $empty[] = $row;
                        $row++;
                        var_dump($row);

                    }

                }

                }


        //Making the table headers
        echo "<h1>Assignment 3 Incorporated</h1>";
        echo "<table>";

                echo "<tr>";
                echo "<td><strong>Name</strong></td>";
                echo "<td><strong>Title</strong></td>";
                echo "<td><strong>Office</strong></td>";
                echo "<td><strong>Phone Number</strong></td>";
                echo "<td><strong>Email</strong></td>";
                echo "</tr>";

        //Printing out the table with foreach loop

         foreach ($empty as $newPerson) {

                echo "<tr>";

                echo "<td>" . $newPerson-> getfirstName() . " " . $newPerson-> getlastName() . "</td>";
                echo "<td>" . $newPerson-> gettitle() .  "</td>";
                echo "<td>" . $newPerson-> getoffice() .  "</td>";
                echo "<td>" . $newPerson-> getphoneNumber() .  "</td>";
                echo "<td><a href='mailto:" . $newPerson-> getemail() . "'>" . $newPerson-> getemail() .  "</a></td>";

                echo "</tr>";

        }



        echo "</table>"; 

        ?>
    </body>
</html>

Person.php

<?php

class Person {

   private $firstName;
   private $lastName;
   private $title;
   private $office;
   private $phoneNumber;
   private $email;
   //private $data;


 //Constructor

 /*   public function __construct($name)
  {
    $this-> setname($name);
  }
  */ 
//Interface Methods

//Get/Set Name  
  public function setname($name)
  {
    //using explode function to split name into 2 strings
      $this-> name = $name;
      if(is_string($name)) {

           list($lastName, $firstName) = explode(",",$this -> name); 
           $this->firstName = $firstName;
           $this->lastName = $lastName;  

      }

      else {
          user_error('Error: Persons Name Must Be a String!');
      }

  }

  public function getfirstName()
  {

    return $this-> firstName;  

  }


  public function getlastName() 
  {

    return $this-> lastName;   

  }


public function getname()
  {
    /**
    $nameSplit = explode(",",$this ->name);
    foreach ($nameSplit[1] as $firstName);
    foreach ($nameSplit[0] as $lastName);
    echo $firstName . "," . $lastName;
    return $this-> name; 
    **/
  }

  //Get/Set Title
  public function settitle($title)
  {
    $this-> title = $title;  
  }

  public function gettitle()
  {
      return $this-> title;
  }   

  //Get/Set office
  public function setoffice($office)
  {
      $this-> office = $office;
  }

  public function getoffice()
  {
      return $this-> office;
  }

  //Get/Set phone number
  public function setphoneNumber($phoneNumber)
  {
      $this-> phoneNumber = $phoneNumber;  

  }

  public function getphoneNumber()
  {
      //str_replace to modify phone number format
      if(is_string($this-> phoneNumber)) {  
      $find3 = array (')', '(', ' '); 
      $replace3 = array ('', '', '-');
      $phonestr = str_replace($find3, $replace3, $this-> phoneNumber);
      return $phonestr;
      }

      else {
         return user_error("Error: Person's phone number must be a string");
      }
  }

  //Get/Set email
  public function setemail($email)
  {
      $this-> email = $email;
  }

  public function getemail()
  {
      return $this-> email;
  }





      }
  ?>
4

1 に答える 1

0

複数のループは必要ありません...

if ($totalRows > 0) {
    $emp = 'Person';
    while ( $row = $result->fetch_object($emp) )
    {

        $newPerson = new person($row);
        echo "<tr>";

        echo "<td>" . $newPerson->getfirstName() . " " . $newPerson->getlastName() . "</td>";
        echo "<td>" . $newPerson->gettitle() . "</td>";
        echo "<td>" . $newPerson->getoffice() . "</td>";
        echo "<td>" . $newPerson->getphoneNumber() . "</td>";
        echo "<td><a href='mailto:" . $newPerson->getemail() . "'>" . $newPerson->getemail() . "</a></td>";

        echo "</tr>";
    }
}

クラスConstructorは次のようになります

class Person {
    private $firstName;
    private $lastName;
    private $title;
    private $office;
    private $phoneNumber;
    private $email;

      public function __construct($row) { 
        $this-> setname($row->firtname); 
        $this-> settitle($row->title);
        //And So on ...
      }
  .... So On

最後に....常に質問を削除して投稿するのではなく..必ずいつか質問に答えてください

于 2012-10-15T00:55:14.003 に答える