3 Replies - 180 Views - Last Post: 16 January 2019 - 12:37 PM Rate Topic: -----

#1 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 19-July 16

PHP OOP generate perfect html table with colum names

Posted 16 January 2019 - 10:50 AM

I want to create simple html table structure with using php oop and pdo object fetching. I dont know if it is right way to do..

So I hava mysql table and columns:

id___first_name___last_name

1___John________Doe

2___Jhon________Dope

And I want get field names also into html table..

Example:
<table style="width:100%">
  <tr>
    <th>id</th>
    <th>first_name</th> 
    <th>last_name</th>
  </tr>
  <tr>
    <td>1</td>
    <td>John</td> 
    <td>Doe</td>
  </tr>
  <tr>
    <td>2</td>
    <td>Jhon</td> 
    <td>Dope</td>
  </tr>
</table>

So php oop class queries data and generates table if I call function..

it loops field names and values.. It doesnt generetate perfect table..

public function dataview() {
        $query = "SELECT id, first_name, last_name FROM " . $this->table_names;
        $stmt = $this->conn->prepare($query);
        $stmt->execute();
        $row = $stmt->fetchAll(); //I have fetch default mode OBJ;

        echo '<table>';
        foreach ($row as $key) {
            foreach ($key as $k => $v) {
                echo '<tr><td>'.$k.'</td><td>'.$v.'</td></tr>';
            }
        }
        echo '</table>';
    } 


Is This A Good Question/Topic? 0
  • +

Replies To: PHP OOP generate perfect html table with colum names

#2 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2766
  • View blog
  • Posts: 10,958
  • Joined: 03-December 12

Re: PHP OOP generate perfect html table with colum names

Posted 16 January 2019 - 10:55 AM

You have a nested foreach loop for whatever reason. Should the table row tag be in the inner or outer loop?
Was This Post Helpful? 0
  • +
  • -

#3 testmetestyou   User is offline

  • D.I.C Head

Reputation: 0
  • View blog
  • Posts: 76
  • Joined: 19-July 16

Re: PHP OOP generate perfect html table with colum names

Posted 16 January 2019 - 11:04 AM

I cant get column field name other way out..
Was This Post Helpful? 0
  • +
  • -

#4 astonecipher   User is offline

  • Senior Systems Engineer
  • member icon

Reputation: 2766
  • View blog
  • Posts: 10,958
  • Joined: 03-December 12

Re: PHP OOP generate perfect html table with colum names

Posted 16 January 2019 - 12:37 PM

So then you need two loops. One for the column names and one for the data. That doesn't mean you need a nested loop however.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1