0

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|
1
  • 2
    whith-out LOOP.....why you want to try so ! Commented Oct 31, 2012 at 7:38

3 Answers 3

3

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>';
Sign up to request clarification or add additional context in comments.

Comments

1

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>

Comments

1

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

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.