Basically the code inside the displayCharityList () works. I decided to put that code inside that function and that function inside a class and then tried to make it work by calling it afterwards….
Somehow it not pulling the data from the database. The query works inside the function but not in the __contruct
Can anyone tell me what i am doing wrong plz?
Thank you
<?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();
?>
Some pointers, as the db connection is not related to the charity class but needed for query’s you would pass the connection to the class, this is called dependency injection. Else as it stands on every initialization of the class, it is going to query the database, which is not needed if your going to access another method that does not require that query.
Also your
echoing and notreturning from the method, thats ok but you would need to place the call to the method specifically where you want placement. Its easier to return from the method and then use a single echo where you want placement. Hope it helps.