473,666 Members | 2,058 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 2288
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..Th anks 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
8281
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 dropdown lists. If you change the selection of the left one (e.g. choose parentoption2), it should open up page2.htm in a popup window.
6
10681
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. Let's say you select 1 of the items, and are properly redirected. You press the back button. I have three servers providing two different functionalities: 1. After pressing the back button, the item you selected in the dropdown is still selected.
4
4589
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, just wondering what is wrong. Selected value is always 1 no matter what is selected in the dropdown list box. Private Sub btn_yes_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btn_yes.Click Dim s_origidval As String
2
4545
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 set to...it is set to false. Can someone show me what I am doing wrong and tell me the correct way? Thank you. In the page load event, I am doing the following:
5
11892
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 dropdown list control does not retain its SelectedValue. Unless I read the SelectedValue right after the control has been loaded, populated, and assigned with its original value (and of course that is
3
3520
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 based on the values selected on both the dropdown lists. But this part is not working. It picks up right value for the first dropdown list and always takes the FIRST value of the second dropdown list. I am not sure how to make it use the selected value...
6
5847
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 ID="FldType_add" Runat="server" DataSource='< %#GetFieldType()%>' DataValueField="Type" DataTextField="Type" /> Oce the page is loaded all the values are added to the dropdown list. But when I thought of getting the selected value from the...
0
1978
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 dropdown list width ? Andrus. To reproduce
3
2799
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 these choices will call another dropdown list box when one of the choices from the first gets selected. (I hope this makes sense) All of this is being done on a single form. I know how to make a (sub) dropdown list not seen before the action...
5
6649
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 according to selection made in first dropdown list and it will also come from ms-access database.(for example if i am selecting country as india in first dropdown list then in second dropdown list will show states of india) the problem is that i...
0
8444
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8356
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8869
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8781
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8639
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
5664
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4198
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2771
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 we have to send another system
2
2011
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.