Forum Moderators: coopster
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files WHERE file_name LIKE '%$string%' OR file_desc LIKE '%$string%' OR file_creator LIKE '%$string%' OR file_longdesc LIKE '%$string%' OR file_version LIKE '%$string%'", 0);
in this query there are 5 fields being queried.
can a form and check boxes be used to build the query so that one, some or all of these fields are queried?
dont know enough about php so any help would be great...
thanks
<input type="checkbox" name="fields[]" value="file_name" />
<input type="checkbox" name="fields[]" value="file_desc" />
and so on...
Then in the script, you can build the query with a loop:
$num_fields = count ($_POST['fields']);
for ($i = 0; $i < $num_fields; $i++){
if ($i < ($num_fields-1)) $where = $fields[$i] . " LIKE '%" . $string% . "' OR ";
else $where = $fields[$i] . " LIKE '%" . $string% . "'";
}
Now you have the WHERE part of the query in a variable. Just stick it in the main query now:
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files WHERE $where", 0);
And there you have it! By the way, welcome to Webmaster World!
Birdman
if ($search == "do") {
$string = strip_tags($string, '<a><b><i><u>');
$locbar = "<a href=\"pafiledb.php\" class=\"small\">$config[1]</a> :: <a href=\"pafiledb.php?action=search\" class=\"small\">$str[search]</a> :: $str[results] $string</a>";
if ($logged == 1) {
adlocbar($locbar, $user, $str);
adminlinks($str);
} else {
locbar($locbar);
}
$result = $pafiledb_sql->query($db, "SELECT * FROM $db[prefix]_files WHERE $where", 0);
$numhits = mysql_num_rows($result);
?>
<table width="100%" border="1" cellpadding="2" cellspacing="0" class="headertable" bordercolor="#000000">
<tr><td width="100%" colspan="2" class="headercell"><center><b><?php echo $str[search];?></b></center></td></tr>
<?php
if ($numhits == 0) {
?>
<tr>
<td width="5%" align="center" valign="center" class="datacell"><img src="styles/<?php echo $config[11];?>/images/error.gif" border="0"></td><td width="95%" class="datacell"><?php echo $str[nomatches];?> <b><?php echo $string;?></b></td></tr>
<?php
} else {
?>
<tr><td width="100%" class="datacell" align="center" colspan="2"><?php echo "$numhits $str[matches] <b> $string";?></b></td></tr>
<?php
while ($r = mysql_fetch_object($result)) {
if ($r->file_posticon == "none" or $r->file_posticon == "none.gif" or empty($r->file_posticon)) {
$posticon = " ";
} else {
$posticon = "<img src=\"images/posticons/$r->file_posticon\">";
}
echo "<tr><td width=\"%5\" align=\"center\" class=\"datacell\">$posticon</td><td width=\"95%\" class=\"datacell\" align=\"left\"><a href=\"pafiledb.php?action=file&id=$r->file_id\">$r->file_name</a></td></tr>";
}
}
echo "</table>";
}
if (empty ($search)) {
$locbar = "<a href=\"pafiledb.php\" class=\"small\">$config[1]</a> :: $str[search]</a>";
if ($logged == 1) {
adlocbar($locbar, $user, $str);
adminlinks($str);
} else {
locbar($locbar);
}
?>
Thanks