Reputation: 4173
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();
?>
Upvotes: 0
Views: 61
Reputation: 46602
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 echo
ing and not return
ing 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.
<?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();
?>
Upvotes: 2
Reputation: 3878
<?php
$hola = new charity();
echo $hola->displayCharity(); //this line no needed
?>
second line no needed, because your calling displayCharity function with in the constructor function itself.. so no need to call again... and then check your query is working correct...
Upvotes: 0
Reputation:
class Charity is not a function remove brackets.
It is a good habit to Capitalize class Names.
Upvotes: 0
Reputation: 1285
Try Removing brackets
class charity
class charity () //which is wrong
Upvotes: 0