Login or Sign up Help | Site Map
Connecting Tech Pros Worldwide

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

Question posted by: indhuma (Newbie) on May 15th, 2008 06:40 AM
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: ( text )
  1. <?php
  2. if(isset($_POST['submit']) && ($_POST['submit']=="GO"))
  3. {
  4. $sqlquery2="select * from news where category like '%thoughts%'";
  5.    
  6. $queryresult2 = mysql_query($sqlquery2) or die(mysql_error());
  7.    
  8. while ($arr2=mysql_fetch_array($queryresult2))
  9. {
  10.       echo $arr2["day"]."<br>";
  11.    
  12.       echo $arr2["category"]."<br>";
  13.    
  14.       echo $arr2["title"]."<br>";
  15.    
  16.       echo $arr2["article"]."<br>";
  17. }
  18. }
  19. ?>
  20.  
  21.  
  22. <form action="test2.php" method="post">
  23. <hr>
  24. <h4>"Category"</h4>
  25. Select your Category:
  26. <select name="category">
  27.   <option value="thoughts">thoughts</option>
  28.   <option value="travel">Travel</option>
  29.   <option value="important_events" selected="selected">Important_Events</option>
  30. </select>
  31. <input type="submit" name="submit" value="GO" />
  32.  
  33. </form>
  34.  
  35. </body>
  36. </html>
Last edited by Atli : May 15th, 2008 at 07:22 AM. Reason: Added [code] tags.
Would you like to answer this question?
Sign up for a free account, or Login (if you're already a member).
Atli's Avatar
Atli
Moderator
1,917 Posts
May 15th, 2008
07:25 AM
#2

Re: Using Forms in PHP-select option (Drop down list)
Hi.

Would it not work to simply put the option you selected into the query?
Code: ( text )
  1. $category = $_POST['category'];
  2. $sqlquery2="select * from news where category like '%{$category}%'";


Also...
Please use [code] tags when posting your code examples. (See How to ask a question)

[code=php] ...PHP code goes here... [/code]

Thanks.

Reply
TheServant's Avatar
TheServant
Needs Regular Fix
462 Posts
May 15th, 2008
10:17 PM
#3

Re: Using Forms in PHP-select option (Drop down list)
Quote:
Originally Posted by indhuma
Code: ( text )
  1. $sqlquery2="select * from news where category like '%thoughts%'";
  2. $queryresult2 = mysql_query($sqlquery2) or die(mysql_error());


Also, do you need all (*) of your information? You could replace the above by:
Code: ( text )
  1. $queryresult = mysql_query (
  2. SELECT day, category, title, article
  3. FROM news
  4. WHERE category = '$category')
  5. or die(mysql_error());

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.

Reply
Reply
Not the answer you were looking for? Post your question . . .
174,853 Experts ready to help you find a solution.
Sign up for a free account, or Login (if you're already a member).

Top PHP Forum Contributors