Recent Topics

1 Sep 13, 2004 05:16    

i created 2 functions to show hit rate of each post, without doing any avoid, it just count when somebody click permalink to read more.

it can show graphic of hit rating
http://www.j2.idv.tw/media/rate.gif
or text.
http://www.j2.idv.tw/media/rate2.gif

just step by step:

======================================
1. Create Table to b2evo DB

CREATE TABLE `post_rate` (
`post_ID` INT( 11 ) UNSIGNED NOT NULL ,
`post_rating` INT( 6 ) UNSIGNED DEFAULT '0' NOT NULL ,
INDEX ( `post_ID` , `post_rating` )
);

======================================
2. Copy these into /conf/hacks.php

<?php

DEFINE("IMAGEROOT", "/img/");  // set the image path to show star
DEFINE("MAX_RATE", "50");        // more popular site adjust higher

function post_stars($p_ID, $show_img=false){
	global $DB;
	$sql_query = 'SELECT post_rating FROM post_rate WHERE post_ID = ' . $p_ID;
	$rating = $DB->get_var($sql_query);
	$alt = "alt=\"Hit Rating!\"";
	if( $show_img ){
		if( $rating == 0 )
			$file="0";
		if( ($rating > 0) && ($rating < MAX_RATE/10 ) ) $file="1";
		if( ($rating >= MAX_RATE/10) && ($rating < 2*MAX_RATE/10) )  $file="2";
		if( ($rating >= 2*MAX_RATE/10) && ($rating < 3*MAX_RATE/10) ) $file="3";
		if( ($rating >= 3*MAX_RATE/10) && ($rating < 4*MAX_RATE/10) ) $file="4";
		if( ($rating >= 4*MAX_RATE/10) && ($rating < 5*MAX_RATE/10) ) $file="5";
		if( ($rating >= 5*MAX_RATE/10) && ($rating < 6*MAX_RATE/10) ) $file="6";
		if( ($rating >= 6*MAX_RATE/10) && ($rating < 7*MAX_RATE/10) ) $file="7";
		if( ($rating >= 7*MAX_RATE/10) && ($rating < 8*MAX_RATE/10) ) $file="8";
		if( ($rating >= 8*MAX_RATE/10) && ($rating < 9*MAX_RATE/10) ) $file="9";
		if( ($rating >= 9*MAX_RATE/10) ) $file="10";
		
		echo "<img src=\"" . IMAGEROOT . "$file.gif\" $alt >";
	}
	else {
		if( $rating == '' )$rating = 0;
		echo "views: $rating";
	}
}

function save_rate($p_ID){
	global $DB;
	$sql_query = 'SELECT post_rating FROM post_rate WHERE post_ID = ' . $p_ID;
	$post_rating = $DB->get_var($sql_query);
	if( $post_rating > 0 ){
		$post_rating++;
		$sql_query = "UPDATE post_rate SET post_rating =  $post_rating  WHERE post_ID =". $p_ID;
	}
	else{
		$sql_query = 'INSERT INTO `post_rate` ( `post_ID`, `post_rating`) VALUES (' . $p_ID . ',1)';
	}
	$DB->query($sql_query);
}

?>


======================================
3. adjust ur MAX_RATE
if ur site is more popular, adjust more higher

======================================
4. add hit of each post
add this code to while loop at /skins/ur_skin/_main.php
after this line

