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 claire - January 28, 2015

Thanks, I think I get what you're looking for.

I don't know of any code snippets offhand that will do this. I'll refer it to another dev, see if anyone else has the appropriate code already written. Otherwise, this would have to be a consulting job, unfortunately.

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

Claire Ryan
interactivetools.com

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

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);
?>