Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7398943
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T03:56:20+00:00 2026-05-29T03:56:20+00:00

I am working on building a PHP based filter that will narrow down a

  • 0

I am working on building a PHP based filter that will narrow down a search based on criteria specified by drop downs in the HTML file. In the MySQL file I have a column for a image_url, client, and service. When the search is complete I would like to display the image, client name, and services.

Currently I have the PHP setup to seach based on one specific set of criteria. I would like for it to take into account the extra set of perameters. How can I do this? Thanks in advance!

This is what I have so far.

HTML

<!DOCTYPE HTML>
<html>
 <head>
  <meta charset="UTF-8" />
  <title>Filter</title>
 </head>
<body>
 <br />
<form action="filter.php" method="post">
  Search by:
  <br />
Client:
<select name="client">
  <option value="any">- Any -</option>
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
  <option value="3">Test 3</option>
</select>

 Services: 
<select name="services">
  <option value="any">- Any -</option>
  <option value="1">Test 1</option>
  <option value="2">Test 2</option>
</select>

 <input type="submit" value="Submit" />
</form>
</body>
</html>

PHP

<?php
            // declare sting variable and set value to link to index.html


            // connect to database and select database 'directory'

            $con = mysql_connect("localhost","username","123456");
            if (!$con)
            {
                  die('Could not connect: ' . mysql_error());
            }

            mysql_select_db("filter", $con);

            $name = 'name';
            if (isset($_REQUEST['client']))
            {
                  $name = trim($_REQUEST['client']);
            } else {
                  echo "Sorry, no search criteria.";

                  exit;
            }

            // contains the string input by the user on the name.html page

            $query = "SELECT * FROM filter WHERE client LIKE '".mysql_real_escape_string($client)."%'";

            $result = mysql_query($query) or die( "Problem executing: ". $query . " " . mysql_error() );

            // if no results are returned from the query, give error message and exit

            if (mysql_num_rows($result) == 0) {
                  echo "Sorry, no matching results.";
                  exit;
            }

            while ($row = mysql_fetch_assoc($result)) {



                       echo "<img src="<?php echo $row["image_url"]; ?>"/> ;
                        echo "<b>Client: </b>";
                  echo $row["client"] ;
                  echo  "&nbsp;";
                  echo "<b>Services: </b>";
                  echo $row["services"];
                  echo "<br>";
                  echo "<br>";

            }
            ?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T03:56:21+00:00Added an answer on May 29, 2026 at 3:56 am

    If I understand the question correctly, you are after something like this:

    $select = 'SELECT image_url, client, service from filter';
    $criteria = array_filter(
        filter_input_array(
            INPUT_POST, 
            array(
                'client' => FILTER_VALIDATE_INT,
                'services' => FILTER_VALIDATE_INT
            )
        )
    );
    if (!empty($criteria)) {
        array_walk($criteria, function(&$val, $column) {
            $val = sprintf('%s = %d', $column, $val);
        });
        $select .= ' WHERE ' . implode(' AND ', $criteria);
    }
    

    Explanation

    When you submit your form, your $_POST array will look like this for example:

    array(
        'client' => 'any',
        'services' => 1
    );
    

    Note that you do not want to use $_REQUEST unless you understood the order in which it is populated.

    Ok, so your $_POST array contains the criteria you want to add to your base query:

    $select = 'SELECT image_url, client, service from filter';
    

    I’m not sure why you used LIKE when you are going to use the fixed values from the Select options, so let’s transform the $_POST array to a simple WHERE condition only. Since you only want the values the base query can accept, we need to do some filtering first:

    $criteria = array_filter(
        filter_input_array(
            INPUT_POST, 
            array(
                'client' => FILTER_VALIDATE_INT,
                'services' => FILTER_VALIDATE_INT
            )
        )
    );
    

    The filter_input_array will pull the values for client and service from the ´$_POST´ array and check whether their values are integers. The array_filter function takes care of removing any elements for which the validation failed. For the example array above, the $criteria variable will now contain

    array(2) {
      ["services"]=>
      string(1) "1"
    }
    

    As you can see, ‘any’ got stripped because it is not an integer. Cool, because now all you got to do is array_walk it

    if (!empty($criteria)) {
        array_walk($criteria, function(&$val, $column) {
            $val = sprintf('%s = %d', $column, $val);
        });
        $select .= ' WHERE ' . implode(' AND ', $criteria);
    }
    

    This code looks much more complicated than it really is: if there is any values in $criteria, we overwrite the values to columnname = value where value can only be a number. You’ll end up with

    Array
    (
       [services] => services = 1
    ) 
    

    After that we append those values to the $select by imploding each value with AND.

    Your $select variable will then contain

    SELECT image_url, client, service from filter WHERE services = 1
    

    If your $_POST array contained for example:

    $array = array(
        'client' => '3',
        'services' => 1,
    );
    

    the resulting $select would be:

    SELECT image_url, client, service from filter WHERE client = 3 AND services = 1
    

    If your $_POST array contained two ‘any’ values, $select would just contain the base query.

    The code snippet above can easily be extended to contain additional criteria. You just need to add the allowed keys to the filter function. The remaining code works with an arbitrary amount of array values in $criteria as long as the criteria values are numeric.

    If your needs get more complex, for instance because you need to add OR, LIKE and IN in the condition, consider using a query builder instead of hand coding it (not that it would be difficult).

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am working on building a small php/mysql script that will act something like
I'm working on building a development tool that is written in JavaScript. This will
I'm working on building a PHP based proxy script to access a particular ASP.NET
I am working on building my first search-engine friendly CMS. I know that perhaps
We're building a PHP app based on Good old codeigniter framework and have run
I'm building my own website in PHP, somehow my tracking has stopped working since
I am currently working on a building community website in PHP. This contains forms
I'm building my first application that requires a calendar. I have it working great
I'm building a web control that will let our junior IT staff manage firmware
I am new to Multi-Dimensional arrays and working on building my php skills as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.