PHP Assitance - Modifying Rating Script

This is a discussion on PHP Assitance - Modifying Rating Script within the PHP General forums, part of the PHP Programming Forums category; If you have a few minutes, I would like to see if anyone can help me out. I'm trying ...


Go Back   Usenet Forums > PHP Programming Forums > PHP General

FAQ Members List Calendar Search Today's Posts Mark Forums Read
  #1 (permalink)  
Old 11-14-2006
brentpaugh@gmail.com
 
Posts: n/a
Default PHP Assitance - Modifying Rating Script

If you have a few minutes, I would like to see if anyone can help me
out. I'm trying to modify a rating script. Currently, It allows you
to vote over and over. What I want to do is after you vote it will
write your IP to a text file called ip.txt in the same folder the
script currently writes the rating.txt
('content/'.$y.'/'.$m.'/'.$entry.'/). I want it to check that ip.txt
file to see if your ip is there, if it is then it doesn't write the
new vote. I am sure this can be done rather quickly; I'm just stuck
since I do not know a lot of php (still learning).


Here is the current function.






// ----------------
// Rating Functions
// ----------------

function write_rating( $y, $m, $entry, $rating ) {
// Save Rating
$result_array = read_rating( $y, $m, $entry, $rating );
if ( $result_array ) {
$result_array[ 'points' ] = $result_array[ 'points' ] + $rating;
$result_array[ 'votes' ] = $result_array[ 'votes' ] + 1;
} else {
$result_array = array();
$result_array[ 'points' ] = $rating;
$result_array[ 'votes' ] = 1;
}

$str = '';
$keys = array_keys( $result_array );
for ( $i = 0; $i < count( $keys ); $i++ ) {
$key = $keys[ $i ];
if ( $i > 0 ) {
$str = $str . '|';
}
$str = $str . $key . '|' . $result_array[ $key ];
}

$dir = 'content/'.$y.'/'.$m.'/'.$entry;
if ( !file_exists( $dir ) ) {
$oldumask = umask( 0 );
$ok = mkdir( $dir, 0777 );
umask( $oldumask );
if ( !$ok ) {
return( NULL );
}
}

$filename = 'content/'.$y.'/'.$m.'/'.$entry.'/rating.txt';



sb_write_file( $filename, $str );



}

function read_rating( $y, $m, $entry ) {
// Read the rating.txt file and return the stored data.
//
// Returns NULL on fail.
$rating_path = 'content/'.$y.'/'.$m.'/'.$entry.'/';
$contents = sb_read_file( $rating_path . 'rating.txt' );

if ( $contents ) {
$result_array = array();

$data_array = explode('|', $contents);
$key_array = array( 'points', 'votes' );

for ( $i = 0; $i < count( $data_array ); $i = $i + 2 ) {
for ( $j = 0; $j < count( $key_array ); $j++ ) {
if ( $data_array[ $i ] == $key_array[ $j ] ) {
$key = $key_array[ $j ];
$result_array[ $key ] = intval( $data_array[ $i+1 ] );
}
}
}

return( $result_array );
}
}
_________________

Reply With Quote
  #2 (permalink)  
Old 11-15-2006
petersprc@gmail.com
 
Posts: n/a
Default Re: PHP Assitance - Modifying Rating Script

Try adding:
....
$result_array[ 'ips' ] = $result_array[ 'ips' ] . ',' .
$_SERVER['REMOTE_ADDR'];
....
$result_array['ips'] = $_SERVER['REMOTE_ADDR'];
....
$key_array = array( 'points', 'votes', 'ips' );
....

Try this, where you see votes referenced, add code to handle the clien

brentpaugh@gmail.com wrote:
> If you have a few minutes, I would like to see if anyone can help me
> out. I'm trying to modify a rating script. Currently, It allows you
> to vote over and over. What I want to do is after you vote it will
> write your IP to a text file called ip.txt in the same folder the
> script currently writes the rating.txt
> ('content/'.$y.'/'.$m.'/'.$entry.'/). I want it to check that ip.txt
> file to see if your ip is there, if it is then it doesn't write the
> new vote. I am sure this can be done rather quickly; I'm just stuck
> since I do not know a lot of php (still learning).
>
>
> Here is the current function.
>
>
>
>
>
>
> // ----------------
> // Rating Functions
> // ----------------
>
> function write_rating( $y, $m, $entry, $rating ) {
> // Save Rating
> $result_array = read_rating( $y, $m, $entry, $rating );
> if ( $result_array ) {
> $result_array[ 'points' ] = $result_array[ 'points' ] + $rating;
> $result_array[ 'votes' ] = $result_array[ 'votes' ] + 1;
> } else {
> $result_array = array();
> $result_array[ 'points' ] = $rating;
> $result_array[ 'votes' ] = 1;
> }
>
> $str = '';
> $keys = array_keys( $result_array );
> for ( $i = 0; $i < count( $keys ); $i++ ) {
> $key = $keys[ $i ];
> if ( $i > 0 ) {
> $str = $str . '|';
> }
> $str = $str . $key . '|' . $result_array[ $key ];
> }
>
> $dir = 'content/'.$y.'/'.$m.'/'.$entry;
> if ( !file_exists( $dir ) ) {
> $oldumask = umask( 0 );
> $ok = mkdir( $dir, 0777 );
> umask( $oldumask );
> if ( !$ok ) {
> return( NULL );
> }
> }
>
> $filename = 'content/'.$y.'/'.$m.'/'.$entry.'/rating.txt';
>
>
>
> sb_write_file( $filename, $str );
>
>
>
> }
>
> function read_rating( $y, $m, $entry ) {
> // Read the rating.txt file and return the stored data.
> //
> // Returns NULL on fail.
> $rating_path = 'content/'.$y.'/'.$m.'/'.$entry.'/';
> $contents = sb_read_file( $rating_path . 'rating.txt' );
>
> if ( $contents ) {
> $result_array = array();
>
> $data_array = explode('|', $contents);
> $key_array = array( 'points', 'votes' );
>
> for ( $i = 0; $i < count( $data_array ); $i = $i + 2 ) {
> for ( $j = 0; $j < count( $key_array ); $j++ ) {
> if ( $data_array[ $i ] == $key_array[ $j ] ) {
> $key = $key_array[ $j ];
> $result_array[ $key ] = intval( $data_array[ $i+1 ] );
> }
> }
> }
>
> return( $result_array );
> }
> }
> _________________


Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are Off
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



All times are GMT +1. The time now is 02:31 PM.


Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.0.0