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?

5
  • What exactly do you want to achieve? There are Arrays which is the closest i can image to a table. Commented Sep 8, 2017 at 7:06
  • you need to store <table> into '' then echo $table1 Commented Sep 8, 2017 at 7:06
  • Possible duplicate of PHP multiline string with PHP Commented Sep 8, 2017 at 7:07
  • 2
    Yes, because your "table" is just a text (string). Commented Sep 8, 2017 at 7:07
  • @DB i want dynamic table from a Mysql database Commented Sep 8, 2017 at 7:16

6 Answers 6

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.

Sign up to request clarification or add additional context in comments.

Comments

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;

Comments

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

Comments

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;

Comments

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;

Comments

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.