You are here:  » User Reviews - add extra fields


User Reviews - add extra fields

Submitted by npaitken on Mon, 2008-09-15 11:31 in

Hi David,

I'd like to add a couple of extra fields to Reviews. The user fields I'd like to add are:

1. Review Title
2. Review Date
3. Reviewer Name
4. Reviewer Location

There was another thread about this but it go into details.

Any help most appreciated,
Thanks,
Neil

Submitted by support on Tue, 2008-09-16 07:47

Hi Neil,

There's quite a lot involved in adding fields - the form, handling code, database modifications and display - a few users have asked for additional review fields so it's something that I'm going to look at - and currently considering a mechanism that doesn't require so much modification.

Are you comfortable with making changes to the review form - for example adding new fields with a name like "review_date" etc.?

Cheers,
David.

Submitted by npaitken on Tue, 2008-09-16 09:56

Hi David,

I'm really keen to add this to my site.

If you're expecting to have a modifiacation fairly soon then I'd just wait. Otherwise, I'd be willing to give it bash - although that said, I'm not an expert with forms, code or databases, so it might be a bit tedious for you!!

How about working through it and seeing how it goes. If it gets really tedious then we'll call it a day and wait for your modification?

Best regards,
Neil

Submitted by support on Tue, 2008-09-16 10:02

Hi Neil,

Bear with me - i'll check out the "alternative" method of implementing this and get back to you...

In the mean time, do you have many reviews that you want to preserve; or would it be OK to start from scratch with no reviews once code for the new fields has been added?

Cheers,
David.

Submitted by npaitken on Tue, 2008-09-16 12:20

Hi David,

It's a new site and I only have a handful of reviews which I can easily transfer manually.

Cheers,
Neil

Submitted by support on Tue, 2008-09-16 12:46

Hi Neil,

Ok, it's worked out reasonably straight forward without any modification of the database - but there will of course be work to do to on the HTML layout in order to make it look visually appealing.

The basic idea, is that rather than the comments field in the review database containing just the comments themselves; what it can do is store a PHP serialized() value of an array containing as many additional comment fields as you wish.

Firstly, in reviews.php, look for the following code on line 10:

    if (!isset($_POST["comments"])) $_POST["comments"] = "";

...and REPLACE this with the following block of code:

    $comments = array();
    foreach($_POST as $k => $v)
    {
      if (substr($k,0,9)=="comments_")
      {
        $k = substr($k,9);
        $comments[$k] = stripslashes($v);
      }
    }
    $_POST["comments"] = serialize($comments);

...find the following code (line 14 prior to the above modification)

      $sql = "INSERT INTO `".$config_databaseTablePrefix."reviews` SET created='".time()."',approved='0',product_name='".database_safe($q)."',rating='".database_safe($_POST["rating"])."',comments='".database_safe(stripslashes(trim($_POST["comments"])))."'";

...and REPLACE with:

      $sql = "INSERT INTO `".$config_databaseTablePrefix."reviews` SET created='".time()."',approved='0',product_name='".database_safe($q)."',rating='".database_safe($_POST["rating"])."',comments='".database_safe($_POST["comments"])."'";

Now, the only other changes required are within html/ratings.php. Here, you need to decide what you want to call each of your new fields; and then add code to the first section of the script to display those field names; and code to the second section of the script to generate the form where those fields can be entered as part of a new review. The field names chosen must match up exactly between the two sections.

Taking 2 new fields as an example: "name" and "location";

- Up to 14/06A, look for the following existing code on line 15 of html/ratings.php:

        <?php print htmlentities($review["comments"],ENT_QUOTES,$config_charset); ?>

...and REPLACE this with the following block of code:

        <?php
        $comments = unserialize($review["comments"]);
        print htmlentities($comments["comments"],ENT_QUOTES,$config_charset);
        print "<br />Name: ".$comments["name"];
        print "<br />Location: ".$comments["location"];
        ?>

For 15/01A look for the following code at line 13:

        <?php print htmlspecialchars($review["comments"],ENT_QUOTES,$config_charset); ?>

...and REPLACE with:

        <?php
        $comments = unserialize($review["comments"]);
        print htmlspecialchars($comments["comments"],ENT_QUOTES,$config_charset);
        print "<br />Name: ".$comments["name"];
        print "<br />Location: ".$comments["location"];
        ?>

