Image Rating Tutorial

Rating Hack Tutorial:

1. Obtain zip file copied from original funbox.ro archive (which is no longer there, which is why I zipped the files up and supplied them here.)
a. Read through the readme file included with the original archive. This is the original readme from the funbox.ro site.
2. Expand zip file to a temporary directory.
3. Edit the _config_rating.php file with your DB parameters to connect to your zenphoto DB
4. About line 38 in the _drawrating.php file, you'll want to edit the path to be the absolute path to your zenphoto installation.
5. Add these lines to your image.php file:

HEAD Section
<?php require('_drawrating.php'); ?>

<!-- Rating System -->
<script type="text/javascript" language="javascript" src="<?= $_zp_themeroot ?>/js/rating.js"></script>
<link rel="stylesheet" type="text/css" href="<?= $_zp_themeroot ?>/css/rating.css" />

BODY Section

<?php $id=$_zp_current_image->id; rating_bar($id,5); ?>

6. Copy the contents of the temporary directory (NOTE: with the exception of the index.php file) you created to your theme folder, and make sure the directories (images/css/js) stay intact. If you already have these folders, that's ok.
7. You will have to add some custom stuff to your DB:

CREATE TABLE 'ratings' (
'id' varchar(11) NOT NULL,
'total_votes' int(11) NOT NULL default '0',
'total_value' int(11) NOT NULL default '0',
'used_ips' longtext,
PRIMARY KEY ('id')
) TYPE=MyISAM AUTO_INCREMENT=3 ;

Note that I was able to successfully put this information in my Images table as well with no issues. Just make sure in the config file (_config_rating.php) that the table is pointing to the correct one.

After this, it should work properly. I will not be providing any official support for this "hack" as it isn't mine to begin with, but I'll be happy to answer questions on the forum to the best of my abilities (and as time permits.) Everything you need should be here to make it work.

