Jump to content

Archived

This topic is now archived and is closed to further replies.

156418

Different Query Drop Down Menu Choice **Sorted**

Recommended Posts

Hi Everyone,

I'm having a bit of touble with a drop down menu, what I'd like to do it have a drop down menu which shows 5 items:

Saturday 9th December
Sunday 10th December
Saturday 16th December
Sunday 17th December
Saturday 23rd December

and I'd like to run a slightly different query depending on which one is selected.


So for example choosing the first one would run this query and display results:

[code]
<?php
//connect to the database - either incoude a connection variable file or
//type the following lines:
$connect = mysql_connect ("localhost", "mysqlusername", "mysqlspassword") or
die ("Connection to Database Failed.");

mysql_select_db("eventsdb");
$query = "SELECT * FROM products WHERE products_eventdate = '2006-12-09'";

$results = mysql_query($query)
or die(mysql_error());

?>[/code]


and the 2nd one would run this:

[code]<?php
//connect to the database - either incoude a connection variable file or
//type the following lines:
$connect = mysql_connect ("localhost", "mysqlusername", "mysqlspassword") or
die ("Connection to Database Failed.");

mysql_select_db("eventsdb");
$query = "SELECT * FROM products WHERE products_eventdate = '2006-12-10'";

$results = mysql_query($query)
or die(mysql_error());

?>[/code]

Could anyone explain, or point me to a way of doing this? I can only find tutorials so far which populate the drop down from the DB and I dont think I need to be doing that!
The queries all work if I setup different pages - so I know that the query is working ok.

Many thanks for any help

Share this post


Link to post
Share on other sites
I think a <select> could do it

<select name="weekend">
<option  value="1" >Saturday 9th December</option>
<option  value="2" >Sunday 10th December</option>
etc.
</select>

and then :

if ( $weekend == 1 ){
                                    $query="SELECT * FROM products WHERE products_eventdate = '2006-12-09'";
}elseif ( $weekend == 2 ){
                                    $query="etc.etc

Share this post


Link to post
Share on other sites
it is rather simple.. here is an example say you have a form like this

[code]
<form action="sompage.php" method="post">
<select name="date" size="5">
  <option value="whatver">whatever</option>
  <option value"whatever2">Whatever 2</option>
</select><br />
<input type="submit" value="submit" name="submit" />
</form>[/code]

All you have to do in the file that is defined in the action of the form is this.

[code]
<?php
//connect to the database - either incoude a connection variable file or
//type the following lines:
$connect = mysql_connect ("localhost", "mysqlusername", "mysqlspassword") or
die ("Connection to Database Failed.");

mysql_select_db("eventsdb");
$date = mysql_real_escape_string(trim(strip_slashes($_POST['date'])));
$query = "SELECT * FROM products WHERE products_eventdate = '$date'";

$results = mysql_query($query) or die(mysql_error());

//now to display the results do something like this.
while ($rw = mysql_fetch_assoc($results)) {
    //to select a field you start it off with $rw followed by ['theField']
    //like this
    echo 'hello one of your fields is ' . $rw['field'] . ', and another field is ' . $rw['field2'] . '.<br />';
}

?>[/code]

Now you could do the same to display the dropdown box.. like this

[code=php:0]echo '<form action="somepage.php" method="post">
    <select name="date">';
while ($rw = mysql_fetch_array($your_sql_query)) {
   echo '<option value="' . $rw[''products_eventdate'] . '">' . $rw[''products_eventdate'] . '"</option>';
}
echo '</select>
</form>';[/code]

Hope that helps,
Tom

Share this post


Link to post
Share on other sites
Thanks Tom,

Looking at it, I think constructed it ok, its just it doesnt like the strip_slashes comment


[quote]Fatal error: Call to undefined function: strip_slashes()[/quote]

Any ideas to why it would do that, or what I've done to create it!


[b]Edit - Never mind, I noticed the _ in the function that wasnt needed - seems to be working as expected so far![/b]

Share this post


Link to post
Share on other sites

×

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.