All this
if ($rater_rating <= 0 ){$rater_stars = "./img/0.jpg";$rater_stars_txt="Not Rated";}
if ($rater_rating >= 5){$rater_stars = "./img/1.jpg";$rater_stars_txt="5";}
if ($rater_rating >= 10 ){$rater_stars = "./img/2.jpg";$rater_stars_txt="10";}
if ($rater_rating >= 15){$rater_stars = "./img/3.jpg";$rater_stars_txt="15";}
if ($rater_rating >= 20 ){$rater_stars = "./img/4.jpg";$rater_stars_txt="20";}
if ($rater_rating >= 25){$rater_stars = "./img/5.jpg";$rater_stars_txt="25";}
if ($rater_rating >= 30 ){$rater_stars = "./img/6.jpg";$rater_stars_txt="30";}
if ($rater_rating >= 35){$rater_stars = "./img/7.jpg";$rater_stars_txt="35";}
if ($rater_rating >= 40 ){$rater_stars = "./img/8.jpg";$rater_stars_txt="40";}
if ($rater_rating >= 45){$rater_stars = "./img/9.jpg";$rater_stars_txt="45";}
if ($rater_rating >= 50 ){$rater_stars = "./img/10.jpg";$rater_stars_txt="50";}
if ($rater_rating >= 55){$rater_stars = "./img/11.jpg";$rater_stars_txt="55";}
if ($rater_rating >= 60 ){$rater_stars = "./img/12.jpg";$rater_stars_txt="60";}
if ($rater_rating >= 65){$rater_stars = "./img/13.jpg";$rater_stars_txt="65";}
if ($rater_rating >= 70 ){$rater_stars = "./img/14.jpg";$rater_stars_txt="70";}
if ($rater_rating >= 75){$rater_stars = "./img/15.jpg";$rater_stars_txt="75";}
if ($rater_rating >= 80 ){$rater_stars = "./img/16.jpg";$rater_stars_txt="80";}
if ($rater_rating >= 85){$rater_stars = "./img/17.jpg";$rater_stars_txt="85";}
if ($rater_rating >= 90 ){$rater_stars = "./img/18.jpg";$rater_stars_txt="90";}
if ($rater_rating >= 95){$rater_stars = "./img/19.jpg";$rater_stars_txt="95";}
if ($rater_rating >= 100 ){$rater_stars = "./img/20.jpg";$rater_stars_txt="100";}
All those ifs can be replaced with
if( $rater_rating < 0 ){
$rater_rating = 0;
}elseif( $rater_rating > 100 ){
$rater_rating = 100;
}
$rater_stars_txt = $rater_rating - $rater_rating % 5;
$rater_stars = "./img/" . ( $rater_stars_txt / 5 ) . 'jpg';
if( $rater_stars_txt == 0 ){
$rater_stars_txt = 'Not Rated';
}
As demonstrated here
function CalculationTest( $rater_rating ){
if( $rater_rating < 0 ){
$rater_rating = 0;
}elseif( $rater_rating > 100 ){
$rater_rating = 100;
}
$rater_stars_txt = $rater_rating - $rater_rating % 5;
$rater_stars = "./img/" . ( $rater_stars_txt / 5 ) . 'jpg';
if( $rater_stars_txt == 0 ){
$rater_stars_txt = 'Not Rated';
}
echo '$rater_rating=' . $rater_rating
. ', $rater_stars=' . $rater_stars
. ', $rater_stars_txt=' . $rater_stars_txt
, '<br/>';
}
for( $i = -10; $i < 110; $i++ ){
CalculationTest( $i );
}
Very large amounts of similar calculations cause blindness over time 😉
What ever your solution is it will be mathmatical rather than breaking DRY. The reason this may be hard for you as it breaks that principal making it a lot harder to comprehend for those who did not write it, as the question when reading other peoples code is what is different. The more that appears the same the harder it is to see what is different.
When you come back to it it may be the same for you as it requires recollection to be easy.
I speak from experience on this as the project I am working at the moment is a search engine simlar to jobserve and due to all the copy paste monkeys at the view level means a 3 day task is 2 weeks. Unless the copy paste junk is dealt with then it will just get worse.
My tip to you is when you find something hard ask yourself why is it hard. Just bad code or lack of knowledge.
I know you probably did not write this but all code that breaks DRY should be killed ASAP for your sanity 😉
I can't really read your question as I am code blind from all the code monkey stuff I have dealt with today who should of known better for their pay grade 🙂
I think my code is right but I am way to tired...
PHP_EOL is the constant for $rater_end_of_line_char though I would switch to a database of some kind else performance will get increasingly problematic, databases are designed to scale and have concurrent changes. Else use a CSV and use http://uk2.php.net/manual/en/function.fgetcsv.php
http://uk2.php.net/manual/en/function.fputcsv.php
Still will get heavy performance issues if it gets large but it will look cleaner.
With this
<?php
if(isset($_REQUEST["rate".$rater_id])){
if(isset($_REQUEST["rating_".$rater_id])){
while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
$rater_rating=$val;
}
$rater_ip = getenv("REMOTE_ADDR");
$rater_file=fopen($rater_filename,"a+");
$rater_str="";
$rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
if($rater_str!=""){
if($rater_ip_voting_restriction){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_ip_vote_count=0;
foreach($rater_data as $d){
$rater_tmp=explode("|",$d);
$rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
if($rater_ip==$rater_oldip){
$rater_ip_vote_count++;
}
}
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
$rater_msg=$rater_already_rated_msg;
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
fclose($rater_file);
}else{
$rater_msg=$rater_not_selected_msg;
}
}
?>
The following can be removed ( all the same )
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
}else{
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg=$rater_thankyou_msg;
}
with
<?php
if(isset($_REQUEST["rate".$rater_id])){
if(isset($_REQUEST["rating_".$rater_id])){
while(list($key,$val)=each($_REQUEST["rating_".$rater_id])){
$rater_rating=$val;
}
$rater_has_already_rated = false;
$rater_ip = getenv("REMOTE_ADDR");
$rater_file=fopen($rater_filename,"a+");
$rater_str="";
$rater_str = rtrim(fread($rater_file, 1024*8),$rater_end_of_line_char);
if($rater_str!=""){
if($rater_ip_voting_restriction){
$rater_data=explode($rater_end_of_line_char,$rater_str);
$rater_ip_vote_count=0;
foreach($rater_data as $d){
$rater_tmp=explode("|",$d);
$rater_oldip=str_replace($rater_end_of_line_char,"",$rater_tmp[1]);
if($rater_ip==$rater_oldip){
$rater_ip_vote_count++;
}
}
if($rater_ip_vote_count > ($rater_ip_vote_qty - 1)){
$rater_has_already_rated = true;
$rater_msg=$rater_already_rated_msg;
}
}
}
if( !$rater_has_already_rated ){
fwrite($rater_file,$rater_rating."|".$rater_ip.$rater_end_of_line_char);
$rater_msg = $rater_thankyou_msg;
}
fclose($rater_file);
}else{
$rater_msg=$rater_not_selected_msg;
}
}
?>
and
Warning: fopen(item_1.rating) [function.fopen]: failed to open stream: Permission denied in /home/a5934501/public_html/star/rater.php on line 33
Is a file permission issue as by default it is undesirable to have it writable on everything.. if the name 'apache' is the service owner then
chown apache folderpath
chmod +755 folderpath
on the command line
That will guarantee any files created in that folder will be Apache writable/readable/executable and readable/executable for anyone else. Though if just readable for everyone else +744 ( 4=Read ,2=Write , 1=execute )
http://ss64.com/bash/chmod.html
http://nersp.nerdc.ufl.edu/~dicke3/nerspcs/chown.html
Anyway you get the gist.. Can't test the file writing stuff as I am at my code limit for the day...