0

基本上 displayCharityList () 中的代码有效。我决定将该代码放在该函数中,将该函数放在一个类中,然后尝试通过稍后调用它来使其工作......

不知何故,它没有从数据库中提取数据。查询在函数内部有效,但在 __contruct 中无效

谁能告诉我我做错了什么?

谢谢

<?php 
// Connection setup


class charity {
public $h;

        function __construct () {
            $c = new PDO('mysql:host=localhost;dbname=seafarer_v2','seafarer_user','supp0rt1243');
            $h = $c->query('SELECT * FROM mnwg_charities')->fetchAll(PDO::FETCH_ASSOC);
            $this->$h = $this->h;
            $this->displayCharity = $this->displayCharityList();
        }


    Public function displayCharityList(){

        echo "<table><tr><th>Name</th><th>Contact</th><th>Post code</th><th>Phone</th><th>Website</th><th>Email</th></tr>";
            foreach ($this->h as $r){

                      echo "<tr><td>";
                      echo $r['Name'];
                      echo "</td>";
                      echo "<td>";
                      echo $r['Contact'];
                      echo "</td>";
                      echo "<td>";
                      echo $r['Postcode'];
                      echo "</td>";
                      echo "<td><div class=\"phone\">";
                      echo "<img src=\"http://m.intertrustgroup.com/images/icon_phone.png\" style=\"margin-right:.5em\">{$r['Phone']}</br>";
                        if($r['Fax']){  echo "<img src=\"http://www.sliksvn.com/gfx/icon_fax.gif\" style=\"margin-right:.5em\">{$r['Fax']}";}
                      echo "</div></td>";
                      echo "<td>";
                      echo "<a href=\"{$r['Website']}\" target=\"_blank\"> Visit Website </a>";
                      echo "</td>";
                      echo "<td>";
                      echo "<a href=\"mailto:{$r['Email']}\">Send Email</a>";
                      echo "</td></tr>";

            }
           echo "</table>";
     }
 }


?>

<?php
$hola = new charity();
echo $hola->displayCharity(); 
?>
4

4 回答 4

2

一些指针,因为 db 连接与慈善类无关,但查询需要您将连接传递给类,这称为依赖注入。否则,因为它代表类的每次初始化,它将查询数据库,如果您要访问另一个不需要该查询的方法,则不需要这样做。还有你的方法echo而不是return方法,没关系,但你需要将调用放在你想要放置的地方。从该方法返回然后在您想要放置的位置使用单个回显更容易。希望能帮助到你。

<?php 

class charity {

    function __construct (PDO $con) {
        $this->con = $con;
    }

    private function get_charities(){
        return $this->con->query('SELECT * FROM mnwg_charities')->fetchAll(PDO::FETCH_ASSOC);
    }

    public function displayCharityList(){

        $return = "<table><tr><th>Name</th><th>Contact</th><th>Post code</th><th>Phone</th><th>Website</th><th>Email</th></tr>";
        foreach ($this->get_charities() as $r){
            $return .= "<tr><td>".$r['Name']."</td>";
            $return .= "</td>";
            $return .= "<td>";
            $return .= $r['Contact'];
            ...
            ...
        }
        $return .= "</table>";
        return $return;
    }
}

$con = new PDO('mysql:host=localhost;dbname=seafarer_v2','seafarer_user','supp0rt1243');

$hola = new charity($con);

echo $hola->displayCharity();
?>
于 2012-10-08T13:30:15.963 回答
0

尝试删除括号

class charity

class charity () //which is wrong
于 2012-10-08T13:03:56.747 回答
0

类 Charity 不是删除括号的函数。

将类名大写是一个好习惯。

于 2012-10-08T13:05:37.900 回答
0
 <?php
 $hola = new charity();
 echo $hola->displayCharity(); //this line no needed
 ?>

不需要第二行,因为您在构造函数本身中调用 displayCharity 函数..所以无需再次调用...然后检查您的查询是否正常工作...

于 2012-10-08T13:09:43.637 回答