The critical parts are the names of the fields within the $review array - in this case $review["comments"], $review["name"] and $review["location"]. These must match exactly the second part of the form field names in this next part of the modification - where each field name begins with "comments_". So, with the same 2 example additional fields (name and location);

Up to 14/06A look for the following existing code in the second part of

html/ratings.php:

        <textarea name='comments' rows='4'></textarea>

...and REPLACE this with the following block of code:

        <textarea name='comments_comments' rows='4'></textarea>
        <br />
        Name: <input type='text' name='comments_name' /><br />
        Location: <input type='text' name='comments_location' /><br />

And for 15/01A look for the following code at line 45:

        <textarea name='comments' placeholder='<?php print translate("Your Comments (optional)"); ?>'></textarea>

...and REPLACE with:

        <textarea name='comments' placeholder='<?php print translate("Your Comments (optional)"); ?>'></textarea>
        <label>Name<input type='text' name='comments_name' /></label>
        <label>Location<input type='text' name='comments_location' /></label>

The only caveat with this quick'n'easy implementation is that the review moderation form will contain the serialized() version of the review; however the separate fields can be easily identified and edited if required so this shouldn't be too much of a problem.

Hope this helps!

Cheers,
David.

Submitted by npaitken on Fri, 2008-09-19 13:33

Hi David,

Your method works really well and only takes 5 minutes to set-up. I've decided that I'll add three extra fields - Name, Location & Handicap.

Just a couple of questions regarding presentation.

The form entry looks like this:

Comment: this is comment text blah blah blah
Name: Bob
Location: Liverpool
Handicap: 15

how would I go about styling the comments to display like this:

this is comment text blah blah blah
Bob from Liverpool (15 Handicap)

is it done by adding html to this block of code:

        <?php
$comments = unserialize($review["comments"]);
        print htmlentities($comments["comments"],ENT_QUOTES,$config_charset);
        print "<br />Name: ".$comments["name"];
        print "<br />Location: ".$comments["location"];
        print "<br />Handicap: ".$comments["handicap"];
        ?>

Many thanks,
Neil

Submitted by support on Fri, 2008-09-19 14:17

Hi Neil,

Try something like this;

        <?php
        $comments = unserialize($review["comments"]);
        print "<p>".htmlentities($comments["comments"],ENT_QUOTES,$config_charset)."</p>";
        print "<p><strong>".$comments["name"]."</strong> from ".$comments["location"]." (".$comments["handicap"]." Handicap)</p>";
        ?>

Cheers,
David.

Submitted by bloach on Sat, 2008-11-01 01:10

Hi David,

Couple of things:

1) Is there anyway to display the data in following way:

November 30th, 2007
Pros: looks good. mp3 is a great feature
Cons: none
Overall: i have been using this head unit for over a year and i must say its great. easy to use and sounds amazing
full star full star full star full star full star

Date field should be added automatically while others to be collected from the user. Also, how to make them required fields.

_____________________________________________________
My Australian Shopping Comparison Site

Australian Shopping Comparison

Submitted by support on Mon, 2008-11-03 10:39

Hi,

Changing the layout is just a case modifying this section;

        <?php
        $comments = unserialize($review["comments"]);
        print htmlentities($comments["comments"],ENT_QUOTES,$config_charset);
        print "<br />Name: ".$comments["name"];
        print "<br />Location: ".$comments["location"];
        ?>

...using the appropriate HTML to print the layout you want, so based on your example (if the code was modified elsewhere to have the fields required), you might use something like:

        <?php
        $comments = unserialize($review["comments"]);
        print $comments["date"];
        print "<br />Pros: ".$comments["pros"];
        print "<br />Cons: ".$comments["cons"];
        print "<br />Overall: ".htmlentities($comments["comments"],ENT_QUOTES,$config_charset);
        ?>

The only element of this that is not part of the modifications described above is the date field. To add this, look for the following line in the above modification (towards the top of reviews.php):

    $_POST["comments"] = serialize($comments);

...and REPLACE this with:

    $comments["date"] = date("F jS, Y");
    $_POST["comments"] = serialize($comments);

This will give a date format as in your example output - e.g. "November 30th, 2007". To change the date format, see the format specification at:

http://uk.php.net/manual/en/function.date.php

Cheers,
David.

Submitted by npaitken on Wed, 2008-11-05 11:58

Hi David,