if( isset($MainList) ) while( $Item = $MainList->get_item() ) {

insert this code

<?php	if( $disp == 'single' ) save_rate( $Item->get('ID') ); ?>

======================================
5. show hit rating of each post
add this code to while loop at /skins/ur_skin/_main.php

<?php post_stars(  $Item->get("ID"), false ); ?>


p.s set "true" to show img, default set is false to show text
u need add 0.gif~10.gif into IMAGEROOT defined in conf/hacks.php

======================================
have fun :D

2 Sep 13, 2004 16:53

Sorry for my silly Q, i am a PHP newbie

How to Create Table to b2evo DB ??

thanks in advance

3 Sep 13, 2004 17:39

hi, ladybird,

create table into "mysql" Database have many ways...
u must know how to connect mysql, select DB u want, and create table

i think u can read first before u want hack something in b2evo

3.1 Connecting to and Disconnecting from the Server
3.3.1 Creating and Selecting a Database
3.3.2 Creating a Table

on this link
http://dev.mysql.com/doc/mysql/en/Tutorial.html

than connect mysql, chose a DB b2evolution build in,
and type the code in step 1 i wrote

4 Feb 09, 2005 23:36

I need your Help Jimmy...

I love Taiwan , Because I feel your pain !
I am a kurd and my country is invaded by 6 country so there is no Kurdistan...now ...
whenever I see Taiwan's indepence I feel good, and I feel bad when I see China's warning.

I have a Kurdish Blog --> Rojbash.net , we are a group that post intresting links , everyday ,

I want to add something so peopel can Rate each link , and i can have the best links of the Week in a page or at side bar,

mind that peopel here don't click on the Permanent link or Read more link , peopel here click on the Tilte that contains a link to go .

do you have any idea or help ?
I would love your help !

6 Feb 10, 2005 07:51

I want something like this :

Peopel can Rate a posts with (1-5) numbers. and we can have the high rated posts on the side bar.

or

as long as that my blog is a Linking To Good News Blog , it would be good to count the most clicked links and have them on the side bar .
(in our blog, we put links in the LINK FIELD)

my blog adress : www.rojbash.net

8 Jul 26, 2005 02:50

Hey jimmy, this is a great hack! I used it, and what I realy like about it, is that the performance is very good of this one.
I was wondering if you know any way to show a little overview of the most hitted items.

The SQL query should be something like this:

SELECT *
FROM `post_rate`
ORDER BY post_rating DESC
LIMIT 0 , 20

But I don't know realy how to transform this to a little list of topics that is shown e.g. in the sidebar. Can someone or you explain how to do this?

9 Oct 29, 2005 07:39

I don't have a file conf/hacks.php :-/

Edit: I suspect this was introduced in .11. I only have .10

I'm guessing hacks.php is simply a file that is included on all the relevent pages so that any functions dropped into that file will work. Can I simply drop this code into one of the other included pages (that exist in .10) and this mod should work. I'd love to get this mod going but simply don't have the time to upgrade my blog to the latest version at this time.

Thanks.

10 Oct 29, 2005 08:09

hacks.php doesn't exist you have to create it.

Open notepad, copy and paste the relevant code and save it as hacks.php and upload it to your conf folder.

¥

11 Nov 12, 2005 00:50

Fatal error: Call to a member function on a non-object in /home/mydir/public_html/blog/skins/custom/_main.php on line 53

I created the table. I uploaded hack.php but I get this error on both function calls.

I'm using the latest b2evolution version. Anyone?

Thanks!

12 Nov 12, 2005 04:27

ferrethouse2005 wrote:

I created the table. I uploaded hack.php but I get this error on both function calls.

hacks.php not hack.php
maybe you can check that
answer is below.

13 Nov 15, 2005 08:00

ferrethouse2005 wrote:

Fatal error: Call to a member function on a non-object in /home/mydir/public_html/blog/skins/custom/_main.php on line 53

Maybe your code at wrong position.

add this code into while loop at /skins/ur_skin/_main.php
after this line

if( isset($MainList) ) while( $Item = $MainList->get_item() ) {

insert this code

<?php if( $disp == 'single' ) save_rate( $Item->get('ID') ); ?>

14 Nov 20, 2005 13:27

p.s set "true" to show img, default set is false to show text
u need add 0.gif~10.gif into IMAGEROOT defined in conf/hacks.php

any ideas where to get this sort of images to use with your hack?

thanks

17 Jan 04, 2006 07:23

is there any way to show a list of most popular hitting post??

thanks for the answer

18 Jun 19, 2006 08:27

Thank you very much, this hack is great and easy to apply, I'm really pleased with the result!
I modified it as you said to count max/200 instead of max/10, I just added more pictures,
as for the ALT feature, I'm thinking of writing

$alt = "alt=\"Read  $Rating  times\"";

(did it in French) instead of

$alt = "alt=\"Hit Rating!\"";

it works.

Thank you so much again!

19 Jun 19, 2006 08:32

Pablo wrote:

is there any way to show a list of most popular hitting post??

thanks for the answer

There should be a way, imitating this very hack : create a table, create a IF that checks the $rating status for every post and then ECHO that table somewhere in your page.
But I'm not php-fluent enough to write this down, sorry :oops:

20 Aug 30, 2006 00:54

See this in action at http://www.goedzo.com

Here is the implementation of the list of most hitted topics:

Add the following code to hacks.php:


function read_most_rated() {
	 global $DB;
    $query = "SELECT pr.post_id, pr.post_rating, ep.post_title, ep.post_urltitle FROM `post_rate` AS pr LEFT JOIN evo_posts AS ep ON pr.post_id = ep.id where post_title != \"\" ORDER BY post_rating DESC LIMIT 0 , 20";
    $ratings = $DB->get_results( $query, ARRAY_N );
    return $ratings; //is an array
}


function read_least_rated() {
	 global $DB;
    $query = "SELECT pr.post_id, pr.post_rating, ep.post_title, ep.post_urltitle FROM `post_rate` AS pr LEFT JOIN evo_posts AS ep ON pr.post_id = ep.id  where post_title != \"\" ORDER BY post_rating ASC LIMIT 0 , 20";
    $ratings = $DB->get_results( $query, ARRAY_N );
    return $ratings; //is an array
}

In your skin file add this (I used my sidebar).


	<div class="bSideItem">
    <h3 class="sideItemTitle">Most viewed topics</h3>
		<?php
			$ratings = read_most_rated();
    		foreach( $ratings as $r){
    			$NewItem = Item_get_by_ID( $r[0] );
    			$permalink = $NewItem->gen_permalink( '', '', false, '&' ); 
    			$title = $r[2];
    			if (strlen($title) > 30) {
    				$title = substr($title,0,30)."...";
    			}
    			else {}
    			
	        	echo "<a href=\"".$permalink."\">".$title."</a> (".$r[1].")<br />\r\n";
    		}
		?>

	</div>

And for the least viewed topic (alway fun to show)


	<div class="bSideItem">
    <h3 class="sideItemTitle">Least viewed subjects</h3>
		<?php
			$ratings = read_least_rated();
    		foreach( $ratings as $r){
    			$NewItem = Item_get_by_ID( $r[0] );
    			if ($NewItem ) {
	    			$permalink = $NewItem->gen_permalink( '', '', false, '&' ); 
	    			$title = $r[2];
	    			if (strlen($title) > 30) {
	    				$title = substr($title,0,30)."...";
	    			}
	    			else {}
				}else{}    			
	        	echo "<a href=\"".$permalink."\">".$title."</a> (".$r[1].")<br />\r\n";
    		}
		?>
	</div>


Form is loading...