2013-07-09 88 views
0

我一直在努力弄清楚這一點,雖然很近,但我無法弄清楚。我的代碼如下...基於表單的多個選擇框生成動態表

<?php 
include('functions.php'); 
connect(); 
if (isset($_POST['submit'])){ 
    $fieldName = $_POST['fieldName']; 
    foreach($fieldName AS $key=>$values){ 
     $fieldList .= $values; 

    } 
    $fieldList = implode(',', $fieldName); //comma separated list 
    echo $rowList; 
    $sql = mysql_query("SELECT ".$fieldList." FROM members"); 
    $row = mysql_fetch_assoc($sql); 
    echo "<table>"; 
    echo "<tr>"; 
    foreach ($row as $col => $value) { 
     echo "<th>"; 
     echo $col; 
     echo "</th>"; 
    } 
    echo "</tr>"; 
    while ($row = mysql_fetch_assoc($sql)){ 
     echo "<tr><td>".$row['city']."</td><td>".$row['firstName']."</td><td>".$row['cellPhone']."</td></tr>";//***HOPING TO GENERATE THIS DYNAMICALLY WITH VARIABLES...MAYBE ANOTHER FOREACH??? 
    } 
    echo "</table>"; 

} 
?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" 
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
<head> 
</head> 
<body> 
<form action="" method="post" name="form"> 
<select name="fieldName[]" multiple="yes" id="fieldName"> 
    <option value="">Select all that apply</option> 
    <option value="city">City</option> 
    <option value="lastName">Last Name</option> 
    <option value="firstName">First Name</option> 
    <option value="state">State</option> 
    <option value="cellPhone">Cell Phone</option> 
</select>   
<input type="submit" name="submit" value="SUBMIT"/> 
</form> 
</body> 
</html> 

我是如此接近,但我不知道如何創建駐留我while循環之間上面的「echo語句」。我的猜測是我會做類似於... $fieldList .= $values;的事情,其中​​語句將根據需要增長,具體取決於用戶在我的下拉框中選擇的項目數。

回答

0

我只是想通了。請找我用來生成動態表上市我正在尋求下面的代碼...

while ($row = mysql_fetch_assoc($sql)){ 
    echo "<tr>"; 
    foreach ($fieldName as $value){ 
     echo "<td>".$row[$value]."</td>"; 
    } 
    echo "</tr>"; 
} 

我希望這可以幫助別人誰是試圖創建一個簡單的報告工具。