How to code the <option> statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • bb nicole
    New Member
    • Jan 2007
    • 127

    How to code the <option> statement

    Below is the list menu of search engine.. How to code if i want to put
    Code:
    <option selected>ALL</option>
    Interface
    Code:
    <tr> 
        <td>Job Category:</td>
        <td><select name="jobCategory">
            <option selected></option>
            <option>Accounting</option>
            <option>Advertising</option>
            <option>Agriculture</option>
            <option>Banking</option> 
    </select></td>
      </tr>
      <tr> 
        <td>Job Location:</td>
        <td><select name="jobLocation">
            <option selected></option>
            <option>Perlis</option>
            <option>Penang</option>
            <option>Kedah</option> 
    </select></td>
      </tr>

    php code
    Code:
    $sql8 = "SELECT * FROM job";
    elseif ($keyword== ""&&$jobCategory==""&&$jobLocation=="") {
    $data = mysql_query($sql8)
    or die("Cannot execute query");
    }
    The code above is function, but when i add ALL between <option selected></option> as below:
    <option selected>ALL</option>

    it cannot function, so, can anyone give me some guildeline... Thanks..
  • ronverdonk
    Recognized Expert Specialist
    • Jul 2006
    • 4259

    #2
    Why don't you have a value attached to your <option> statements? How do you e.g. know that option Accounting has been selected?? The usual format to code an option statement is e.g.

    Code:
    <option value="Accounting">Accounting</option>
    so you can check the existence of this value in the 'Category' field passed to you.

    Also an option like <option selected>ALL</option> is coded as
    Code:
    <option value='ALL' selected='selected'>ALL</option>
    My question here is, what are you trying to accomplish with these options? In my opinion none of your selects/options will work.

    Ronald :cool:

    Comment

    • bb nicole
      New Member
      • Jan 2007
      • 127

      #3
      Originally posted by ronverdonk
      Why don't you have a value attached to your <option> statements? How do you e.g. know that option Accounting has been selected?? The usual format to code an option statement is e.g.

      Code:
      <option value="Accounting">Accounting</option>
      so you can check the existence of this value in the 'Category' field passed to you.

      Also an option like <option selected>ALL</option> is coded as
      Code:
      <option value='ALL' selected='selected'>ALL</option>
      My question here is, what are you trying to accomplish with these options? In my opinion none of your selects/options will work.

      Ronald :cool:
      Emm...Isn't both
      Code:
      <option>Accounting</option>
      and
      Code:
      ]<option value="Accounting">Accounting</option>
      is same??
      Actually i didn't very know what the use of put the value in the option, so i didn't use it... Usually, i use
      Code:
      <option>Accounting</option>
      , i use it in register form, search engine and so on... And in register form, what i'm select will enter to database... And in search engine, it will display the result according to what i'm select...
      The register form and search engine can work properly, so i don't know whether is have a problem or not...

      What i want to do is when the users click the search button without key in anything(keywor d, or job category or job location), all the job in database will display out as a result.

      Code:
      $sql8 = "SELECT * FROM job";
      if ($keyword== ""&&$jobCategory==""&&$jobLocation=="") {
      $data = mysql_query($sql8)
      or die("Cannot execute query");
      }
      I want to add ALL between <option selected></option> in list menu, so the user will know all the job will display out when they didn't key in anything and click the search button. But the code above will leave a blank if the user didn't key in anything.
      Although it can display all the job after the user click the search button, but the users would not know that all the job will display if they just click the search button..

      So, what should i do now besides have a value attached to my <option> statements??
      Thanks..

      Comment

      • ronverdonk
        Recognized Expert Specialist
        • Jul 2006
        • 4259

        #4
        No, I was only showing that you should use the value in <option> because often the value is different from the text. But in your case you can use this, althought it is not 'clean' programming practise.

        So for your ALL selection, I would advise you to test the ALL value instead of testing the 'emptiness' of the other values.

        Your dropdown list code here would be something like:[php]
        <tr>
        <td>Job Category:</td>
        <td><select name="jobCatego ry">
        <option selected='selec ted'>ALL</option>
        <option>Account ing</option>
        <option>Adverti sing</option>
        <option>Agricul ture</option>
        <option>Banking </option>
        </select></td>
        </tr>
        <tr>
        <td>Job Location:</td>
        <td><select name="jobLocati on">
        <option selected='selec ted'>ALL</option>
        <option>Perli s</option>
        <option>Penan g</option>
        <option>Kedah </option>
        </select></td>
        </tr>
        [/php]
        And the sql build would then be (I don't know what variable $keyword contains so I took it out, but you can put it back when required):[php]
        $sql8 = "SELECT * FROM job";
        if ($_POST['jobCategory'] == "ALL" AND
        $_POST['jobLocation'] == "ALL") {
        $data = mysql_query($sq l8)
        or die("Cannot execute query");
        }[/php]

        Ronald :cool:

        Comment

        • bb nicole
          New Member
          • Jan 2007
          • 127

          #5
          Originally posted by ronverdonk
          No, I was only showing that you should use the value in <option> because often the value is different from the text. But in your case you can use this, althought it is not 'clean' programming practise.

          So for your ALL selection, I would advise you to test the ALL value instead of testing the 'emptiness' of the other values.

          Your dropdown list code here would be something like:[php]
          <tr>
          <td>Job Category:</td>
          <td><select name="jobCatego ry">
          <option selected='selec ted'>ALL</option>
          <option>Account ing</option>
          <option>Adverti sing</option>
          <option>Agricul ture</option>
          <option>Banking </option>
          </select></td>
          </tr>
          <tr>
          <td>Job Location:</td>
          <td><select name="jobLocati on">
          <option selected='selec ted'>ALL</option>
          <option>Perli s</option>
          <option>Penan g</option>
          <option>Kedah </option>
          </select></td>
          </tr>
          [/php]
          And the sql build would then be (I don't know what variable $keyword contains so I took it out, but you can put it back when required):[php]
          $sql8 = "SELECT * FROM job";
          if ($_POST['jobCategory'] == "ALL" AND
          $_POST['jobLocation'] == "ALL") {
          $data = mysql_query($sq l8)
          or die("Cannot execute query");
          }[/php]

          Ronald :cool:


          Thanks again, Ronald, it is work... But now the problem happen in other query..
          Below is my code for the search engine...
          [PHP]<?php
          //required file for database connection
          include("config .php");
          if (isset($_POST['keyword'])) {
          $keyword = mysql_real_esca pe_string($_POS T["keyword"]);
          $jobCategory=$_ POST["jobCategor y"];
          $jobLocation=$_ POST["jobLocatio n"];
          ?>

          <p align=center><c enter><font face='Arial' size='3'><stron g>Search Results</strong></p>


          <?php
          $sql = "SELECT * FROM job";


          $string1 = array();
          $where1 = "";
          if (isset($_POST["keyword"]) AND !empty($_POST["keyword"]))
          $string1[] = " jobTitle LIKE '%".$_POST["keyword"]."%' ";
          if (isset($_POST["jobCategor y"]) AND !empty($_POST["jobCategor y"]) AND ($category != Others))
          $string1[] = " jobCategory LIKE '%".$_POST["jobCategor y"]."%' ";
          if (isset($_POST["jobLocatio n"]) AND !empty($_POST["jobLocatio n"]))
          $string1[] = " jobLocation LIKE '%".$_POST["jobLocatio n"]."%' ";
          if (!empty($string 1))
          $where1 = " WHERE ".implode("AND" , $string1);// OR/AND
          $sql1="SELECT * FROM job $where1";


          $string2 = array();
          $where2 = "";
          if (isset($_POST["keyword"]) AND !empty($_POST["keyword"]))
          $string2[] = " jobTitle LIKE '%".$_POST["keyword"]."%' ";
          if (isset($_POST["jobCategor y"]) AND !empty($_POST["jobCategor y"]) AND ($category != Others))
          $string2[] = " jobCategory LIKE '%".$_POST["jobCategor y"]."%' ";
          if (isset($_POST["jobLocatio n"]) AND $_POST['jobLocation'] == "All")
          $string2[] = $_POST['jobLocation'] == "All";
          if (!empty($string 2))
          $where2 = " WHERE ".implode("AND" , $string2);// OR/AND
          $sql2="SELECT * FROM job $where2";


          $string3 = array();
          $where3 = "";
          if (isset($_POST["keyword"]) AND !empty($_POST["keyword"]))
          $string3[] = " jobTitle LIKE '%".$_POST["keyword"]."%' ";
          if (isset($_POST["jobLocatio n"]) AND !empty($_POST["jobLocatio n"]))
          $string3[] = " jobLocation LIKE '%".$_POST["jobLocatio n"]."%' ";
          if (!empty($string 3))
          $where3 = " WHERE ".implode("AND" , $string3);// OR/AND
          $sql3="SELECT * FROM job $where3";


          $string4 = array();
          $where4 = "";
          if (isset($_POST["jobCategor y"]) AND !empty($_POST["jobCategor y"]) AND ($category != Others))
          $string4[] = " jobCategory LIKE '%".$_POST["jobCategor y"]."%' ";
          if (isset($_POST["jobLocatio n"]) AND !empty($_POST["jobLocatio n"]))
          $string4[] = " jobLocation LIKE '%".$_POST["jobLocatio n"]."%' ";
          if (!empty($string 4))
          $where4 = " WHERE ".implode("AND" , $string4);// OR/AND
          $sql4="SELECT * FROM job $where4";


          $string5 = array();
          $where5 = "";
          if (isset($_POST["keyword"]) AND !empty($_POST["keyword"]))
          $string5[] = " jobTitle LIKE '%".$_POST["keyword"]."%' ";
          if (!empty($string 5))
          $where5 = " WHERE ".implode("AND" , $string5);// OR/AND
          $sql5="SELECT * FROM job $where5";


          $string6 = array();
          $where6 = "";
          if (isset($_POST["jobCategor y"]) AND !empty($_POST["jobCategor y"]) AND ($category != Others))
          $string6[] = " jobCategory LIKE '%".$_POST["jobCategor y"]."%' ";
          if (!empty($string 6))
          $where6 = " WHERE ".implode("AND" , $string6);// OR/AND
          $sql6="SELECT * FROM job $where6";


          $string7 = array();
          $where7 = "";
          if (isset($_POST["jobLocatio n"]) AND !empty($_POST["jobLocatio n"]))
          $string7[] = " jobLocation LIKE '%".$_POST["jobLocatio n"]."%' ";
          if (!empty($string 7))
          $where7 = " WHERE ".implode("AND" , $string7);// OR/AND
          $sql7="SELECT * FROM job $where7";


          // Call for this $sql
          if ($keyword==""&& $_POST['jobCategory'] == "All"&&$_PO ST['jobLocation'] == "All") {
          $data = mysql_query($sq l)
          or die("Cannot execute query");
          }

          elseif(!empty($ keyword)&&!empt y($jobCategory) &&!empty($jobLo cation)){
          $data = mysql_query($sq l1)
          or die("Cannot execute query");
          }


          elseif(!empty($ keyword)&&!empt y($jobCategory) &&$_POST['jobLocation'] == "All"){
          $data = mysql_query($sq l2)
          or die("Cannot execute query");
          }


          elseif(!empty($ keyword)&&$_POS T['jobCategory'] == "All"&&!empty($ jobLocation)){
          $data = mysql_query($sq l3)
          or die("Cannot execute query");
          }

          elseif($keyword ==""&&!empty($j obCategory)&&!e mpty($jobLocati on)){
          $data = mysql_query($sq l4)
          or die("Cannot execute query");
          }


          elseif(!empty($ keyword)&&$_POS T['jobCategory'] == "All"&&$_PO ST['jobLocation'] == "All"){
          $data = mysql_query($sq l5)
          or die("Cannot execute query");
          }


          elseif($keyword ==""&&!empty($j obCategory)&&$_ POST['jobLocation'] == "All"){
          $data = mysql_query($sq l6)
          or die("Cannot execute query");
          }


          elseif($keyword ==""&&$_POST['jobCategory'] == "All"&&!empty($ jobLocation)){
          $data = mysql_query($sq l7)
          or die("Cannot execute query");
          }
          ?>[/PHP]

          The search engine is such kind like below:
          Keyword:
          Job Category:
          Job Location:
          The users can search the result for job either fill in all the 3 field, 1 or 2 of the field or didn't fill in everything...
          The problem now is the list menu for job category and job location which supposed empty last time now is change to All.. It only can execute the query $sql and $sql1, the others($sql2, $sql3, $sql4, $sql5, $sql6, $sql7) failed to search the job ...
          Can u give me some guildeline?? Thanks..:)

          Comment

          • ronverdonk
            Recognized Expert Specialist
            • Jul 2006
            • 4259

            #6
            I would start to echo the content of $sql2 ... etc. so you can see that the statement is correct. Chance is that there is something that is not quite what you would expect.

            When you are sure it is correct, only test e.g. $sql2 and build some echoes around it to see if and how it executes.

            But first echo the content of the statement to be certain.

            Ronald :cool:

            Comment

            Working...