Recent Topics

1 Jun 27, 2004 17:20    

I'm trying to impliment a rating system for each post. I have most of the interface coded. I am just having trouble with how b2evo accesses the database.

#calculate ratings
function do_rating($postnum, $rating){ 
    if (session_is_registered("rating$postnum")){ 
        echo "Sorry! You have already voted!"; 
    } else { 
        $querystr = "\"SELECT post_rating, post_num_votes FROM evo_posts WHERE id=" . $postnum . "\"";
        $get_count = $DB->query($querystr);  //line with error
        while(list($post_rating, $post_num_votes)=$db->get_list($get_count)){ 
            $new_count = ($post_num_votes + 1); 
            $post_rating2 = ($post_rating * $post_num_votes); 
            $new_rating = (($rating + $post_rating2) / ($new_count)); 
            $new_rating2 = number_format($new_rating, 2, '.', ''); 
            $updatestr = "\"UPDATE evo_posts SET post_rating='$new_rating2',post_num_votes='$new_count' WHERE id=" . $posnum . "\""; 
            $update_rating = $dataobject->dbupdate($updatestr); 
            $sessionvar = "post$postnum"; 
            session_register($sessionvar); 
             
            echo "<div align=\"center\"> 
            <p>Thanks for your vote!</p> 
            <p>The new rating for this post is:             
            $new_rating2 out of 5</p>
            </div>"; 
        } 

    } 
} 

i get "Fatal error: Call to a member function on a non-object"

what does this error mean? how do i fix it?

i'll post the entire hack here if i ever get it working!

2 Jun 27, 2004 17:44

how about adding a

global $DB;

into the func ? ;)

3 Jun 27, 2004 20:56

i inserted this code at the bottom of _functions_bposts.php right now though i will prob move it to my _hacks.php file :roll:

so anyway as u know

	global $DB, $tableposts, $tablepostcats, $query;
	global $localtimenow, $default_locale;

is already at the top of that page.

what else could i try?

4 Jun 27, 2004 22:20

No, it has to be within the function, not just on the page somewhere.

function do_rating($postnum, $rating)
{
    global $DB;
    if (session_is_registered("rating$postnum")){
        echo "Sorry! You have already voted!";
    }
...

Also, you reference $DB once, and $db later. I believe that variable names are case-sensitive, so watch out for that.

5 Jun 28, 2004 03:26

Thanks that fixed THAT problem.

I had some others after that but got them worked out. the code will now do everything except stop multiple votes by the same person on the same post.

Other then that i'm now working on display.

will hopefully post working code and instructions here tomorrow.

6 Jun 28, 2004 05:43

Excellent! I'm looking forward to it.

I'm guessing that you'll come to love php as you work with it more. It's really very forgiving.

7 Jun 28, 2004 21:12

ok current working version:

warning the code is prob. rather sloppy. PHP is still too new to me to write good tight code. Revisions are appreatiated!

first manually insert two additional fields to evo_posts in your MySQL DB:
post_rating and post_num_votes.

Can someone code an automatic way of doing this?

second insert this into /conf/hacks.php


DEFINE("IMAGEROOT", "images/");  //SET THE PATH OF THE ASSOCIATED IMAGES

#build our rating menu
function ratemenu($postnum){ 
    #rating titles
    $fivelabel = 'Great Idea';  //LABEL FOR A 5 OUT OF 5 RATING
    $fourlabel = 'Good';  //LABEL FOR A 4 OUT OF 5 RATING
    $threelabel = 'Average';  //LABEL FOR A 3 OUT OF 5 RATING
    $twolabel = 'Not Your Best';  //LABEL FOR A 2 OUT OF 5 RATING
    $onelabel = 'Bad Idea';  //LABEL FOR A 1 OUT OF 5 RATING
    $zerolabel = 'WTF?';  //LABEL FOR A 0 OUT OF 5 RATING
    
    
    echo "<form name=\"rating\" method=\"post\" action=index.php?disp=ratepost>
    <font class=\"subfont\">Rate this Post:</font> 
    <select name=\"rating\"> 
    <option value=\"5.0\">5 - " . $fivelabel . "</option> 
    <option value=\"4.0\">4 - " . $fourlabel . "</option> 
    <option value=\"3.0\" selected>3 - " . $threelabel . "</option> 
    <option value=\"2.0\">2 - " . $twolabel . "</option> 
    <option value=\"1.0\">1 - " . $onelabel . "</option> 
    <option value=\"0.0\">0 - " . $zerolabel . "</option> 
    #<input type=\"hidden\" name=\"cmd\" value=\"do_rating\"> 
    <input type=\"hidden\" name=\"postnum\" value=\"$postnum\"> 
    <input type=\"submit\" value=\"Go!\"> 
    </select> 
    </form>"; 
} 

#calculate ratings
function do_rating($postnum, $rating){ 
    global $DB, $DataObject;
    
    if ($HTTP_COOKIE_VARS["post$postnum"]){ 
        echo "Sorry! You have already voted!"; 
    } else { 
        $querystr = "SELECT post_rating FROM evo_posts WHERE id=" . $postnum;
        $get_rating = $DB->get_var($querystr); 
        $querystr = "SELECT post_num_votes FROM evo_posts WHERE id=" . $postnum;
        $get_count = $DB->get_var($querystr); 
        $post_rating = $get_rating;
        $post_num_votes =  $get_count;
        $new_count = ($post_num_votes + 1); 
        $post_rating2 = ($post_rating * $post_num_votes); 
        $new_rating = (($rating + $post_rating2) / ($new_count)); 
        $new_rating2 = number_format($new_rating, 2, '.', ''); 
        $updatestr = "UPDATE evo_posts SET post_rating=$new_rating2,post_num_votes=$new_count WHERE id=" . $postnum; 
        $update_rating = $DB->query($updatestr); 
        #$sessionvar = "post$postnum"; 
        #setcookie($sessionvar, $sessionvar); 
             
        echo "<div align=\"center\"> 
        <p>Thanks for your vote!</p> 
        <p>The current rating for this post is:             
        $new_rating2 out of 5</p>
        </div>"; 
    } 
} 

#display ratings

    #star images
    $zerostar = '0.gif';  //IMAGE FOR NO STARS
    $onestar = '1.gif';  //IMAGE FOR ONE STAR
    $twostar = '2.gif';  //IMAGE FOR TWO STARS
    $threestar = '3.gif';  //IMAGE FOR THREE STARS
    $fourstar = '4.gif';  //IMAGE FOR FOUR STARS
    $fivestar = '5.gif';  //IMAGE FOR FIVE STARS


function post_stars($post_rating){ 
    if((($post_rating >= 0)or($post_rating == 0)) && ($post_rating <= 0.99)){ 
        echo "<img src=\"" . IMAGEROOT . $zerostar . "\" width=\"70\" height=\"18\">"; 
    } 
    if((($post_rating >= 1.00)or($post_rating == 1.00)) && ($post_rating <= 1.99)){ 
        echo "<img src=\"" . IMAGEROOT . $onestar . "\" width=\"70\" height=\"18\">";     
    } 
    if((($post_rating >= 2.00)or($post_rating == 2.00)) && ($post_rating <= 2.99)){ 
        echo "<img src=\"" . IMAGEROOT . $twostar . "\" width=\"70\" height=\"18\">"; 
    } 
    if((($post_rating >= 3.00)or($post_rating == 3.00)) && ($post_rating <= 3.99)){ 
        echo "<img src=\"" . IMAGEROOT . $threestar . "\" width=\"70\" height=\"18\">"; 
    } 
    if((($post_rating >= 4.00)or($post_rating == 4.00)) && ($post_rating <= 4.99)){ 
        echo "<img src=\"" . IMAGEROOT . $fourstar . "\" width=\"70\" height=\"18\">"; 
    } 
    if($post_rating == 5.0){ 
        echo "<img src=\"" . IMAGEROOT . $fivestar . "\" width=\"70\" height=\"18\">"; 
    } 
}

The first function is called from _main.php where your posts go.


<?php ratemenu("$Item->ID") ?>

third add a case to your post includes inorder to display the results of the voteing.


case 'ratepost':
        // this displays new post rating
        do_rating($postnum, $rating);
        break;

This is the functionality as i finally ended up using it. The third function only displays pictures of the # of stars or dots or whatever out of 5 that you wish to design. I decided to not do a graphical representation, but its there for others to play with.

Also there is still no rate blocking, so anyone can vote as many times in a row as they like for every post!

8 Jul 03, 2004 07:39

Thats Great!

i was thinking about making a photo contest in my communty blog and this come in a good time :)

