Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8659169
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T15:56:46+00:00 2026-06-12T15:56:46+00:00

Basically the code inside the displayCharityList () works. I decided to put that code

  • 0

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(); 
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T15:56:48+00:00Added an answer on June 12, 2026 at 3:56 pm

    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 not returning 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();
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

The following code basically works as expected. However, to be paranoid, I was wondering,
So basically this code: class A { }; class B { B (const B&
I have (basically) this code - <script type=text/javascript language=javascript> $(document).ready(function() { $(#loadDiv).load(mypage.html, function(){ alert($(#someText).text())
$(document).ready(function() { $('#domain').change(function() { // }); }); The code inside the change function will
I have this coding inside my java file . This code basically receives an
I am looking for some code that will load a page inside of a
Basically my problem is as follows. I put the following code in the browser
I have trouble with some code I just wrote. Basically, this code is inside
I've the following code : (basically it creates various JSONObjects (beneficiario) and put them
I have a created a snippet of code that basically animates an h2 onto

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.