我是那些背景一直支持而不是开发的相对 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>';
最后一步也是根据结果集检索和填充表的列标题。提前感谢您的任何指导、建议和改进。
吉姆