473,506 Members | 9,749 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dropdown list - help pls

6 New Member
Hi,

Using the following selection criteria, I am able to list the data from mysql db, but I would like to provide the following options for users: Select All Data, Select None. I am not sure if this is possiible. Any help would be greatly appreciated.


Here's my code

Expand|Select|Wrap|Line Numbers
  1. <h4>SEARCH</h4>
  2.  
  3. <form action="<?php echo $_SERVER['PHP_SELF'] ?>" method = "post" target = "right">
  4.  
  5. //1st DROPDOWN
  6.  
  7. Date<br>
  8. <select size="1" name="search">
  9. <?php
  10.  
  11. $con = mysql_connect("localhost","****","****");
  12. if (!$con) {die('Database is down: ' . mysql_error());}
  13. mysql_select_db("test",$con);
  14.  
  15. $query = ("select distinct date from **** order by date desc");
  16. $result = mysql_query($query) or die(mysql_error());
  17. while($row = mysql_fetch_array($result)){
  18. echo "<option value=\"$row[date]\">$row[date]</option>";
  19. }
  20. ?>
  21. </select>
  22.  
  23.  
  24. //2nd DROPDOWN
  25.  
  26. <br><br>
  27. Product<br>
  28. <select size="1" name="search1">
  29. <?php
  30. $con = mysql_connect("localhost","****","****");
  31. if (!$con) {die('Database is down: ' . mysql_error());}
  32. mysql_select_db("test",$con);
  33.  
  34. $query = ("select distinct productname from *** order by productname desc");
  35. $result = mysql_query($query) or die(mysql_error());
  36. while($row = mysql_fetch_array($result)){
  37. //echo "<option value=\"$row[date]\">.$row[date].'</option>'";
  38. echo "<option value=\"$row[productname]\">$row[productname]</option>";
  39. }
  40. ?>
  41. </select>
  42.  
  43. <input type="Submit" value="Submit" name="Submit">
  44. </form>
  45.  
  46.  
  47.  
  48. //ASSINGING THE SELECTED FIELDS TO A VARIABLE
  49.  
  50. $search = empty($_POST['search'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search']); 
  51.  
  52. $search1 = empty($_POST['search1'])? die ("ERROR: Enter Search Criteria") : mysql_escape_string($_POST['search1']); 
  53.  
  54.  
  55.  
  56. //QUERY
  57. $query = "SELECT * FROM ***  where date = '$search' and productname = '$search1'  or die (mysql_error()); 
  58. $result = mysql_query($query) or die (mysql_error()); 
  59. $num = mysql_numrows($result); 
  60.  
  61. mysql_close($connect); 

//OUTPUT
***
Dec 25 '09 #1
7 2279
Dormilich
8,658 Recognized Expert Moderator Expert
of course this is possible. you can use for instance a dropdown field (or radios) to select a fixed SQL statement.

ex.
Expand|Select|Wrap|Line Numbers
  1. // HTML
  2. <input type="radio" name="sql" value="1" checked>
  3. <input type="radio" name="sql" value="2">
  4. <input type="radio" name="sql" value="3">
Expand|Select|Wrap|Line Numbers
  1. // PHP
  2. $sql_type = (int) $_POST['sql'];
  3. if (1 == $sql_type) // that's only one possibility
  4. {
  5.     $sql = "SELECT * FROM mytable";
  6. }
  7. elseif (2 == $sql_type)
  8. {
  9.     $sql = "SELECT `id` FROM mytable";
  10. }
  11. // etc.
Dec 25 '09 #2
microsoftboy
6 New Member
Thanks a lot.

I am rewriting my code, I will post the results soon..
Dec 26 '09 #3
microsoftboy
6 New Member
imm..I think I am making some mistakes..

I've modified my code as mentioned below--

...

Expand|Select|Wrap|Line Numbers
  1. <input type="radio" name="product[]" value="1" /> Select All <br>
  2. <input type="radio" name="product[]" value="2" /> ProductName1 <br>
  3. <input type="radio" name="product[]"  value="3" /> ProductName2<br>
  4.  
  5. <input type="Submit" value="Submit" name="Submit">
  6. </form>
  7. ..
  8. ..
  9.  
  10. $connect = mysql_connect($host,$user,$password) or die ("Unable to connect to host"); 
  11. mysql_select_db($db) or die ("Unable to connect to database"); 
  12.  
  13.  
  14.  
  15. $sql_type = (int) $_POST['product']; 
  16.  
  17. if (1 == $sql_type) // that's only one possibility 
  18.     $prodcut = "SELECT distinct productname FROM mytable"; 
  19. elseif (2 == $sql_type) 
  20.     $sql = "SELECT productname FROM environment_performance WHERE productname='ProductName1'"; 
  21. elseif (3 == $sql_type) 
  22.     $sql = "SELECT productname FROM mytable WHERE productname='ProductName2'"; 
  23. }
  24.  
  25.  
  26. $query = "SELECT * FROM mytable where productname ='$product'" or die (mysql_error()); 
  27. $result = mysql_query($query) or die (mysql_error()); 
  28. $num = mysql_numrows($result);  
  29.  
  30. mysql_close($connect); 
  31.  
  32. //OUTPUT GOES HERE
Note: I can see only the value 2 so am not sure you know.


When users select - select all then the output should list all the column values where productname = ProductName1 and ProductName2

Any help would be really appriciated..Thanks agian!
Dec 27 '09 #4
Dormilich
8,658 Recognized Expert Moderator Expert
well, why don't you use the selected SQL? you set the query string $sql via form and in your query you use $query instead of $sql. and you should not name the radio boxes "product[]" (there is no need to use an array)

tip: in the if-elseif conditions, use an else block in case the input does not match any previous condition (this may be another SQL statement as well as an Exception (Error message))

Expand|Select|Wrap|Line Numbers
  1. if (1 == $sql_type)
  2. { ... }
  3. elseif (2 == $sql_type)
  4. { ... }
  5. else
  6. {
  7.     throw new Exception("Incorrect choice of the SQL query.");
  8. }
note for the HTML: the text that belongs to an input element should be put into a label
Expand|Select|Wrap|Line Numbers
  1. <input type="radio" value="2" name="product" id="product2">
  2. <label for="product2">ProductName 2</label>
Dec 27 '09 #5
microsoftboy
6 New Member
Ok sure, Investigating...

Thanks again!
Dec 27 '09 #6
microsoftboy
6 New Member
Hi,

I was able to select all or none by using the checkboxes as mentioned below.

<tr>
<td>
Product
</td>
<td>
<input type="checkbox" name="product[]" value="AP" />AP
<input type="checkbox" name="product[]" value="AR" />AR
<input type="checkbox" name="product[]" value="GL" />GL
<input type="checkbox" name="product[]" value="COM" />COM<br />
</td>
</tr>



$product_array = $_POST['product'];
//echo $product_array;
foreach ($product_array as $one_product) {
$source .= $one_product.", ";
}
$product = substr($source, 0, 100);



Thanks a lot.
Dec 29 '09 #7
microsoftboy
6 New Member
I was able to get it working by your idea as well..

i.e
Expand|Select|Wrap|Line Numbers
  1. $sql_type = (int) $_POST['sql']; 
  2. if (1 == $sql_type) // that's only one possibility 
  3.     $sql = "SELECT * FROM mytable"; 
  4. elseif (2 == $sql_type) 
  5.     $sql = "SELECT `id` FROM mytable"; 

Thanks again!
Jan 11 '10 #8

Sign in to post your reply or Sign up for a free account.

Similar topics

1
8254
by: Joseph Barron | last post by:
Here is a SIMPLE problem that I'm trying to solve. It works in Netscape 6.2, but IE6 gives ""No such interface supported." Below are page1.htm and page2.htm . In page1.htm, there are two...
6
10653
by: Mark | last post by:
I have two dropdown lists. Both have autopostback set to true. In both dropdowns, when you select an item from the list, it redirects to the Value property of the dropdown. Nothing fancy. ...
4
4576
by: Paul | last post by:
I have a dropdown list box and a button on a web form, the autopost back is false for the dropdown list box and button. When the button is pushed the selection in the dropdownlist box is lost,...
2
4532
by: Mike Collins | last post by:
I cannot get the correct drop down list value from a drop down I have on my web form. I get the initial value that was loaded in the list. It was asked by someone else what the autopostback was...
5
11869
by: jung_h_park | last post by:
From: jung_h_park@yahoo.com Newsgroups: microsoft.public.dotnet.framework.aspnet Subject: Dropdown List not retaining its SelectedValue Date: Mon, 26 Jun 2006 21:02:57 -0700 Hello, My...
3
3504
by: er1 | last post by:
Hi all, I have created a double dropdown list. Based on the first list selection, second list populates (this works fine). I have a submit button, which when clicked should run a select query...
6
5836
by: yasodhai | last post by:
Hi, I used a dropdown control which is binded to a datagrid control. I passed the values to the dropdownlist from the database using a function as follows in the aspx itself. <asp:DropDownList...
0
1970
by: Andrus | last post by:
I'm using WinForms DataGridView I need to make dropdown list wider that grid column width. I tried the following code, but dropdown list widht is the same as column width. How to increase...
3
2786
by: fish919 | last post by:
Hello All, I am creating a date base in access. I want to create a dropdown list box that is connected to another dropdown list box. You start with a dropdown list that has 5 choices and each of...
5
6640
by: abhi3211 | last post by:
i am using java inside java script page. in that page i want to use two dropdown list. in first dropdown list i am getting data from ms-access database. in second dropdown list i want to get data...
0
7220
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
7308
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
7371
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
7479
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
1
5037
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
4702
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
3188
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
3178
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
0
1534
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.