Jump to content


Photo

Different Query Drop Down Menu Choice **Sorted**


  • Please log in to reply
3 replies to this topic

#1 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 27 September 2006 - 10:34 AM

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:

<?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());

?>


and the 2nd one would run this:

<?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());

?>

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

#2 enkidu72

enkidu72
  • Members
  • PipPipPip
  • Advanced Member
  • 51 posts

Posted 27 September 2006 - 11:34 AM

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



#3 tomfmason

tomfmason
  • Staff Alumni
  • Advanced Member
  • 1,696 posts
  • Locationstealing your wifi

Posted 27 September 2006 - 11:46 AM

it is rather simple.. here is an example say you have a form like this

<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>

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

<?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 />';
}

?>

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

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>';

Hope that helps,
Tom

Traveling East in search of instruction, and West to propagate the knowledge I have had gained.

current projects: pokersource

My Blog | My Pastebin | PHP Validation class | Backtrack linux


#4 156418

156418
  • Members
  • PipPipPip
  • Advanced Member
  • 31 posts

Posted 27 September 2006 - 12:34 PM

Thanks Tom,

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


Fatal error: Call to undefined function: strip_slashes()


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


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




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users