simple drop down search list

10 posts by 2 authors in: Forums > CMS Builder
Last Post: August 14, 2017   (RSS)

By willydoit - July 19, 2017

Hi all,

I have a table called "advertisers" and within that a field called "title" which contains each advertisers business name.

I want to create a search facility based on the "title" field but am unsure how to populate the search form. I am using code which was designed to show options from a pre-created drop down list within the table and thought I would just need to change the field name from that of the dropdown options list to that containing the business name ("title" ) but that doesnt work.

The drop down list will ultimately be used to show an advertisers location on a google map ( I already have this working when selecting from a services-provided field to show businesses providing certain services but this is drawing data from a precreated options list within the table if that makes sense.

The code I am trying to get to work is shown below and produces a source code error of  " Unknown optionsType"  and I am not sure whether I need to simply use different php syntax due to the field not being a list field or whether I need to create a "list field" containing the business names within the cmsb table using the related records function but then if I do I am unsure how to accomplish this.

Ideally I would like to have this search on the same page as the existing services search as the ultimate goal is to allow the user to search for businesses providing certain services ie Hotel and from that initial search to have it populate the google map with pins and at the same time populate the secondary search box with all the business names that meet that critera so that they can select any of the short listed business names to have that business location displayed on the redrawn map as a solitary pin.

Any help would be appreciated.

<form method="post" action="?">
        
      
      
       <select name="title">
      <h5>  <?php foreach (getListOptions('advertisers', 'title') as $value => $label): ?> 

<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['title']);?>>

<?php echo $label; ?></option> 

<?php endforeach ?> </p></h5>

      
         
         </select>
<br/>
        
        
        <div align="left"><h5>
          <input type="submit" name="search" value="  Search Listings  "/></h5>
        </div>
        
        
        
</form>

By Dave - July 28, 2017

Hi willydoit, 

Sorry for the delay in responding.  How about this?

  // show pulldown of option for table/field
  $tablename            = "advertisers";
  $fieldname            = "title";
  $showEmptyOptionFirst = true; 
  $selectedValue        = isset($_REQUEST[$fieldname]) ? $_REQUEST[$fieldname] : ''; // default value
  $valuesToLabels       = getListOptions($tablename, $fieldname);
  $optionsHTML          = getSelectOptions($selectedValue, array_keys($valuesToLabels), array_values($valuesToLabels), $showEmptyOptionFirst);
  print "<select name='$fieldname'>$optionsHTML</select>\n";

Let me know if that works for you!

Dave Edis - Senior Developer
interactivetools.com

By willydoit - August 1, 2017

Hi Dave,

sorry for the late response, been up to my eyes in it.

It may be better to explain what I want to do.

I have a page with a google map which displays the locations of all records in our advertisers table, on the page we also have a search box which allows the user to select particular types of business such as hotel, bakery etc ( this field is a multi selection list field within the table) Once a business type is selected the page reloads showing only the locations for businesses of the selected type.

Thats what we have at the moment and all that works fine.

What I want to do is have a second search form which contains the business names of the curently sorted content ie if the user has elected to show the locations of bakers in the town and we have information on two, the business name list will have just two entries. The idea being that by selecting one of the names in the business list the page will again reload showing only the pin depicting the location of that business.

I appreciate that in this example the exercise seems a bit pointless but if the user was looking for accommodation locations the list could be quite long so the ability to select one entry from a longer list would have obvious benefits.

I assumed the solution would consist of something like for each record in the array that has been created on the current page load, echo each business name in a drop down search box and upon selection reload the page with just that one record loaded.

That's how simplistic it is in my mind but the stumbling block I have is that the first search box relating to "BusinessType" is created using the contents of a predifined multi selection list field so all the options in the list are contained in a single field, wheras the business name list would have to be created by pulling the content from the business_name field from multiple records which I dont know how to do or even if it is possible.

Hopefully this explains my requirements a little better.

Thanks in advance.

By Dave - August 8, 2017

Hi willydoit, 

What you want to do is break it down into steps.  It's getting into a bit of more complicated programming, but basically, you work with the data you have.  So working in reverse: 

  • You want a list of business_name's from TableA 
  • And you only want business name's that match... a record number?  

If you had a list of record numbers you could load only records that matched those record numbers.  Something like this: 

  // load records match certain record numbers
  $recordNums      = [118, 167, 145];
  $recordNumsAsCSV = mysql_escapeCSV($recordNums);
  $records         = mysql_select("accounts", " num IN ($recordNumsAsCSV) ");
  $numToUsername   = array_column($records, 'username', 'num');
  showme($numToUsername);
  exit;

Hope that helps!

Dave Edis - Senior Developer
interactivetools.com

By willydoit - August 9, 2017

Hi Dave,

  I dont think thats what I am after.

here is a link to my page as I have it so far http://bridlington.www68-66-241-20.a2hosted.com/bridlington-advertiser-map.php

As you will see it displays markers for businesses on a google map. You will see that there is a drop down box to refine that search to a business type using the "services_provided" field values, the coding used for this search filter, which may or may not be relevant is