I'll just wait too see the opinion of Fplanque and if he has something to add.

Thanx :)

9 Jul 06, 2004 01:36

So... somebody has tryed? Anyone ?

10 Sep 11, 2004 17:49

code by phpmyadmin to alter table:


ALTER TABLE `evo_posts` ADD `post_num_votes` FLOAT UNSIGNED NOT NULL ,
ADD `post_rating` FLOAT UNSIGNED NOT NULL ;

ALTER TABLE `evo_posts` ADD INDEX ( `post_num_votes` , `post_rating` ) ;

11 Feb 10, 2005 22:17

Hey I did what you said , but I see this at the begin of each page , in the Back office also at the being of the blog's first page:
(mind that Ihave installed B2ev on my own computer by local host)

DEFINE("IMAGEROOT", "images/"); //SET THE PATH OF THE ASSOCIATED IMAGES #build our rating menu function ratemenu($postnum){ #rating titles $fivelabel = 'Great Idea'; //LABEL FOR A 5 OUT OF 5 RATING $fourlabel = 'Good'; //LABEL FOR A 4 OUT OF 5 RATING $threelabel = 'Average'; //LABEL FOR A 3 OUT OF 5 RATING $twolabel = 'Not Your Best'; //LABEL FOR A 2 OUT OF 5 RATING $onelabel = 'Bad Idea'; //LABEL FOR A 1 OUT OF 5 RATING $zerolabel = 'WTF?'; //LABEL FOR A 0 OUT OF 5 RATING echo....

and in the blogs page , I get this error :

Fatal error: Call to undefined function: ratemenu() in c:\program files\easyphp1-7\www\rojbash\skins\custom\_main.php on line 94

Plus I have another question :
where should I add this case Code:

case 'ratepost': 
        // this displays new post rating 
        do_rating($postnum, $rating); 
        break; 

12 Feb 17, 2005 12:58

I also tried to install this hack recently, but it didn't work. I suspect it's not compatible with the current version of b2evo, but if someone can fix it up so that it works with the current version, I'd be very interested in this hack.

14 Nov 21, 2005 01:30

Has anyone else worked on this? The link that DTHP posted works, but there's an error in the coding right where the rating system should be... (looks like a bad omen... ;) ) This would be SO cool if I could get it for my blog... even if there's a JavaScript rating software out there somewhere... I've tried searching the engines but I couldn't find anything...


Form is loading...