出於某種原因,我不能爲我的生活找出/理解爲什麼這不能正常工作......我所要做的就是顯示在照片頁面發表評論。我在測試這個照片有2個評論,但我得到的是這樣的:注意:試圖獲取非對象的屬性 - 也不是foreach正常循環
Notice: Trying to get property of non-object in /Applications/MAMP/htdocs/photo_gallery/public/photo.php on line 47
NULL @ ::
photo.php頁面簡單地調用由照片的身份證的意見,然後在foreach循環顯示它們:
<?php
$photo = Photograph::findByID($_GET['id']);
$comments = $photo->comments();
printr($comments);
?>
<div class="comments">
<?php if ($comments): ?>
<?php foreach ($comments as $comment): ?>
<p style="margin-bottom: 15px;">
<?php echo htmlentities($comment->author); ?> @ <?php echo strip_tags($comment->created, '<strong><em><p>'); ?> :: <?php echo $comment->body; ?>
</p>
<?php endforeach; ?>
<?php else: ?>
<p>No comments found.</p>
<?php endif; ?>
</div>
當我的print_r對$意見的結果,只有第一個評論對象數組中顯示出來..照片類的
部分:
在MySQLiDatabase類class Photograph extends DatabaseObject {
public $id;
function comments() {
return Comment::findCommentsOn($this->id);
}
}
部分:
public static function findByPrepare($sql = "", $params) {
global $db;
$result = $db->query($sql, $params);
foreach ($result as $row) {
printr($result);
return self::instantiate($row);
}
}
private static function instantiate($record) {
$className = get_called_class();
$obj = new $className;
foreach ($record as $attribute => $val) {
if ($obj->hasAttribute($attribute)) {
$obj->$attribute = $val;
}
}
return $obj;
}
private function hasAttribute($key) {
// get_object_vars returns an assoc array with all attributes as keys and their current values as the value
$objVars = $this->attributes();
return array_key_exists($key, $objVars);
}
protected function attributes() {
// return an array of attribute keys and their values
$attributes = array();
foreach(static::$dbFields as $field) {
if (property_exists($this, $field)) {
$attributes[$field] = $this->$field;
}
}
return $attributes;
}
好像它是從findByPrepare()函數的foreach制止......
當我printr($結果)它顯示兩個評論在一個數組罰款,但是當我printr($行)它只顯示數組中的第一個評論......所以不知何故我需要獲得$行來循環每個結果。我曾嘗試循環數組中的自我::實例化($行),但隨後拋出另一個錯誤:致命錯誤:調用一個成員函數評論()非對象
* 在$ print_r的上評論(這只是想出個結果時,有實際2條評論)*
Comment Object
(
[id] => 1
[photo_id] => 1
[created] => 2013-12-20 16:37:02
[author] => Nate
[body] => This is a cool pic!
)
* 的var_dump($ comment->作者)顯示*
NULL @ ::
* 的var_dump($評論)顯示*
object(Comment)#4 (5) { ["id"]=> int(1) ["photo_id"]=> int(1) ["created"]=> string(19) "2013-12-20 16:37:02" ["author"]=> string(4) "Nate" ["body"]=> string(19) "This is a cool pic!" }
被請求的其餘代碼:
評論類:
class Comment extends DatabaseObject {
protected static $tableName = "comments";
protected static $dbFields = array('id', 'photo_id', 'created', 'author', 'body');
public $id;
public $photo_id;
public $created;
public $author;
public $body;
public static function findCommentsOn($photo_id = 0) {
$params = func_get_args();
$sql = "SELECT * FROM ". self::$tableName ." WHERE photo_id = ? ORDER BY created ASC";
$results = parent::findByPrepare($sql, $params);
return $results;
}
}
MySQLiDatabase類:
class MySQLiDatabase {
public $conn;
public $lastQuery;
public $stmt;
public $id;
public $username;
public $password;
public $first_name;
public $last_name;
private $params;
public function __construct() {
$this->connection();
}
public function connection() {
$this->conn = new mysqli(DB_SERVER, DB_USER, DB_PASS, DB_NAME);
if ($this->conn->connect_errno) {
return "Failed to connect to MySQL: (" . $this->conn->connect_errno . ") " . $this->conn->connect_error;
}
}
public function query($sql, $params) {
$this->lastQuery = $sql;
$result = $this->prepareQuery($sql, $params);
if ($params) { $this->bindParams($params, $this->stmt); }
$this->stmt->execute();
$results = $this->bindResults($this->stmt);
return $results;
}
private function prepareQuery($sql, $params) {
$this->stmt = $this->conn->prepare($sql);
$this->confirmQuery($this->stmt);
return $this->stmt;
}
public function bindParams($params, $stmt) {
$types = "";
for($i = 0; $i < sizeof($params); $i++) {
$types .= "s";
}
$array = array_merge(array($types), $params);
return call_user_func_array(array($this->stmt, 'bind_param'), $this->refValues($array));
}
public function bindResults($stmt) {
if ($stmt->affected_rows === -1) { // SELECT
$meta = $this->stmt->result_metadata();
$params = array();
while ($field = $meta->fetch_field()) {
$params[] = &$row[$field->name];
}
call_user_func_array(array($this->stmt, 'bind_result'), $params);
$results = array();
while ($stmt->fetch()) {
$x = array();
foreach ($row as $key => $val) {
$x[$key] = $val;
}
$results[] = $x;
}
} else { // INSERT UPDATE DELETE
$results = $stmt->affected_rows;
}
return $results;
}
您是否提供了註釋的var_dump或print_r? –
當然,現在更新問題。 – Nate
它在我看來像你迭代索引,ID,photo_id等,而不是實際的評論。這與您的評論相關,只有一條評論正在顯示。我不確定你使用的數據庫層是什麼,但我相當確定這裏的問題是數據庫被告知返回一行而不是所有行。 –