Question

 public function displayMember()
    {
        $statement=$this->db->query("SELECT user_name,user_email,user_gender,date_of_join,user_firstname FROM tbl_users");
          while($row=$statement->fetch(PDO::FETCH_ASSOC))

This is from a class file. How we can return the associative array in the page where we display our values where we call the displayMember() method. What I need is to print the data not inside this function but to pass it and print it on the page I call this function inside a table.

Was it helpful?

Solution 2

I found the answer finally. This is how it worked for me

public function displayMember()
    {
        $statement=$this->db->query("SELECT user_name,user_email,user_gender,date_of_join,user_firstname FROM tbl_users");
          while($row=$statement->fetch(PDO::FETCH_ASSOC))
           {
            $data[]=$row;
           }
         if(!empty($data))
          {
           return $data;
           }
}

And on the displaying page

if($obj->displayMember()!=NULL)
{

foreach($obj->displayMember() as $value)
{
extract($value);

echo $user_name.'<br>';
echo $user_email.'<br>';
echo $user_gender.'<br>';
//actually I echoed the values inside a table.

}
}

Thanks for all your support.

OTHER TIPS

public function fetchMembers()
{
    $sql = "SELECT user_name,user_email,user_gender,date_of_join,user_firstname FROM tbl_users";
    return $this->db->query($sql)->fetchAll();
}
$members = $class->fetchMembers();

Now you can pass $members to template. It is not an associative array but rather a list though

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top