Comments

  • Good work but it does not work for me.

    1. mySQl gave errors so I had to run this query instead of yours:
    CREATE TABLE `ratings` (
    `id` INT( 11 ) NOT NULL AUTO_INCREMENT PRIMARY KEY ,
    `total_value` INT( 11 ) NOT NULL DEFAULT '0',
    `total_votes` INT( 11 ) NOT NULL DEFAULT '0',
    `used_ips` LONGTEXT NOT NULL
    ) ENGINE = MYISAM ;

    2. Everything else was setup as you said except in image.php I had to specify the path of
    <?absolute path here/_drawrating.php'); ?> as the original README suggests.

    3. I can see the stars and clicking on them reloads, no errors occur but the vote does not register.
  • @jay-

    since it was a hack made by a zenphoto poster whose site was down for others, and I grabbed it, i figured i'd post it. but i can't guarantee it works in everyone's implementation, since I haven't tested it at all on anyone's install.

    everyone else is welcome to post here and help us work out the bugs, as I'm sure there are some.

    You may try initializing the SQL table with some data first, before voting. It does mention something about that in the readme file.
  • No problem, but I will post anyway if someone has an idea. I made some changes but no luck so far. I ran a new SQL query

    'CREATE TABLE `ratings` (
    `id` VARCHAR( 11 ) NOT NULL ,
    `total_value` INT( 11 ) NOT NULL DEFAULT '0',
    `total_votes` INT( 11 ) NOT NULL DEFAULT '0',
    `used_ips` LONGTEXT NULL ,
    PRIMARY KEY ( `id` )
    ) ENGINE = MYISAM AUTO_INCREMENT =3;'

    This ran and then I initialized by running.

    'INSERT INTO `clientz_ratings` ( `id` , `total_value` , `total_votes` , `used_ips` )
    VALUES (
    '1', '0', '0', NULL
    ), (
    '2', '0', '0', NULL
    );'

    The paths seem ok to me. Everything runs fine (i.e. no errors) except the vote does not register!!
  • Also what does this paragraph in the README file mean?

    I did away with the INSERT statement, because someone could get the db.php href and alter that (though not the rating or the IP) to add a new line into your db. I couldn't figure out a decent way to check that. So, at least for now, any new raters placed on a page have to be entered in your db manually, before votes can be registered to that id.

    Is this the source of my troubles?
  • Yup, it is the insert and initialize thing. So I added the following line into the _drawrating.php file and all is well!!!!

    After the line:

    $query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ")or die(" Error: ".mysql_error());

    Add:

    if (mysql_num_rows($query) == 0) {
    $sql = "INSERT INTO $tableName (`id`,
    `total_votes`, `total_value`, `used_ips`)
    VALUES ('$id', '0', '0', '')";
    $result = mysql_query($sql);
    }

    And it works.

    Of course, on this forum I can never get code to show up accurately so I don't know but believe me this works. Of course I wonder if the original reason for avoiding the INSERT command in favor of manual insert is still valid. Any thoughts anybody?
  • Just to give credit where it is due...here is the big forum on how to use this rating system: http://www.masugadesign.com/the-lab/scripts/unobtrusive-ajax-star-rating-bar/

    Comment number 12. WaxOr is what helped.

    Others have done things like most popular etc.
  • @jay-

    did you do line #4 on my tutorial? I noted that in the drawrating file, I had to COMPLETELY hardcode the URL to get it to work:

    `&q=<?php echo $id ?>&t=<?php echo $ip ?>&c=<?php echo $units ?>" title="<?php echo $ncount ?> out of <?php echo $units ?>" class="r<?php echo $ncount ?>-unit rater"><?php echo $ncount ?>`

    Thus, http://www.thinkdreams.com/zenphoto/themes/thinkdreams_photoblog would be what I would use in my case. That might be causing your issue.
  • sorry, i was composing my response after you had posted, and I didn't refresh.... Glad to see it working.
  • So do you think the INSERT INTO $tableName solution is a bad idea? If so, I might try your URL instead of SERVER PATH suggestion. BTW, thanks a lot for everything.

    Now for some cool icons...I think the stars (starrating.gif) are much nicer but they come with a rectangular white background block that does not take kindly to transparency. On the masugadesign.com page they tell you how to design custom icons.
  • if you look in the images dir, i started on a psd template of my own that i use, that you can model from if you like.
  • Great stuff guys, with all the changes in this discussion it works perfectly!
  • Oh great. Very thoughtful.
  • I just tried without the INSERT mod and even with a complete URL and manual database initialization it does not work for me. So back to INSERT and all that comes with it.
  • hmm. what i can't understand is why some installations it works, and on some it doesn't. seems to be ok for subjunk.

    @subjunk, is there anything possibly different about your install that you could share that might help jayray out?

    and mod_rewrite is off right now on yours jayray right?
  • No mod_rewrite is ON. I thought you dismissed that as a possible cause. Anyway, as long as the INSERT command is not terribly insecure I don't mind running it so.
  • No mod_rewrite is ON. I thought you dismissed that as a possible cause. Anyway, as long as the INSERT command is not terribly insecure I don't mind running it so.
  • mod_rewrite needs to be off. then it should work. (at least from what i've found). since most people's 1.0.6 installations are going to be non-mod_rewrite to use subalbums anyway, I assumed where I should have explicitly stated it in the instructions. I apologize for that oversight.

    Try it without mod_rewrite just for kicks. The problem is that the db.php call in the drawrating file is coded for a specific url and parameters, and it will get messed around with mod_rewrite. There may be a way to fix it in the mod_rewrite statements but I'm not sure how to go about that.
  • Ok. I will experiment and let you know.
  • Cool. Thanks Jay for testing too.
  • Sorry thinkdreams, still did not work. I did all the changes carefully in .htaccess and zp_config.php to switch off mod_rewrite. Anyway, at least we know that it will work perfectly (with mod_rewrite ON and without full URL) of we insert:

    if (mysql_num_rows($query) == 0) {
    $sql = "INSERT INTO $tableName (id,
    total_votes, total_value, used_ips)
    VALUES ('$id', '0', '0', '')";
    $result = mysql_query($sql);
    }

    After the line:

    $query=mysql_query("SELECT total_votes, total_value, used_ips FROM $tableName WHERE id='$id' ")or die(" Error: ".mysql_error());
  • Sorry I don't know what isn't working. Your MySQL query to create the table isn't working? Is that the problem?
    Sorry I'll check back on this thread more often and try to help. I do have another worthwhile change to make though, for those of you who want valid code:

    In _drawrating.php, change:

    <?php echo $ncount ?>&q=<?php echo $id ?>&t=<?php echo $ip ?>&c=<?php echo $units ?>" title="<?php echo $ncount ?> out of <?php echo $units ?>" class="r<?php echo $ncount ?>-unit rater"><?php echo $ncount ?>

    To:

    <?php echo $ncount ?>&q=<?php echo $id ?>&t=<?php echo $ip ?>&c=<?php echo $units ?>" title="<?php echo $ncount ?> out of <?php echo $units ?>" class="r<?php echo $ncount ?>-unit rater"><?php echo $ncount ?>

    It just escapes the ampersands, just that little change got rid of 18 perceived W3C errors for me.
  • Haha okay well that didn't work. How silly of me :p
    Replace all the ampersands (&) with:
    & amp; (remove the space between & and a)
  • Okay, a few little bugs. The line 122 "behaviour undefined" error is fixed by just adding:

    `/js/behavior.js">`

    Then another error occurs when a star (or whatever image you use for rating) is clicked, the error is:

    `Line 60, character 2: Null is null or an invalid object'`

    No filename is given in the error but I expect it is in rating.js
    On line 63 of rating.js there is:

    `xmlhttp.send(null);`

    Anyone have any ideas of how to fix this error?
  • script works good (though after adding the `INSERT`-statement in _drawrating.php), thanx thinkdreams and other developers.
  • curiouz Member
    @subjunk: I tried to add that line with "behavior.js" but that makes the rating fuction hang at the loading sign (try it here: http://www.bewoog.nl/zp/natuurgetrouw/IMG_8014.JPG.php). Did you get it to work properly without it needing to reload?
  • SubJunk Member
    Hi, yes I got working but I've forgotten how. If you send me an email at subjunkNO@SPAMgmail.com I can send you my code so you can see what I changed by comparing mine and yours
Sign In or Register to comment.