Use getListOptions to pull in list options ONLY if a field has been selected in the current records?

7 posts by 2 authors in: Forums > CMS Builder
Last Post: January 30, 2015   (RSS)

By dm - January 27, 2015

Hi everyone,

I have a multi-record section with a table called 'properties' which has four list options:

'property_type', 'town', 'bedrooms' & 'bathrooms'.

I would like to be able to use these four lists to auto-populate drop downs in a search form but only display the options that have been selected in the database records.

I can auto populate the search drop downs no problem but I am unable to narrow down the results to selected list options...

Is it possible to expand on the getListOptions() method and do this?

ie getListOptions('properties','property_type') with some extra code to only display the "selected" list options..

If not could maybe someone point me in the right direction to achieve this...

Many thanks in advance for any help with this!

By claire - January 27, 2015

Hi there

Can you give me a link to an example page for this please?

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By dm - January 28, 2015 - edited: January 28, 2015

hi claire

thanks for taking a look, much appreciated!

i have been unable to contact my friend to get permission to post a live link (but can send you in a mail if needed) so i have uploaded an example php page

its is a basic property listings site and i just wanted the search form drop downs in the attached example to only pull in the list options if there is a property listing in the database with this option selected...

ie for bedrooms there is currently a list field "bedrooms" with options 1,2,3,4,5,6,7,8,9,10 - i would like that if there are no properties with 10 bedrooms in the database, don't show this in the search form dropdown.. to save visitors searching for something that doesnt exist

<select name="bedrooms" id="bedrooms">
<option value = "" <?php selectedIf($value, @$_REQUEST['bedrooms']);?>>Bedrooms</option>
<?php foreach (getListOptions('properties', 'bedrooms') as $value => $label): ?>
<option value = "<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['bedrooms']);?>><?php echo $label; ?></option>
<?php endforeach ?>
</select>

thanks again for you time!

By dm - January 29, 2015

Hi Claire, 

Thanks very much for spending time on this...

I tried to code something else skipping the getlistoptions method and using the usual foreach record code to display lists below:

<select name="bedrooms" id="bedrooms" >

<option value="">Bedrooms</option>

<?php foreach ($propertiesbedroomsRecords as $record): ?> 
<?php if (@$alreadyShown[ $record['bedrooms'] ]++) { continue; } ?>
<option value="<?php echo $record['bedrooms'] ?>" ><?php echo $record['bedrooms'] ?></option> 
<?php endforeach; ?> 
</select>

But unfortunately with the numerical search drop downs such as number of bedrooms and bathrooms if a number has already been "selected" in the one field it then doesn't display it in the other field (i think because it sees the number 1 in bedrooms for example and then assumes 1 is @$aleadyShown for any other numerical option in the search form), so back to the drawing board... i guess i could add 1 bed, 2 bed... 1 bath, 2 bath etc to make the field values unique but it is a pretty ugly way of going about it

Fingers crossed one of the other devs has an idea re a simpler getlistoptions type method to achieve as I am sure this would be useful for anyone wanting a search form on their listings type site.

And from a visitor point of view it is much more user friendly than displaying search options that are are currently not in the database, saving them wasting time searching for things that don't exist on the site...

Thanks again and all the best!

By claire - January 29, 2015

I've asked, but I don't think we've got anything handy that will do this. I suspect the most efficient way of doing it involves adding joins to the getRecords function, which is going to be custom code.

That said - I think it's probably not going to take much to do it, once we get a look at your site. If you send an email to consulting@interactivetools.com, I'll ask Ross to talk to me about it first.

--------------------

Claire Ryan
interactivetools.com

Save time by getting our experts to help with your project.
http://www.interactivetools.com/consulting/

By dm - January 30, 2015 - edited: January 30, 2015

Hi Claire,

Not to worry, unfortunately consulting isn't an option for this site at the moment, I seemed to have found a way around the issue by using some mysql... will leave an example below in case anyone else is looking for something similar...

Thanks again for your time...

<?php
$con=mysqli_connect("localhost","my_user","my_password","my_db");

if (mysqli_connect_errno())
{echo "Failed to connect to MySQL: " . mysqli_connect_error();}

$result = mysqli_query($con,"SELECT * FROM cms_properties GROUP BY bedrooms ORDER BY bedrooms");

echo "<select name=\"bedrooms\" id=\"bedrooms\" >
<option value=\"\">Bedrooms</option>
";

while($row = mysqli_fetch_array($result))

if(@$_REQUEST['bedrooms'] == $row['bedrooms'])
{echo "<option selected=\"selected\" value=" . $row['bedrooms'] . " >" . $row['bedrooms'] . "&nbsp;Bedroom" ."</option>";}
else
{echo "<option value=" . $row['bedrooms'] . " >" . $row['bedrooms'] . "&nbsp;Bedroom" ."</option>";}
echo "<option value=\"\">All Bedrooms</option></select>";

mysqli_close($con);
?>