2012-07-24 84 views
2

我是那些相對的PHP/MySQL新手之一,其背景一直支持,而不是發展。然而,我已經設法把很多事情都放在一起,因爲像這樣的很棒的資源網站,所以我要提前感謝所有過去,現在和未來。然而,我要麼不能構建一個合適的谷歌查詢來查找我需要的信息,要麼我錯過了我認爲的一個簡單技巧。PHP MySql - 通過複選框自定義列顯示選擇

總之,我有一個具有大約15列(字段)的表的MySQL數據庫。我有一個php前端,我可以查詢數據庫,或者返回所有行,或者只是包含一般搜索項的所有行,跨所有字段。一切順利,表現良好,效果很好。但是,我想讓用戶選擇他們想要在結果中返回的那些列(字段)。因此,我有一個搜索頁面,列名顯示了相應的複選框和值(列名)。代碼如下:

<form action="result.php" method="post">  Search for* : <input type="text" name="searchterm" /> 
<input type="submit" name="submit" value="Search Database" /> <br /><br /><br /> 
<input type=button value="Select ALL" onclick="select_all(check, true); return false;"> 
<input type=button value="Clear ALL" onclick="select_all(check, false); return false;"> 
<table> 
<tr><th>FirstName<br/><input type="checkbox" id="check" name="field[]" value="FirstName,"></th> 
<th>LastName<br/><center><input type="checkbox" id="check" name="field[]" value="LastName,"></center></th> 
<th>Age<br/><center><input type="checkbox" id="check" name="field[]" value="Age,">  </center></th> 
<th>Instrument<br/><center><input type="checkbox" id="check" name="field[]" value="Instrument,"></center></th> 
<th>Grade<br/><center><input type="checkbox" id="check" name="field[]" value="Grade,"></center></th> 
<th>LandlineNo<br/><center><input type="checkbox" id="check" name="field[]" value="LandlineNo,"></center></th> 
<th>MobileNo<br/><center><input type="checkbox" id="check" name="field[]" value="MobielNo,"></center></th> 
<th>Email<br/><center><input type="checkbox" id="check" name="field[]" value="Email,"></center></th> 
<th>AddressLine1<br/><center><input type="checkbox" id="check" name="field[]" value="AddressLine1,"></center></th> 
<th>AddressLine2<br/><center><input type="checkbox" id="check" name="field[]" value="AddressLine2,"></center></th> 
<th>Town<br/><center><input type="checkbox" id="check" name="field[]" value="Town,"></center></th> 
<th>County<br/><center><input type="checkbox" id="check" name="field[]" value="County,"></center></th> 
<th>Postcode<br/><center><input type="checkbox" id="check" name="field[]" value="Postcode,"></center></th> 
<th>Price<br/><center><input type="checkbox" id="check" name="field[]" value="Price,"></center></th> 
<th>Paid<br/><center><input type="checkbox" id="check" name="field[]" value="Paid,"></center></th> 
<th>LastUpdated<br/><center><input type="checkbox" id="check" name="field[]" value="LastUpdated,"></center></th> 
</tr> 
</table> 
</form> 

所有複選框被指定爲field []作爲名稱,因此輸出到result.php中的數組。進一步讀取一點點後我可以得到陣列用逗號分隔顯示(回波)的選定的列,每:

for ($i=0; $i<count($_POST['field']); $i++){ 
$columns = addslashes($_POST['field'][$i]); 
echo $columns; 
} 

// output example 

LastName,Age,Instrument,Email 

然而,我現在在不知如何獲得只對選定的在結果視圖中檢索列。任何人都可以幫助我,朝着正確的方向前進。我已經得到了這個,但是這不起作用,因爲所有的列都被返回。

$result = mysql_query("select * from TABLE where FirstName like '%$searchterm%' or LastName like '%$searchterm%' or Age like '%$searchterm%' or Instrument like '%$searchterm%' or Grade like '%$searchterm%' or LandlineNo like '%$searchterm%' or MobileNo like '%$searchterm%' or Email like '%$searchterm%' or AddressLine1 like '%$searchterm%' or AddressLine2 like '%$searchterm%' or Town like '%$searchterm%' or County like '%$searchterm%' or Postcode like '%$searchterm%' or Price like '%$searchterm%' or Paid like '%$searchterm%'"); 

echo '<table>'; 
while ($row = mysql_fetch_assoc($result)) { 
if (empty($columns)) { 
    $columns = array_keys($row); 
    echo '<tr><th>'.implode('</th><th>', $columns).'</th></tr>'; 
} 
$resultset[] = $row; 
echo '<tr><td>'.implode('</td><td>', $row).'</td></tr>'; 
} 
echo '</table>'; 

最終碰觸也將檢索並根據結果集填充到表的列標題。預先感謝任何指導,建議和改進。

吉姆

回答

0
$result = mysql_query("select * from TABLE where FirstName like '%$searchterm%' or LastName like '%$searchterm%' or Age like '%$searchterm%' or Instrument like '%$searchterm%' or Grade like '%$searchterm%' or LandlineNo like '%$searchterm%' or MobileNo like '%$searchterm%' or Email like '%$searchterm%' or AddressLine1 like '%$searchterm%' or AddressLine2 like '%$searchterm%' or Town like '%$searchterm%' or County like '%$searchterm%' or Postcode like '%$searchterm%' or Price like '%$searchterm%' or Paid like '%$searchterm%'"); 
$start = FALSE; 
echo '<table><thead><tr>'; 
while ($row = mysql_fetch_assoc($result)) { 
foreach ($row as $column => $value) 
{ 
// Now we print the columns if they aren't printed 
if ($start === FALSE && !empty($value)) 
{ 
echo "<th>$column</th>"; 
} 
} 
// And if the columns were printed now we turn start to TRUE. 
if ($start === FALSE) 
{ 
echo "</tr></thead><tbody>"; 
$start === TRUE; 
} 
echo "<tr>"; 
// Now for each key and value in the array we verify if the user wanted to print the column and we print it. 
foreach ($row as $column => $value) 
{ 
if (!empty($value)) 
{ 
echo "<td>$value</td>"; 
} 
echo '</tr>'; 
} 
} 
echo '</tbody></table>'; 

這應該工作。你可以看到它在評論中的工作原理。

+0

感謝@Adria快速回復。不幸的是,結果並不行爲。它以列表格式在第一個未選擇的列下顯示所有行字段,並顯示所有其他列標題(但空白)。不知道如何添加圖像以幫助顯示結果。 – DevSki 2012-07-24 14:43:43