Just got around to implementing this. However, it's driving me mad. I've added the code to both reviews.php and HTML/ratings.php but when I add new comments they seem to be showing intermittently. Perhaps there's an error in my code.

Could you please run you're eye over it.

reviews.php down to end of inserted code

<?php
  require("includes/common.php");
  $q = (isset($_GET["q"])?tapestry_normalise($_GET["q"],":\."):"");
  $rewrite = isset($_GET["rewrite"]);
  if (isset($_POST["submit"]))
  {
        $comments = array();
    foreach($_POST as $k => $v)
    {
      if (substr($k,0,9)=="comments_")
      {
        $k = substr($k,9);
        $comments[$k] = $v;
      }
    }
    $comments["date"] = date("F jS, Y");
    $_POST["comments"] = serialize($comments);

HTML/ratings.php
first bit

        <?php
        $comments = unserialize($review["comments"]);
        print htmlentities($comments["comments"],ENT_QUOTES,$config_charset)."<br/>";
print "<br />".$comments["name"]." (".$comments["handicap"]." Handicap)";
        print "<br />".$comments["location"]."";
print "<br /><small>".$comments["date"]."</small>";
        ?>

second bit

        <textarea name='comments_comments' rows='4'></textarea>
        <br />
        Name: <input type='text' name='comments_name' /><br />
Handicap: <input type='text' name='comments_handicap' /><br />
        Location: <input type='text' name='comments_location' /><br />

Also, I cleared out the reviews table in the PT database prior to starting the mods (only had a couple of reviews so planned to add them again manually). Hopefully, I've not upset anything in the database?

Frustrated,
Neil

Submitted by support on Wed, 2008-11-05 12:43

Hi Neil,

When you say "showing intermittently", do you mean it either works or it doesn't (enter reviews + other fields, displays fine, and continues to display fine every time you view the page) - or do you mean that you enter a review + other fields and then it sometimes displays and sometimes doesn't when you view the product page?

Cheers,
David.

Submitted by npaitken on Wed, 2008-11-05 13:23

Hi David,

The latter. It entered a couple of reviews which initially appeared ok. Then, once I'd completed everything and checked back the reviews weren't displaying properly.

See examples:

All six reviews don't appear properly
{link saved}

For this one, one of the reviews appears ok, but not the other two
{link saved}

Please delete above URLs and this text once you've reviewed

Any thoughts,
Neil

Submitted by support on Wed, 2008-11-05 13:27

Hi Neil,

Sorry - it's tricky to explain the very similar cases but I think it's the first case - the two pages display the same information every time - which indicates that the problem is with the creation side of things rather than the display side of things.

Could you perhaps email me (as attachments) your modified reviews.php and html/ratings.php and i'll check them out for you - it's easier to debug when looking at the actual code in situ...

Cheers,
David.

Submitted by npaitken on Wed, 2008-11-05 14:00

Thanks David,

Email on its way

Neil

Submitted by HJW on Wed, 2009-02-04 16:47

Hello again David,

Sorry to pester you, I have made the above mods to my site, but now the reviews aren't working properly.
I think its a similar/same problem as the one described above, I have noticed however that if it's a short review, maybe 6 or 7 words it seems to work fine, for a longer review all that is displayed are the stars and the comments/name/location boxes are blank. I have emailed you my reviews.php and html/ratings.php files.
Any help appreciated.

Thanks

Hayden

Submitted by support on Wed, 2009-02-04 17:04

Hi Hayden,

Thanks for the files, i'll follow up by email...

Cheers,
David.

Submitted by daem0n on Thu, 2009-02-05 07:56

Hi David,

Can you post your fix to this problem publicly (if it was something that needed further modification to the code that you posted)?

I'll be looking at adding some extra fields to the reviews also so this thread will definitely come in handy.

Thanks a lot, -Joe

Submitted by support on Thu, 2009-02-05 10:45

Hi Joe,

I fixed the code above (stripslashes was in the wrong place) so the instructions
will should work fine now.

Cheers,
David.

Submitted by coyote on Mon, 2009-02-16 16:55

Hello David

(i saw your mail thank you i am preparing the file ton send you)

Another question in relation with this topic :

You changed my products.php a while ago in order to have reviews on the same page as product

it worked perfect, then i saw this topic and wanted to add name & subject on reviews

i followed the changes you indicate on this topic : but i made the changes on products.php instead of reviews.php of course, then on ratings.php

then i experienced a problem : comment message doesnt appear, only name & subject appear

do you see where the problem could come from ?

Cheers

Coyote

Submitted by support on Mon, 2009-02-16 16:58

Hi Coyote,

If name and subject are appearing that's the main thing! If you could email
me your modified files i'll take a look for you!

Cheers,
David.

Submitted by coyote on Mon, 2009-02-16 17:06

Thank you for your very quick answer ;)

