2015-11-07 77 views
0

我是一個執行OOP的總noob,目前正在尋找重構一個程序化PHP項目。目前,我遇到的問題是試圖簡單地顯示來自SELECT查詢的結果 - 具有諷刺意味的是,做一個INSERT查詢變得有意義。這是我的.class文件:使用oop顯示SELECT查詢結果php

<?php 
class MadLibs {     
    private $noun; 
    private $verb; 
    private $adverb; 
    private $adjective; 
    private $story; 

    // Getters 
    public function getNoun() { 
     return $this->noun; 
    } 
    public function getVerb() { 
     return $this->verb; 
    } 
    public function getAdverb() { 
     return $this->adverb; 
    } 
    public function getAdjective() { 
     return $this->adjective; 
    } 
    public function getStory() { 
     return $this->story; 
    }   
    // Setters 
    public function setNoun($noun) { 
     $this->noun = $noun; 
    } 
    public function setVerb($verb) { 
     $this->verb = $verb; 
    } 
    public function setAdverb($adverb) { 
     $this->adverb = $adverb; 
    } 
    public function setAdjective($adjective) { 
     $this->adjective = $adjective; 
    } 
    public function setStory($story) { 
     $this->story = $story; 
    } 
    // Figured this one out 
    public function insertStoryToDatabase() { 
     date_default_timezone_set('America/Chicago'); 
     $submit_date = date('Y-m-d' . " " . 'H:i:s');    
     $dbc = mysqli_connect('localhost','root','','mad_libs') 
      or die('Error establishing connection');       
     $query = "INSERT INTO storyTime (noun, verb, adjective, adverb, createdDate, fullStory) " . 
       "VALUES ('$this->noun','$this->verb','$this->adjective','$this->adverb','$submit_date','$this->story')";    
     mysqli_query($dbc,$query) 
       or die('Error querying database');      
     mysqli_close($dbc); 
    } 
    // method I am having trouble setting up 
    public function displayAllStories() { 
     $dbc = mysqli_connect('localhost','root','','mad_libs') 
      or die('Error establishing connection');     
     $result  = "SELECT fullStory, createdDate FROM storyTime ORDER BY createdDate DESC";    
     $display = mysqli_query($dbc,$result) 
       or die('Error gathering data!');    
     while ($row = mysqli_fetch_array($display)) { 
      echo $row['story'] . '<br/>'; 
     } 
     return $display; 
    }  
    }  
?> 

這是我在我的其他PHP文件正在做的:

<?php 
    require_once('MadLibs.php'); 
    // Instantiating instance of MadLibs() 
    $foo = new MadLibs(); 

    // Playing around to see ensure getters/setters were correct 
    $foo->setNoun('Bob'); 
    $foo->setVerb('jumps'); 
    $foo->setAdverb('quietly'); 
    $foo->setAdjective('ugly'); 
    $foo->setStory("The " . $foo->getNoun() . " always " . $foo->getVerb() . " at the " 
. $foo->getAdjective() . " strangers " . $foo->getAdverb() . ".");  
    echo $foo->getNoun(); 
    echo '<br />'; 
    echo $foo->getVerb(); 
    echo '<br />'; 
    echo $foo->getAdverb(); 
    echo '<br />'; 
    echo $foo->getAdjective(); 
    echo '<br />'; 
    echo $foo->getStory(); 

    $foo->insertStoryToDatabase(); 

    $foo->displayAllStories();  
?>  

我敢肯定,這是簡單的東西,但對我的生活中,我可以」弄清楚我做錯了什麼。

任何和所有的反饋是最感謝!

+0

'回聲$行[' 故事'。 '
';'將其存儲在一個變量中然後返回。 – aldrin27

回答

0

您試圖在displayAllStories()函數中顯示錯誤的變量。 '迴響$行[' 故事 '] should be change to $行[' fullStory '];`或更改查詢

SELECT fullStory as story, createdDate FROM storyTime ORDER BY createdDate DESC 
+0

謝謝@Senanayaka,讀完這本以後,我完全面面相覷! – kmancusi