2016-01-05 31 views
1

如何從數據庫中獲得OOP的註釋?我試着用下面的代碼,但它顯示錯誤。即時通訊新的使用類和我git堅持在這裏試圖解決這個問題。從表中獲取數據並使用OOP顯示

下面是評論類代碼:

function getComments(){ 
    $komentet = array(); 
    $connect = mysql_connect("localhost","root","") or die(mysql_error()); 
    $select = mysql_select_db("profile") or die(mysql_error()); 
    $id = $_SESSION['id']; 
    $result = mysql_query("SELECT * FROM posts order by mycoment DESC") or    die(mysql_error()); 

    while($row = mysql_fetch_array($result)){ 
     array_push($komentet,$row['mycoment']); 
    } 

    return $komentet; 
} 

有了下面的代碼,我試着以顯示我的網頁的DATAS,但我不能。

<?php 
$komentet = $comm->getComments(); 

foreach($komentet as $cm){ 
    echo "<tr><td>".$cm."</td></tr>"; 
} 
?> 

它返回以下錯誤:

Notice: Undefined variable: comm in C:\xampp\htdocs\profile\uprofile.php on line 194 Fatal error: Call to a member function getComments() on null in C:\xampp\htdocs\profile\uprofile.php on line 194

全班組長是下面的代碼:

<?php 

class Comments { 

public $datetime; 
public $comment; 

function insertDate(){ 
    $connect = mysql_connect("localhost","root","") or die(mysql_error()); 
    $select = mysql_select_db("profile") or die(mysql_error()); 
    $id = $_SESSION['id']; 
    $this->datetime = date("F j, Y, g:i a"); 
    $sql = "INSERT INTO posts(userid, mycoment, time) VALUES('$id','$this->comment','$this->datetime')"; 
    if(mysql_query($sql)){ 
     echo "Comment Added"; 
    }else{ 
     echo "Comment Failed"; 
    } 
} 

function getComments(){ 
    $komentet = array(); 
    $connect = mysql_connect("localhost","root","") or die(mysql_error()); 
    $select = mysql_select_db("profile") or die(mysql_error()); 
    $id = $_SESSION['id']; 
    $result = mysql_query("SELECT * FROM posts order by mycoment DESC") or die(mysql_error()); 
    while($row = mysql_fetch_array($result)){ 
    array_push($komentet,$row['mycoment']); 
    } 
    return $komentet; 
} 

} 
+0

哪裏有'$ comm'被初始化了? – Script47

+0

您需要初始化$ comm。 $ comm = new Comments(); – pajamas

+0

@ Fred-ii-我認爲'getComments'在類'Comments'中。 – Script47

回答

0

你有問題,你箱類註釋內部的實例不通過的條件:

require_once('comments.php'); 

if(isset($_POST['postsomething'])){ 
    $comm = new Comments(); 
    $comm->comment = trim($_POST['mycoment']); 
    $comm->insertDate(); 
} 

$komentet = $comm->getComments(); 
foreach($komentet as $cm){ 
    echo "<tr><td>".$cm."</td></tr>"; 
} 

所以你應該改變這樣的:

require_once('comments.php'); 

$comm = new Comments(); 

if(isset($_POST['postsomething'])){ 
    $comm->comment = trim($_POST['mycoment']); 
    $comm->insertDate(); 
} 

$komentet = $comm->getComments(); 
foreach($komentet as $cm){ 
    echo "<tr><td>".$cm."</td></tr>"; 
} 

我做了你一個小的工作和重構使用mysqli擴展和李爾庭索姆DRY(不要重複自己)違反評論類。我強烈建議你堅持使用mysqli並避免任何代碼重複。

class Comments 
{ 

    public $datetime; 
    public $comment; 

    function insertDate(){ 
     $id = $_SESSION['id']; 
     $this->datetime = date("F j, Y, g:i a"); 
     $sql = "INSERT INTO posts(userid, mycoment, time) VALUES('$id', '$this->comment', '$this->datetime')"; 
     if($this->getConnection()->query($sql)){ 
     echo "Comment Added"; 
     } else{ 
     echo "Comment Failed"; 
     } 
    } 

    function getComments(){ 
     $komentet = array(); 
     $result = $this->getConnection()->query("SELECT * FROM posts order by mycoment DESC"); 
     while($row = $result->fetch_assoc()){ 
     array_push($komentet, $row['mycoment']); 
     } 
     return $komentet; 
    } 

    private function getConnection(){ 
     if($this->connection === null) 
     $this->connection = mysqli_connect("localhost", "root", "", "profile") or die(mysqli_error($this->connection)); 
     return $this->connection; 
    } 

    /** @var mysqli */ 
    private $connection; 
} 
+0

該死的,我已經嘗試了這麼多,這是正確的答案。非常感謝@Reloecc :) :),感謝他人的努力。 –

+0

所以它是一個非包含然後,就像我在[這個評論]中提到的(http://stackoverflow.com/questions/34617440/get-datas-from-table-and-display-with-oop#comment56981640_34617440)到OP。我有種感覺這是可變範圍問題。 –

+0

我已經編輯了答案,所以對於不感興趣的所有人都很清楚。很高興爲您提供幫助。 – Reloecc

相關問題