Jump to content

Dropdown menu filter help


phpnewbiemio

Recommended Posts

Hello,

 

I'm new to php, I don't really have much experience, just have used it in combination with dreamweaver. I'm tackling a new problem now.

 

Basically, it's about a dropdown menu with pre-defined values. There are multiple dropdowns, having different value choices. People are supposed to be able to select more than one option and then search for the results. these should then be generated according to the selected values, or if no value is selected, ALL is shown.

 

When the user clicks on search, the values create a msql query that will then be executed and the results shown on the page.

 

I've been searching high and low for code ideas, but see different stuff everywhere I look, and none of the code seems to be what i want. Some people are using Javascript, some Ajax, or PHP. ideally, I want to use just php and sql

 

Can someone point me in the right direction on how to tackle this problem, or maybe someone has some code ideas?

 

Thanks in advance.

 

Mio

 

 

 

 

Link to comment
Share on other sites

this is going to seem like i am giving you a hard time, but how about taking the information you have about the pre-defined values the dropdown menus will submit to your php code (i.e. the input data), then try to write some code that does what you want and produces the WHERE clause you need for the sql query statement?

 

do you even have the search form coded so that you could test to make sure it is submitting the proper values to the php code and so that you could experiment with the php code you are writing to see if it produces the expected result for each of the expected combinations of inputs? i recommend use a method='get' form, since you are telling the page what data to get/display.

Link to comment
Share on other sites

Here's something to get you started. If I read your post correct you wanted a multiple select drop-down. If not then remove the "multiple" tag from the select and the value the user selected won't be in an array, but in the $_POST['condiments'] value itself. 

 

HTML

<form name="form1" method="post" action="process_form.php">

<select name="condiments[]" multiple>
<option value="olives">Olives</option>
<option value="pickles">Pickles</option>
<option value="onions">Onions</option>
</select>
<input type="submit" value="submit">

</form>

PHP

<?php

if( isset($_POST['condiments']) )
{
    $condiments_selected = array();

    foreach($_POST['condiments'] as $condiment)
    {
        $condiments_selected[] = $condiment;
    }

    // here you can build your sql statement to insert into database

}
?>

Hopefully others will chime in on the database insert part - if not, I'll post that part later as well.

Link to comment
Share on other sites

Thanks for the help!! I think it was unclear, I just meant people can select 1 option from different dropdowns and then get a result.

 

so basically the statement is something like

 

WHERE option1 AND (option 2 , 3, 4, 5)

 

I do have the html for the drowpdown menu already, I'm just failing at the php. (Maybe I should just hire someone for this piece of code :) )

Link to comment
Share on other sites

Pay yourself with the challenge and reward of coding it yourself.

Your friends will be amazed, want to borrow money and drop off all of their old, porn and malware affected machines at your doorstep for you to fix. The hot neighbor girl will drop off her....computer as well.

Mac_gyver and those guys will be much more willing to help if they see you've put in some effort first (posting your effort thus far - the code you've written/attempted)

<?php

// set up database variables or import them from config file
$dbname = "database_name";
$user = "super_developer";
$pass = "man_o_steele";

$condiments_selected = array();

if( isset($_POST['condiments']) )
{

foreach($_POST['condiments'] as $condiment)
{
   $condiments_selected[] = $condiment;
}

// create query
$query = 'INSERT INTO tablename(column_name_1, column_name_2, column_name_3)
VALUES(:choice1, :choice2, :choice3)';

// query check
// echo $query;

// fire up the database...vrrrm! vrrrm!
try
{
   $con = new PDO("mysql:host=localhost;dbname=$dbname",
   $user, $pass, array(PDO::MYSQL_ATTR_INIT_COMMAND => "SET NAMES utf8"));
   con->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
   $stm = $con->prepare( $query );
}
catch(PDOException( $e ))
{
   echo($e->getMessage());
}

$stm->bindValue(':choice1', $condiments_selected[0]);
$stm->bindValue(':choice2', $condiments_selected[1]);
$stm->bindValue(':choice3', $condiments_selected[2]);
$stm->execute();

} // end if( isset($_POST['condiments'] )
Link to comment
Share on other sites

a recommendation - even if your drop-down menus have pre-defined values, you should store those values in a data structure of some type (database table, array) so that you can both dynamically produce the drop-downs and use that same data structure to validate the submitted data. you can then change the drop-downs to be whatever you want simply by changing the defining data. no need to edit hard-coded html every time you want to add, remove, or change an item or reuse your code for a completely different project.

  • Like 1
Link to comment
Share on other sites

 

 

a recommendation - even if your drop-down menus have pre-defined values, you should store those values in a data structure of some type (database table, array) so that you can both dynamically produce the drop-downs and use that same data structure to validate the submitted data. you can then change the drop-downs to be whatever you want simply by changing the defining data. no need to edit hard-coded html every time you want to add, remove, or change an item or reuse your code for a completely different project.

 

This recommendation will save you countless hours and you can drag the code over and easily apply it to every client you have.

 

A simple example:

<?php
require('common_functions.php');
?>
<html>
<head>
</head>
<body>
<?php

$dropdown_to_add = get_state_dropdown_menu('menu_name');
echo "<select name=\"menu_name\">\r\n";

foreach($dropdown_to_add as $name => $value)
{
   echo "<option name=\"$name\" value=\"$value\">$value</>\r\n";
}
echo "</select>\r\n";
?>

Anyways...you can create generic functions to return an array of the data you need or they can even return the html with the data, name etc. just passing it arguments of what you want. 

Link to comment
Share on other sites

Thanks for all the suggestions. I've read so much about it in the last few days, that my head is seriously hurting :) I've tried out many things, but my code is too embarassing to display ;)

 

I can live with the hot neighbour not showing me her.... computer ;)

 

I'm going to hire someone to write the code for me, you can make me an offer too ;) pm me

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.