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

  • SEARCH
  • Home
  • 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 6669701
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T03:10:53+00:00 2026-05-26T03:10:53+00:00

i set up a multi select form to return arrays and then looped them

  • 0

i set up a multi select form to return arrays and then looped them through mysql query to return results

i dont want duplicate results if the user selects multiple options and those
options are in one record

for example user selects three different ‘Views’ and one property has all three views i dont want that displayed in the results three times … thank you if you can help

    require ('db.php');

    $N = $_GET['Neigh'];
   $V = $_GET['view'];
   $C = $_GET['Con'];
  $F = $_GET['front'];
 $minPrice = $_GET['minprice'];
 $maxPrice = $_GET['maxprice'];
 $Year = $_GET['YearBuilt'];



   foreach($N as $Nvalue){
   if ($Nvalue != "\n\r" || $Nvalue != "" || $Nvalue !=NULL)
   foreach($C as $Cvalue){
   foreach($F as $Fvalue){
   foreach($V as $Vvalue){

   $query="SELECT *
   FROM `foo`
   WHERE `Building` LIKE '%{$Bvalue}%' && `Neigh` = '{$Nvalue}' && `View` 
   LIKE  '%{$Vvalue}%' && `Con` LIKE '%{$Cvalue}%'
   && `front` LIKE '%{$Fvalue}%' && `Listprice` BETWEEN '{$minprice}' AND '{$maxprice}' 
   && `Year_Built` >= '{$Year}' && `Status` LIKE '%Active%' GROUP BY `MLS` 
  ORDER BY `Neigh`, `price`, `tmk` ASC";

  $result=mysql_query($query) or die('Query failed: ' . mysql_error() . "<br />\n $query"); ;

  $num=mysql_num_rows($result);

sorry if this is a mess .. im self taught from the internet .. it does work but returns duplicates for multiple variables in the same record …

  • 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-26T03:10:53+00:00Added an answer on May 26, 2026 at 3:10 am

    If you want to keep the current design with view stored in a single cell, then there is something you can do. Although I do not suggest it I give an example below because you already have designed your project like this.

    NOTICE: It is a testing example, to see the basic functionality. This is not final code cause sql injection and other things are not taken in consideration.

    I do the assumption that views are stored in a single cell, space delimited, and if a view consist of more that one word – are place in between, for example City-Center.

    Study this example and see if you can adjust it to your needs:

    <?PHP
    echo '<pre>';
    
    //mysql connect
    mysql_connect('localhost', 'root',''); 
    mysql_select_db("test"); 
    //add some tsting data
    addTestingData();
    
    //build sql from user input via $_GET   
    $sqlConditions = builtSql();//build sql conditions
    $sql = 'select * from `building` where '.$sqlConditions;//build final sql
    
    //get data from mysql
    $result  = mysql_query($sql )  ; 
    while($row=   mysql_fetch_row($result) ) 
        print_r( $row );
    
    ///////////////end//////////////////////////////////////////// 
    
    function addTestingData()
    {
    
    mysql_query("DROP TABLE IF EXISTS `Building`");
         mysql_query("
    CREATE TABLE `Building` (
      `building_uniqueid` MEDIUMINT UNSIGNED NOT NULL  , 
      `building_street` VARCHAR(30) NOT NULL,
      `building_street_nr` VARCHAR(7) NOT NULL,  
      `building_neighborhood` VARCHAR(30) NOT NULL,  
      `building_view` VARCHAR(250) NOT NULL, 
      `building_condition` VARCHAR(150) NOT NULL,  
      `building_frontage` VARCHAR(30) NOT NULL,
      `building_listprice` float NOT NULL, 
      `building_year` smallint not null,  
      `bsnss_comments` VARCHAR(255), 
      PRIMARY KEY  (`building_uniqueid`)
    ) ENGINE=InnoDB DEFAULT CHARSET=utf8 ROW_FORMAT=COMPACT;
    "); 
    
         mysql_query('
    insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
    `building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
    `building_listprice`,`building_year`,`bsnss_comments`) values 
    ("1","street1","strnr1","neighb1","Mountain Ocean Lake Park City-Center",
    "good","frontage1","500.3","1990","good building")
    '); 
    
         mysql_query('
    insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
    `building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
    `building_listprice`,`building_year`,`bsnss_comments`) values 
    ("id2","street1","strnr1","neighb2","River Ocean Lake Park City-Center",
    "very good","frontage1","800.5","1991","good building")
    ') or die(mysql_error()); 
    
         mysql_query('
    insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
    `building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
    `building_listprice`,`building_year`,`bsnss_comments`) values 
    ("3","street3","strnr3","neighb1","Ocean Park City-Center",
    "fantastic","frontage77","600.7","1994","good building")
    '); 
    
         mysql_query('
    insert into `building` (`building_uniqueid`,`building_street`,`building_street_nr`,
    `building_neighborhood`,`building_view`,`building_condition`,`building_frontage`,
    `building_listprice`,`building_year`,`bsnss_comments`) values 
    ("4","street4","strnr4","neighb1","Ocean Park Mountain City-Center",
    "good","frontage1","500.23","1994","good")
    '); 
    
      $_GET['Neighborhood']=array('neighb1');
      $_GET['View']=Array('Mountain','River', 'City Center');
      $_GET['Condition']=array('good','very good');
      $_GET['Frontage']=array('frontage77','frontage1');
      $_GET['minPrice']='500';
      $_GET['maxPrice']='600';
      $_GET['minYear']='1990';
      $_GET['maxYear']='1995';
    
    
    
    }
    
    
    
    function builtSql()
    {
    
      $sqlBuild = '( ';
    
    //formate sql for Neighborhood
    foreach($_GET['Neighborhood'] as $value)
        $sqlBuild .=' `building_neighborhood` = \''.$value.'\' or ';   
        $sqlBuild=removeLastOr($sqlBuild);
        $sqlBuild.=') and (';
    
    //formate sql for View
    foreach($_GET['View'] as $value) 
        $sqlBuild .=' `building_view` LIKE \'%'.str_replace(" ", "-",$value).'%\' or ';   
        $sqlBuild=removeLastOr($sqlBuild);
        $sqlBuild.=') and (';
    
     //formate sql for Condition
    foreach($_GET['Condition'] as $value) 
        $sqlBuild .=' `building_condition` = \''.$value.'\' or ';   
        $sqlBuild=removeLastOr($sqlBuild);
        $sqlBuild.=') and (';
    
     //formate sql for Frontage
    foreach($_GET['Frontage'] as $value) 
        $sqlBuild .=' `building_frontage` = \''.$value.'\' or ';   
        $sqlBuild=removeLastOr($sqlBuild);
        $sqlBuild.=') and (';
    
     //formate sql for Price
    $sqlBuild.=
    ' `building_listprice` BETWEEN \''.$_GET['minPrice'].'\' and \''.$_GET['maxPrice'].'\'   ';
        $sqlBuild=removeLastOr($sqlBuild);
        $sqlBuild.=') and (';
    
    //formate sql for Year
    $sqlBuild.=
    ' `building_year` BETWEEN \''.$_GET['minYear'].'\' and \''.$_GET['maxYear'].'\' '; 
        $sqlBuild.=')  ';
    
    return $sqlBuild;
    }
    
    function removeLastOr($str)
    {
        $tmp=substr($str ,0,(strlen($str )-2));
        return $tmp=substr($str ,0,(strlen($str )-3)); 
    }
    
    ?>
    

    Although you see a some foreach loops, there is no need to worry cause they run for small arays that contain user data, so consider them runing super fast cause there is no mysql query involved!!

    If any problem remains consider giving details on db schema and some basic description. Hope this can help!

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

Sidebar

Related Questions

hi i am using codeigniter , i want to add a multi select box
I'm trying to set up a multi-select filter on a foreign key in the
Is there some way to set the Height attribute of a WPF multi-select ListBox
How do I generate page numbers for a multi page data set using javascript?
This is my c++ struct (Use Multi-Byte Character Set) typedef struct hookCONFIG { int
What is the max size of the multi line text field? Can I set
Is it possible to use gcov for coverage testing of multi-threaded applications? I've set
I'm trying to set up a many to many relationship using the has_many :through
I have a windows form with a listview control. I set the MultiSelect property
Requirement is to create a reusable multi-select combobox custom control. To accomplish this, I

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.