<form method="post" action="?">
        
      
      
       <select name="services_provided">
      <h5>  <?php foreach (getListOptions('advertisers', 'services_provided') as $value => $label): ?> 

<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['services_provided']);?>>

<?php echo $label; ?></option> 

<?php endforeach ?> </p></h5>

      
         
         </select>
<br/>
        
        
        <div align="left"><h5>
          <input type="submit" name="search" value="  Search Listings  "/></h5>
        </div>
        
        
        
</form>

Once a "services_provided" value is selected from the list the page reloads showing only pin locations for businesses offering that service. 

I assume this is doing this via causing the page to reload with a ?services_provided = value of selected item parameter which causes the page to go to the table "advertisers" and create an array consisting of all the records where the field "services_provided" has content which matches the submitted search value.

All of this works fine.

What I want to add is another search filter box next to the services search box which contains a drop down list of the business names (<?php echo htmlencode($record['title']) ?>)  of the businesses contained in the current array. I then want to be able to apply a search against any selected business name ("title") so that the page is reloaded containing the location pin of just that one business.

So where the page was initially loaded using the criteria  where "services_provided" = "search criteria" when the second search box is used it would  reload the page using where "title" = the value selected from the list.

In my head it seems quite straightforward but thats probably because I dont program php or mysql :-(

Hopefully the link and what I hope is a better description of my needs will help you to understand my requirements. The main thing is to know that I am not a programmer so instructions need to be basic to say the least.

Thanks again.

By Dave - August 9, 2017

Hi willydoit, 

Can you attach bridlington-advertiser-map.php to this post so we can see the code?

Thanks!

Dave Edis - Senior Developer
interactivetools.com

By Dave - August 10, 2017

Hi willydoit, 

We're getting into some custom programming here.  But give this a try: 

  // get pulldown of matched records
  $records              = $advertisersRecords;
  $valueField           = 'num';
  $labelField           = 'title';
  $showEmptyOptionFirst = true;
  $selectedValue        = isset($_REQUEST[$valueField]) ? $_REQUEST[$valueField] : ''; // default value
  $valuesToLabels       = array_column($records, $labelField, $valueField);
  $optionsHTML          = getSelectOptions($selectedValue, array_keys($valuesToLabels), array_values($valuesToLabels), $showEmptyOptionFirst);
  print "<select name='$valueField'>$optionsHTML</select>\n";

Let me know if that works for you.  I can't test it on your server so you may need to fix any typos but it should give you a sense.

Dave Edis - Senior Developer
interactivetools.com

By willydoit - August 11, 2017

Thanks dave,

excuse my ignorance but as mentioned I am not a php or mysql programmer. I dont know whether the code supplied is for the header or to place inside a form section. Do i place all of this code into the pre header section ie in the code below.

<?php 
  // load viewer library
$libraryPath = 'cmsb/lib/viewer_functions.php';
   $dirsToCheck = array('/home/sites/lpl-testsite.co.uk/public_html/','','../','../../','../../../');
  foreach ($dirsToCheck as $dir) { if (@include_once("$dir$libraryPath")) { break; }}
  if (!function_exists('getRecords')) { die("Couldn't load viewer library, check filepath in sourcecode."); }
  
    // get pulldown of matched records
  $records              = $advertisersRecords;
  $valueField           = 'num';
  $labelField           = 'title';
  $showEmptyOptionFirst = true;
  $selectedValue        = isset($_REQUEST[$valueField]) ? $_REQUEST[$valueField] : ''; // default value
  $valuesToLabels       = array_column($records, $labelField, $valueField);
  $optionsHTML          = getSelectOptions($selectedValue, array_keys($valuesToLabels), array_values($valuesToLabels), $showEmptyOptionFirst);
  print "<select name='$valueField'>$optionsHTML</select>\n";

  // error checking
  if (!@$GLOBALS['GEOCODER_PLUGIN']) { die("You must activate the Geocoder plugin before you can access this page."); }
if (!@$GLOBALS['GEOCODER_GOOGLE_API_KEY']) { die("You must have a Google Maps API key to display a map, see the readme file for instructions on getting one."); }
  // get records
   list($advertisersRecords, $advertisersMetaData) = getRecords(array(
   'tableName'   => 'advertisers',
    'loadUploads' => false,
    'allowSearch' => true,


  ));

?>

If so can I then just replicate the search form I have used for the services provided but replace the services_provided field with the "title" field as below?

 <form method="post" action="?">
        
      
      
       <select name="title">
      <h5>  <?php foreach (getListOptions('advertisers', 'title') as $value => $label): ?> 

<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['title']);?>>

<?php echo $label; ?></option> 

<?php endforeach ?> </p></h5>

      
         
         </select>
<br/>
        
        
        <div align="left"><h5>
          <input type="submit" name="search" value="  Show Business Location  "/></h5>

Thanks in advance

By Dave - August 14, 2017

Hi willydoit, 

I'd experiment, but you could try putting it where you want to want it to display.  Just surround it with <?php ?> tags.

Dave Edis - Senior Developer
interactivetools.com