i sent you the files products & ratings

Coyote

Submitted by FirstByte on Thu, 2009-07-23 23:01

Hi Guys,

I just added the follow field to capture reviewers ip address.

 <input type='hidden' name='comments_ip' value="<?php echo $_SERVER['REMOTE_ADDR']?>" />

Submitted by FirstByte on Thu, 2009-07-23 23:12

Hi David,

I'm modified the /admin/moderate.php file so that It displays the review along side the textarea to get an idea of what the review is about.

That's working fine, I've also managed to copy the MYSQL query and managed to add an Edit button to edit the content of the reviews.
I only edit the content between these ";s:4;".

 a:8:{s:4:"ease";s:1:"4";s:6:"design";s:1:"2";s:7:"quality";s:1:"3";s:4:"name";s:4:"test";s:2:"ip";s:13:"122.108.46.46";s:9:"purchased";s:9:"test shop";s:8:"comments";s:359:"<p><span style="font-family: tahoma; font-size: 12px; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px;">BlackBerry Pearl 8110 in stock now and shipping from MobiCity.<strong><span style="text-decoration: underline;"> Comes with 12 months warranty and support. Visit www.mobicity.com.au for more details.</span></strong></span></p>";s:4:"date";s:15:"July 23rd, 2009";}

I'm able to change the content and save it. I've been able to verify that content has been saved via MyphpAdmin.

The problem is that once it's saved the content not longer works. I figured it had to do something with the "serialize" but not to sure how it works.

below is my attempt at the edit function.

    if ($_POST["submit"] == "Accept")
    {
      $sql = "UPDATE `".$config_databaseTablePrefix."reviews` SET comments='".database_safe(widget_posted($_POST["comments"]))."',approved='".time()."' WHERE id='".$_POST["id"]."'";
    }
    elseif ($_POST["submit"] == "Edit Review")
    {
    $_POST["comments"] = serialize($comments);
    $sql = "UPDATE `".$config_databaseTablePrefix."reviews` SET comments='".database_safe(widget_posted($_POST["comments"]))."' WHERE id='".$_POST["id"]."'";
    }
    else
    {
      $sql = "DELETE FROM `".$config_databaseTablePrefix."reviews` WHERE id='".$_POST["id"]."'";
    }

Submitted by support on Fri, 2009-07-24 07:35

Hi Rod,

You're correct - it's because the format of the serialized data must be preserved - the numbers for example relate to the length of the next string section, or number of items in an array.

What you would need to do is rather than present the review in a regular edit box, would be to unserialize() it, create a text field / text box for each field, and then recombine the fields and serialize() again in the form handling code.

If you're not sure how to go about this email me your modified files and I'll take a look for you...

Cheers,
David.

Submitted by sirmanu on Tue, 2016-10-11 11:32

Hi David

It would involve too much modifications in order to have something like that in last distribution?

{img saved}

Submitted by support on Tue, 2016-10-11 11:50

Hello sirmanu,

I have always wanted to avoid any kind of user context within Price Tapestry, especially where email addresses etc. are involved as it very quickly becomes a very complex area!

However, as social media becomes more established, something I am considering for the future is the use of "social sign-on" where a user could associate their review with, for example, a twitter or Facebook account so I'll keep this in mind...

Cheers,
David.
--
PriceTapestry.com

Submitted by bat on Mon, 2017-02-13 00:22

Hi David,
I was using this for adding name and location into my review form in my previous versions but I'm using 15/09A and it doesn't appear to work as it should.
I've got the form bit but the name and location doesn't appear in the backend when acepting/rejecting the review and neither does it appear on the front page after being approved.

Is different code needed?

Many thanks

Submitted by support on Mon, 2017-02-13 10:23

Hi,

Please could you email me the modified html/ratings.php and admin/moderate.php and I'll check it out further for you...

Cheers,
David.
--
PriceTapestry.com