Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2009
    Posts
    73
    Thanks
    5
    Thanked 9 Times in 9 Posts

    Question Field Count - Then Average totals HELP!

    Hello everyone,

    I am not a javascript programmer and if I need it usually I try to find examples on the web to build off of. But in this case I'm both pressed for time and can't find a good example. So here is what i have:

    We use a PHP driven form to write HTML to file that is pre-formatted with certain "rich snippet - schema" code. Specifically it is the hreview schema. So below as a portion of the output that we need javascript to work with:

    Code:
    <blockquote class='description'><p><abbr class='rating hide' title='5'> 5 out of 5 Stars: </abbr>So easy to deal with! It's quite refreshing to have someone return your call so quickly. Mike was very professional! Highly recommend!</p></blockquote>
    Every review that is written by the php script will have <abbr class='rating hide' title='5'> <------- this code generated, the only thing that will change is the TITLE attribute which will always be a # between 1 and 5 (This represents the star rating from the reviewer).

    So what I need is a script that finds each instance of the code containing the CSS CLASS of 'rating' , looks at the # in the TITLE attribute, adds the total all together and then divides the SUM by the # of records found (basic averaging) - lastly the result of that math is assigned to a variable.

    I have (I think) part of the idea figured out on how to acquire the record count of each occurrence of the CLASS 'rating' as shown below - but I can't figure out how to get the # value in the TITLE attribute and perform math on it.

    Code:
    <script>
    var list = document.getElementsByClassName("hreview");
    for (var i = 1; i <= list.length; i++) {
        document.getElementById("count").innerHTML = i;
    }
    </script>
    Guys/Girls any help would be greatly appreciated

  • #2
    Senior Coder
    Join Date
    Dec 2010
    Posts
    2,398
    Thanks
    12
    Thanked 570 Times in 563 Posts
    This is the record count of all elements with class "rating"
    Code:
    var ratingElements = document.getElementsByClassName('rating');
    var recordCount = ratingElements.length;
    This is the total of all the TITLE attributes for those elements, and the average
    Code:
    titlesum = 0;
    for(i=0; i<recordCount; i++) {
       var titleNumber = Number(ratingElements[i].title);
       titlesum += titleNumber;
    }
    var average = titlesum / recordCount;

  • #3
    New Coder
    Join Date
    Sep 2009
    Posts
    73
    Thanks
    5
    Thanked 9 Times in 9 Posts

    Exclamation Not sure where to ask for help

    1'st) Devnull69 thank you - with only a minor change that code did exactly as asked.

    2'nd) The end result has fixed a problem but created an unexpected new one, unfortunately I do not know exactly if it is a script problem or smart snippet microdata problem - so I'm going to try and expand on the explanation of what i'm doing and maybe someone can help or direct me to the best source.


    -- I have a page which will contain a series of reviews for a business, each review will be coded with microdata formats - because this page will contain multiple reviews on it we will also code a DIV or section that contains the microdata format for "Review-aggregate"

    -- The reviews are published by filling out a form which on submit pre-formats the results with HTML,CSS and Microdata format as needed and outputs the results to a file which is then brought into a HTML page using a PHP include statement when the relevant HTML page is served to the browser.

    -- THE PROBLEM: All of the individual reviews and the data are parsed properly as microdata format by google when we test the end results EXCEPT for the 2 values that the script creates. - This only occurs when google tries to parse the data, if you look at the page in a browser then the 2 values create by the script calculate and display properly.

    -- MY THOUGHTS: So, here is what I think is happening and if the hypothesis is correct I need to know if this can be corrected through script or any other suggestion:

    1.) The script HAS to be placed/run at the end of the HTML file AFTER the reviews, otherwise if placed in the header or beginning of the body the script does not see the reviews so it calculates 0 for both values.

    2.) Google on the other hand parses the page in order from the beginning of the code to the end, thus when googles parser looks for the VALUES in the two fields it see's NOTHING because the script has not run yet to produce the values.

    I am sorry if this is ultimately not a script issue but I do not know where it really falls and this is the best way to describe the problem being created. Thanks in advance!

  • #4
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Ummm...no. It's easier than that.

    Google will *NOT* RUN your JavaScript code.

    So take your existing page. Pull out *ALL* the JavaScript. And *THAT* is what Google sees.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    You know, you really answered your own question. (The one you send me privately.)
    -- The reviews are published by filling out a form which on submit pre-formats the results with HTML,CSS and Microdata format as needed and outputs the results to a file which is then brought into a HTML page using a PHP include statement when the relevant HTML page is served to the browser.
    And therein lies your mistake.

    Instead of outputting the reviews to a text file, they should be stored in a database.

    And part of what would be stored, besides the preformatted HTML of the review, would be any fields needed to easily calculate the needed totals/values via a SQL query.

    Then your PHP page executes the appropriate SQL query (or possibly queries, though I wouldn't expect you need more than one), gets all the data, plunks it into the HTML page, along with the now-calculated-without-JavaScript values that you want Google to see.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    I might mention that as a side benefit of storing the reviews in a database, you get the possibility of adding value to your site by allowing searches of various kinds.

    Besides the text of the review, and the rating, you could store the date/time it was created, the author, the IP address of the author (and the location, if you have an ip to location lookup service), etc., etc. And then you could offer searches on any of that data in addition to keyword searches within the reviews.

    Text files are a simple solution to some problems, but they are so terribly terribly limited.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #7
    New Coder
    Join Date
    Sep 2009
    Posts
    73
    Thanks
    5
    Thanked 9 Times in 9 Posts
    Pendant,

    If a database option was available i agree, but in this case we are working with a client who does not wish to have much out of pocket expense. Truthfully it is being done this way out of a combination of self curiosity as well as this clients financial limitations.

    So even though what you propose makes sense and I am in agreement, it is not really an option.

  • #8
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    27,191
    Thanks
    80
    Thanked 4,562 Times in 4,526 Posts
    Okay, then...

    It's a pretty horrible hack, but...

    Still do it in PHP, but do it by parsing the data out of the text file!

    Use PHP to read the file into a string. Since you say the totals appear after the reviews, then just send the string out to the browser.

    Then parse the string, looking for all occurrences of <abbr class='rating hide' title='???'>

    It shouldn't be too hard to do. You could split the string on "<abbr class='rating hide' title='"

    I don't use PHP, but something like:
    Code:
    $txt = ... the full text of all reviews, from the file ...
    $ratings = explode( "<abbr class='rating hide' title='", $txt );
    $total = 0;
    // start at 1 because element 0 will come before the first split point
    for ( $r = 1; $r < count($ratings); ++$r )
    {
        // if the rating is always 0 through 9, never a 2 digit number, then:
        $rating = substr( $ratings[$r], 0, 1 );
        $total += $rating;
    }
    No? It's ugly, but it's quick and dirty and gets away from using JavaScript.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •