Alfred Kimotho
Alfred Kimotho

Reputation: 101

Is it possible to have a PHP variable as a table?

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?

Upvotes: 0

Views: 3702

Answers (6)

tre
tre

Reputation: 915

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.

Upvotes: 0

MC Emperor
MC Emperor

Reputation: 22997

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.

Upvotes: 2

D B
D B

Reputation: 532

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).

Upvotes: 1

Nawin
Nawin

Reputation: 1692

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;

Upvotes: 1

dhi_m
dhi_m

Reputation: 1265

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;

Upvotes: 0

Manav
Manav

Reputation: 1367

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;

Upvotes: 0

Related Questions