0

例如,如何在 PHP 中使用两列和 10 个变量制作表格(无循环)?

例如:

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

我想在表格中显示它的方式:

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

3 回答 3

2

没有循环:

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

使用 foreach 循环

$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>';
于 2012-10-31T07:38:41.097 回答
1

简单的。使用此语法:

<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>
于 2012-10-31T07:39:25.403 回答
1

你可以使用这样的东西:

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

或者代替那个,你可以使用一个循环:

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

print_r($array);
于 2012-10-31T07:40:42.057 回答