Jump to content

Recommended Posts

I have built a mqsql database based site, and am using php / dreamweaver. I need the site to have an 'adavnced search section' where you can choose a number of options to search by. So far I have managed to build what you will see below. The search works, but you MUST select an option for each section or else it fails. My question is how do I get it to either ignore a field that has not been posted or use a 'wildcard' to select all form taht section?

For the search page (build in dreamweaver if that makes any difference), here is the code:

[code]
<td><form id="advancedsearch" name="advancedsearch" method="post" action="advancedresults.php">
      <table border="0" class="titles">
        <tr valign="top">
          <td width="105">Gender:
          
          <p>
            <label>
            <span class="maintext">
<input type="radio" name="gender" value="male" />
Male</span></label>
            <span class="maintext"><br />
            <label>
            <input type="radio" name="gender" value="female" />
Female</label>
            <br />
            <label>
            <input type="radio" name="gender" value="children" />
Children</label>
            </span><br />
          </p></td>
          <td width="112">Age group:
            <p>
              <label>
                <span class="maintext">
                <input type="radio" name="agegroups" value="children" />
                Children</span></label>
              <span class="maintext"><br />
              <label>
                <input type="radio" name="agegroups" value="teenager" />
                Teenager</label>
              <br />
              <label>
                <input type="radio" name="agegroups" value="twenties" />
                20's</label>
              <br />
              <label>
                <input type="radio" name="agegroups" value="thirties" />
                30's</label>
              <br />
              <label>
                <input type="radio" name="agegroups" value="forties" />
                40's</label>
              <br />
              <label>
                <input type="radio" name="agegroups" value="fifties" />
                50's</label>
              <br />
              <label>
                <input type="radio" name="agegroups" value="sixties" />
                60+</label>
              </span><br />
            </p></td>
          <td>Genre
            <select name="genre" id="genre">
              <option value="documentary">Documentary</option>
              <option value="corporate">Corporate</option>
              <option value="children's tv">Children's TV</option>
              <option value="animation">Animation</option>
            </select>
            <label></label>
            <p>
            </label>
            <label>Accent
            <select name="accent" id="accent">
              <option>RP</option>
              <option>Cockney</option>
              <option>Birmingham</option>
              <option>Black Country</option>
              <option>Australian</option>
              <option>Norfolk</option>
              <option>West Country</option>
                                                                                                                                                </select>
            </label>
</p>
            <p>
              <input name="Submit" type="submit" id="Submit" value="Submit" />
            </p></td>
        </tr>
      </table>
          </form>    </td>
[/code]


For the recordset in the results page, I have this entered in dreamweaver:
[code]
SELECT *
FROM performers
WHERE gender = 'colname'   AND genre LIKE '%colgenre%'  AND accents LIKE '%colaccent%' AND agegroups LIKE '%colage%'
ORDER BY name ASC
[/code]

which seems to have produced this code:

[code]
$maxRows_Recordset1 = 10;
$pageNum_Recordset1 = 0;
if (isset($_GET['pageNum_Recordset1'])) {
  $pageNum_Recordset1 = $_GET['pageNum_Recordset1'];
}
$startRow_Recordset1 = $pageNum_Recordset1 * $maxRows_Recordset1;

$colaccent_Recordset1 = "0";
if (isset($_POST['accent'])) {
  $colaccent_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['accent'] : addslashes($_POST['accent']);
}
$colage_Recordset1 = "0";
if (isset($_POST['agegroups'])) {
  $colage_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['agegroups'] : addslashes($_POST['agegroups']);
}
$colgenre_Recordset1 = "0";
if (isset($_POST['genre'])) {
  $colgenre_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['genre'] : addslashes($_POST['genre']);
}
$colname_Recordset1 = "0";
if (isset($_POST['gender'])) {
  $colname_Recordset1 = (get_magic_quotes_gpc()) ? $_POST['gender'] : addslashes($_POST['gender']);
}
mysql_select_db($database_connFirstvoice, $connFirstvoice);
$query_Recordset1 = sprintf("SELECT * FROM performers WHERE gender = '%s'   AND genre LIKE '%%%s%%'  AND accents LIKE '%%%s%%' AND agegroups LIKE '%%%s%%' ORDER BY name ASC", $colname_Recordset1,$colgenre_Recordset1,$colaccent_Recordset1,$colage_Recordset1);
$query_limit_Recordset1 = sprintf("%s LIMIT %d, %d", $query_Recordset1, $startRow_Recordset1, $maxRows_Recordset1);
$Recordset1 = mysql_query($query_limit_Recordset1, $connFirstvoice) or die(mysql_error());
$row_Recordset1 = mysql_fetch_assoc($Recordset1);

if (isset($_GET['totalRows_Recordset1'])) {
  $totalRows_Recordset1 = $_GET['totalRows_Recordset1'];
} else {
  $all_Recordset1 = mysql_query($query_Recordset1);
  $totalRows_Recordset1 = mysql_num_rows($all_Recordset1);
}
$totalPages_Recordset1 = ceil($totalRows_Recordset1/$maxRows_Recordset1)-1;

[/code]
The search works fine as long as you have selected an option for each variable. However, not all users will want to narrow it down so much. My question is how do I work things so that the search does not fail if one of the search fields has been left empty. Is there a 'wildcard' option I can choose or can I get it to ignore empty fields?

HELP!!!!

Also, I would like to search wor more than one item in a field, e.g you might select 'documentary' and 'animation' in the genre field, adn I would like it to pick out records that contain either. How do I do this?

Thanks in advance for your help! I took on a project that was fine until advanced search was requested now I am really struggling!
Link to comment
https://forums.phpfreaks.com/topic/12304-search-with-empty-options/
Share on other sites

  • 3 months later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.