Using Forms in PHP-select option (Drop down list)

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • indhuma
    New Member
    • May 2008
    • 3

    #1

    Using Forms in PHP-select option (Drop down list)

    Hi,
    I'm using the following code to show the category = thoughts. I have three categories, when i select thoughts only it has to show the thoughts category. If i select other category it has to show that particular category details. I don't know how i have to mention the particular category. Please help me.
    [code=php]
    <?php
    if(isset($_POST['submit']) && ($_POST['submit']=="GO"))
    {
    $sqlquery2="sel ect * from news where category like '%thoughts%'";

    $queryresult2 = mysql_query($sq lquery2) or die(mysql_error ());

    while ($arr2=mysql_fe tch_array($quer yresult2))
    {
    echo $arr2["day"]."<br>";

    echo $arr2["category"]."<br>";

    echo $arr2["title"]."<br>";

    echo $arr2["article"]."<br>";
    }
    }
    ?>


    <form action="test2.p hp" method="post">
    <hr>
    <h4>"Category "</h4>
    Select your Category:
    <select name="category" >
    <option value="thoughts ">thoughts</option>
    <option value="travel"> Travel</option>
    <option value="importan t_events" selected="selec ted">Important_ Events</option>
    </select>
    <input type="submit" name="submit" value="GO" />

    </form>

    </body>
    </html>
    [/code]
    Last edited by Atli; May 15 '08, 07:22 AM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hi.

    Would it not work to simply put the option you selected into the query?
    [code=php]
    $category = $_POST['category'];
    $sqlquery2="sel ect * from news where category like '%{$category}%' ";
    [/code]

    Also...
    Please use &#91;code] tags when posting your code examples. (See How to ask a question)

    &#91;code=ph p] ...PHP code goes here... &#91;/code]

    Thanks.

    Comment

    • TheServant
      Recognized Expert Top Contributor
      • Feb 2008
      • 1168

      #3
      Originally posted by indhuma
      [code=php]$sqlquery2="sel ect * from news where category like '%thoughts%'";
      $queryresult2 = mysql_query($sq lquery2) or die(mysql_error ());[/code]
      Also, do you need all (*) of your information? You could replace the above by:
      [PHP]$queryresult = mysql_query (

      SELECT day, category, title, article
      FROM news
      WHERE category = '$category'
      )
      or die(mysql_error ());[/PHP]
      This is probably more efficient I believe. Also, you can use some MySQL coding like I have done. I have capitals for SQL commands and then space it out appropriately for ease of reading.

      Comment

      Working...