Drop-down box record count / refreshing

8 posts by 3 authors in: Forums > CMS Builder
Last Post: September 29, 2011   (RSS)

By MickC - September 15, 2011

I am using multiple drop down boxes to search machines by location, type, or manufacturer.
They auto-populate options from the table, I even employed the mm_jumpmenu feature to prevent user from having to click a button.

I have the problem where if you choose say "location 1", the page refreshes with the correct results, but if you then want to specify another field, eg "Manufacturer 1", the page will show only the results for that manufacturer, and the Location dropdown reverts back to "all locations"
Is there a way to make this work?

2nd part of this question is:
I have the options coming out of the table correctly, but i have seen where drop downs have "Make (3)" with the 3 being the number of listings in that category.
Is this just come coding to get the information to display?

Here is the current code for one of the drop downs:

<select name = "branch" onchange="MM_jumpMenu('self',this,1)">
<option value="?">All Locations</option>
<?php foreach (getListOptions('listings', 'branch') as $value => $label): ?>

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

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

</select>

Re: [MickC] Drop-down box record count / refreshing

By Jason - September 15, 2011

Hi Mike,

You have the right idea using the selectedIf() function, however, I think the issue you're running into is the values you are giving the <option> tags.

For example, say you're outputting a branch that has a value of 1. The value you're giving the option tag would be "?branch=1". So if you select this branch from the list the selectedIf() function would be comparing the value in the foreach loop (1) against the value in the $_REQUEST array ("?branch=1"). So you'll need to find a way to make these mesh up. The easiest way would be to change the value in your selectedIf statement like this:

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

For your second question, you'll need to use a separate query to the database to get a count of all the listings from a given category and then output that as part of your label.

If you're using a newer version of CMS Builder (2.08 or higher) you can use the mysql_count() function.

In this example, assume we're outputting a list of categories using the num as the value and the name as the label. Then in a section called listings, we have a field called category than also uses the category num as a value, we could output a list like this:

<select name = "category">
<option value = "">Select a Category</option>

<?php foreach ($categoryRecords as $category): ?>
<?php $listingsCount = mysql_count("listings", "category = '".intval($category['num'])."'"); ?>

<option value = "<?php echo $category['num'];?>" <?php selectedIf($category['num'], @$_REQUEST['category']);?>
<?php echo $category['name']."(".$listingsCount.")"; ?>
</option>

<?php endforeach ?>
</select>


Hope this helps get you started
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Jason] Drop-down box record count / refreshing

By MickC - September 15, 2011

Thanks Jason,

I gave it a quick try, but just got 0's after each option.
I tried this code last night and it gave every option a 1 after it.
<select style="background-color:#615921; font:Verdana, Geneva, sans-serif; size:10pt; color:#FFF" name = "manufacturer" onchange="MM_jumpMenu('self',this,1)">
<option value="?">All Manufacturers</option>
<?php foreach (getListOptions('listings', 'manufacturer') as $value => $label): ?>

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

<?php echo $label; ?> - <?php echo count($record);?></option>


<?php endforeach ?>

</select>

Anway, i'll have a play with this tonight
Thanks mate.

Re: [MickC] Drop-down box record count / refreshing

By Jason - September 16, 2011

Hi,

The count() function is a PHP function that counts the number of elements in an array. In this case, we are re-using the same variable ($record) for each iteration of the loop. $record will only contain 1 element.

What we need to do is do a query during each iteration of the loop. Try this:

<select style="background-color:#615921; font:Verdana, Geneva, sans-serif; size:10pt; color:#FFF" name = "manufacturer" onchange="MM_jumpMenu('self',this,1)">
<option value="?">All Manufacturers</option>
<?php foreach (getListOptions('listings', 'manufacturer') as $value => $label): ?>

<?php $lisitingCount = mysql_count('listings', "manufacturer = '$value'"); ?>

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

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


<?php endforeach ?>

</select>


Hope this helps
---------------------------------------------------
Jason Sauchuk - Project Manager
interactivetools.com

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

Re: [Jason] Drop-down box record count / refreshing

By MickC - September 16, 2011

Thanks Jason.

That worked perfectly. My attempt had me about 2 characters off....

Now I am going to try and employ an "if" statement so that any options with a listing count of "0" will not be shown

I haven't had a proper go at this yet, but this is the code i have so far - but Dreamweaver doesnt like it.
<select name = "type">
<option value="?">All Equipment Types</option>
<?php foreach (getListOptions('listings', 'type') as $value => $label): ?>
<?php $lisitingCount = mysql_count('listings', "type = '$value'"); ?>
<option value = "?type=<?php echo $value;?>" <?php selectedIf($value, @$_REQUEST['type']);?>>
<?php if $lisitingCount =0; ?>
<?php echo $label; ?> - (<?php echo $lisitingCount;?>)
<?php else; ?>
<?php echo $label; ?>
<?php endif; ?></option>
<?php endforeach ?>

</select>


I will have a play and see what I can do

Re: [MickC] Drop-down box record count / refreshing

By MickC - September 28, 2011

Okay, so I have all my search menus working well, getting list options as above.

We decided to do away with the count because it looked messy in the list.

What I want to do now, is to only put an entry in the list if there are records for it.

Our "Manufacturers" list is quite long, and a lot of the time, most of them wont have any matching records, so I only want the list to populate with options if they have records matching them.

Im sure i can do it with the same mySql count function, where anything with a count of less than 1 would be omitted, possibly with an "If" for each option?

Re: [MickC] Drop-down box record count / refreshing

By ross - September 29, 2011

Hi MickC

Glad to see this all coming together :). For having options appear only if they have records, something like this should work:

<?php if (mysql_count('listings', "manufacturer = '$value'")): ?>
YOUR CODE HERE
<?php endif>

That basically runs the same count function like you thought and then only displays your code if the count finds something.

Give that a whurl and let me know :).

Thanks!
-----------------------------------------------------------
Cheers,
Ross Fairbairn - Consulting
consulting@interactivetools.com

Hire me! Save time by getting our experts to help with your project.
Template changes, advanced features, full integration, whatever you
need. Whether you need one hour or fifty, get it done fast with
Priority Consulting: http://www.interactivetools.com/consulting/