MahmoudMustafa
MahmoudMustafa

Reputation: 144

PHP table two columns

How can I make table in PHP with two columns and 10 variables for example (without loop)?

For example:

$var1= $row['name'];
$var2= $row['age'];

The way I want to show it in the table:

    ______________________
   | Customer Name | $var1|
   | Customer Age  | $var2|

Upvotes: 0

Views: 44220

Answers (3)

user1483059
user1483059

Reputation:

You can use something like this:

$array = array();
$array[0]["name"];
$array[0]["age"];

or instead of that , you can use a loop:

$i=0;
while($i<10){
$array[$i]["name"]=...;
$i++;
}

print_r($array);

Upvotes: 1

Praveen Kumar Purushothaman
Praveen Kumar Purushothaman

Reputation: 167172

Simple. Use this syntax:

<table >
    <tr>
        <th>Customer Name</th>
        <td><?php echo $var1; ?></td>
    </tr>
    <tr>
        <th>Customer Age</th>
        <td><?php echo $var2; ?></td>
    </tr>
    <tr>
        <th>Customer Name</th>
        <td><?php echo $var3; ?></td>
    </tr>
    <tr>
        <th>Customer Age</th>
        <td><?php echo $var4; ?></td>
    </tr>
    <tr>
        <th>Customer Name</th>
        <td><?php echo $var5; ?></td>
    </tr>
    <tr>
        <th>Customer Age</th>
        <td><?php echo $var6; ?></td>
    </tr>
    <tr>
        <th>Customer Name</th>
        <td><?php echo $var7; ?></td>
    </tr>
    <tr>
        <th>Customer Age</th>
        <td><?php echo $var8; ?></td>
    </tr>
    <tr>
        <th>Customer Name</th>
        <td><?php echo $var9; ?></td>
    </tr>
    <tr>
        <th>Customer Age</th>
        <td><?php echo $var10; ?></td>
    </tr>
</table>

Upvotes: 1

Mihai Matei
Mihai Matei

Reputation: 24276

Without loop:

echo '
<table>
  <tr>
    <td>Customer Name</td>
    <td>', $var1, '</td>
  </tr>
  <tr>
    <td>Customer Age</td>
    <td>', $var2, '</td>
  </tr>
  <tr>
    <td>Customer ...</td>
    <td>', $var3, '</td>
  </tr>
</table>';

With foreach loop

$myFields = array("Customer Name" => $row['Name'], 
                  "Customer Age"  => $row['Age']);

echo '<table><tr>';

foreach($myFields as $field_title => $field_value)
   echo '<td>', $field_title, '</td>
         <td>', $field_value, '</td>';

echo '</tr></table>';

Upvotes: 2

Related Questions