![]() |
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><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> |
This is the record count of all elements with class "rating"
Code:
var ratingElements = document.getElementsByClassName('rating');Code:
titlesum = 0; |
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! |
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. |
You know, you really answered your own question. (The one you send me privately.)
Quote:
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. |
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. |
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. |
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 ... |
| All times are GMT +1. The time now is 08:57 PM. |
Powered by vBulletin®
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.