在函数内部和类内部创建一段代码

时间:2012-10-08 12:59:28

标签: php mysql

基本上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 个答案:

答案 0 :(得分:2)

一些指针,因为db连接与charity类无关,但是查询需要将连接传递给类,这称为依赖注入。另外,因为它代表了类的每次初始化,它将查询数据库,如果您要访问不需要该查询的另一个方法,则不需要该数据库。 也是你的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();
?>

答案 1 :(得分:0)

尝试删除括号

class charity

class charity () //which is wrong

答案 2 :(得分:0)

类Charity不是函数删除括号。

将课程名称大写是一个好习惯。

答案 3 :(得分:0)

 <?php
 $hola = new charity();
 echo $hola->displayCharity(); //this line no needed
 ?>

第二行不需要,因为你在构造函数本身调用displayCharity函数。 所以不需要再打电话...... 然后检查您的查询是否正常...