0

I have a list of database results, each tagged with css properties for filtering purposes. Some could have many tags, some few.

    <ul class="results">
      <li class="filterThis property propertysome property other propertyetc>
        <!— item info —> 
      </li>
      <!— next item —>
    </ul>

This line counts the visible number of results that have one particular css property ('.property' in this case).

    var numProperty = $('.filterThis.property:visible').length

And this updates the number in the list of filters, which are loaded after the results are loaded

    $(".whenReady label span.property”).html(numProperty);

How do I extend this to loop through all available css properties, count them as above, and update the html as above?

Each() seems like the way to go, but I can't seem to get the structure or naming right to either produce results at all, or to carry the names all the way through so I can assign the html updates.

All the possible properties are available as php results, so that could be a json object to jQuery (or something else?), but I’m at a loss of how to take the next steps and get the desired functionality.

Any help would be greatly appreciated.

Cœur
  • 37,241
  • 25
  • 195
  • 267
Gary
  • 41
  • 6
  • Maybe try this: http://stackoverflow.com/questions/1227286/get-class-list-for-element-with-jquery – Sébastien Nov 12 '13 at 19:59
  • Thanks for the suggestion @Sébastien - maybe I just don't understand it. That seems to require iterating through each one by hand-coded name to do anything (count, update the html). I've updated my question to be more accurate. – Gary Nov 12 '13 at 20:09

1 Answers1

0

Couldn't you fix this in the backend? For example, keep track of the available filters:

<?php
  $results = .... ;
  $filters = array();

  foreach($result in $results){
    // assuming each result has an array of filters
    $filters = array_merge($filters, $result['filter']);
  }

  $filters = array_unique($filters);
  $csFilters = implode(',',$filters);
?>

With the example above, you have a comma seperated string with available filters which you can then apply as a data-attribute on your results list:

<ul class="results" data-filters="a,b,c,d">

Now you can access theses filters in jQuery:

$('.results').data('filters').split(',');
meavo
  • 1,042
  • 1
  • 7
  • 16
  • Nice @meavo, I hadn't thought of doing it that way. I'm familiar with 'as' but not 'in'? Changing it to 'as' lets the page render but creates and invalid argument warning for the foreach. What do I have wrong? – Gary Nov 12 '13 at 20:53
  • foreach ($result in $results) should work properly. What you could do to debug is add a print_r($filters) in your foreach loop. Check if the array is filled as expected. Maybe you can then find out, what goes wrong. – meavo Nov 12 '13 at 21:01
  • Check if your error reporting is on (http://php.net/manual/en/function.error-reporting.php). If not, you'll see a white screen instead of a logical error :) – meavo Nov 12 '13 at 22:08
  • I'm not sure if this -could- be overwritten by a php.ini setting of some sort. Simply test is by making an obvious mistake in your PHP; will you see error message then? – meavo Nov 12 '13 at 22:27
  • Yes @meavo, but for some it generates a white page. Switched to a debugging tool, where I should have been in the first place (thank you :). The error thrown is "Parse error: syntax error, unexpected T_STRING" I looked it up and tried to resolve, but no luck. Any thoughts? – Gary Nov 13 '13 at 01:32