2012-10-31 40 views
0

簡單的問題, 我怎樣才能使表在PHP中有兩列和10個變量,例如(不循環)?PHP表兩列

例如:

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

我想表明它在路表:

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

蒙山出LOOP ....爲什麼你想試試! –

回答

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

謝謝大家,這個工作的罰款:) – MahmoudMustafa

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> 
1

您可以使用這樣的事情:

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

或代替的,你可以使用一個循環:

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

print_r($array); 
+0

** NO LOOP !!!! ** –

+0

所以它是如此簡單的手工做,沒有環... – 2012-10-31 07:46:41