0

Is it possible to define a variable in PhP as a table? ie

$table1 = <table style="width:100%">
               <tr>
                  <th>Firstname</th>
                  <th>Lastname</th> 
                  <th>Age</th>
               </tr>
               <tr>
                   <td>Jill</td>
                   <td>Smith</td> 
                   <td>50</td>
               </tr>
               <tr>
                    <td>Eve</td>
                    <td>Jackson</td> 
                    <td>94</td>
               </tr>
          </table>
;

Or is there a particuar way i can achieve the same?

dhi_m
  • 1,235
  • 12
  • 21
Alfred Kimotho
  • 99
  • 3
  • 17

6 Answers6

2

I think you're mixing up two things:

  • A list data structure where each element possesses the properties firstname, lastname and age.
  • A way to represent the list, such as a table. In this case, a table in the markup language HTML.

You could use an array() with for each element an array() with key-value pairs in it. But it is better to apply an object-oriented approach.

You first need to construct objects of data from the database, and then populate an array with them:

class Person {

    private $firstname;
    private $lastname;
    private $age; // It's better to pass a birthdate.

    public function __construct($firstname, $lastname, $age) {
        $this->firstname = $firstname;
        $this->lastname = $lastname;
        $this->age = $age;
    }
}
$persons = array();

foreach ($someResultingRowFromDatabase as $row) {
    $persons[] = new Person($row['firstname'], $row['lastname'], $row['age']);
}

I don't know the exact code for fetching data from a database, but you should be using PDO.

And later walk over the $persons array with the Person objects and write the HTML table.

MC Emperor
  • 22,334
  • 15
  • 80
  • 130
1

If you want a Static table as PHP variable you can insert with in the single quote (') like this code...

$table1 = '<table style="width:100%">
              <tr>
                <th>Firstname</th>
                <th>Lastname</th> 
                <th>Age</th>
              </tr>
              <tr>
                <td>Jill</td>
                <td>Smith</td> 
                <td>50</td>
              </tr>
              <tr>
                <td>Eve</td>
                <td>Jackson</td> 
                <td>94</td>
              </tr>
            </table>';

If you want dynamic table columns This code will work... Use concatenate Operator....

$table1 = '<table style="width:100%">
          <tr>
            <th>Firstname</th>
            <th>Lastname</th> 
            <th>Age</th>
          </tr>';
foreach($array as $val){
    $table1 .= '<tr>
                <td>'.$val['fname'].'</td>
                <td>'.$val['lname'].'</td> 
                <td>'.$val['age'].'</td>
              </tr>';
}
$table1 .= '</table>';
echo $table1;
Nawin
  • 1,653
  • 2
  • 14
  • 23
1

What you are looking for is not called table, but an Array. An Array is basically a set of data with an index. The data elements itself can be arrays again. Your structure itself would look like this as array:

$array = 
[
    [
        "Firstname" => "Jill",
        "Lastname" => "Smith",
        "Age" => 50
    ],
    [
        "Firstname" => "Eve",
        "Lastname" => "Jackson",
        "Age" => 94
    ],
];

You have now a variable, that contains your data in a structured form and you can loop through it using foror foreach loops. If your data is stored in a MySQL Databse, you might have a look at this example on php.net. This explains how to get data from you database to an array (not the $actor variable at the end of the example).

D B
  • 534
  • 3
  • 14
0

You could use a heredoc string

 $table1 = <<<EOD
<table style="width:100%">
                              <tr>
                                <th>Firstname</th>
                                <th>Lastname</th> 
                                <th>Age</th>
                              </tr>
                              <tr>
                                <td>Jill</td>
                                <td>Smith</td> 
                                <td>50</td>
                              </tr>
                              <tr>
                                <td>Eve</td>
                                <td>Jackson</td> 
                                <td>94</td>
                              </tr>
                            </table>
EOD;
Manav
  • 1,357
  • 10
  • 17
0

Please try this

$table1 = '<table style="width:100%">
               <tr>
                  <th>Firstname</th>
                  <th>Lastname</th> 
                  <th>Age</th>
               </tr>
               <tr>
                   <td>Jill</td>
                   <td>Smith</td> 
                   <td>50</td>
               </tr>
               <tr>
                    <td>Eve</td>
                    <td>Jackson</td> 
                    <td>94</td>
               </tr>
          </table>'
;

echo $table1;
dhi_m
  • 1,235
  • 12
  • 21
0

You successfully did it!) U placed table to variable. But seriously you try to combine two things in one: data and view. It's a very wrong way in programing.

tre
  • 845
  • 1
  • 8
  • 11