SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Need opinions on improving a block of code.

    I have a block of code that only counts some numbers up (its an achievement)

    This is what i have and as you can tell its pretty dirty so i am looking for better ways to do this(If there is).
    Code PHP:
    // total number of goals completed, lots of dirty math i would expect here
     
    	$posts_40 = ($member['user_posts'] >= 40) ? true : false;
    	$posts_80 = ($member['user_posts'] >= 80) ? true : false;
    	$posts_160 = ($member['user_posts'] >= 160) ? true : false;
    	$posts_240 = ($member['user_posts'] >= 240) ? true : false;
    	$posts_480 = ($member['user_posts'] >= 480) ? true : false;
    	$posts_600 = ($member['user_posts'] >= 600) ? true : false;
    	$posts_1000 = ($member['user_posts'] >= 1000) ? true : false;
    	$posts_2000 = ($member['user_posts'] >= 2000) ? true : false;
    	$posts_3000 = ($member['user_posts'] >= 3000) ? true : false;
     
    	$total_post_goal = $posts_40 + $posts_80 + $posts_160 + $posts_240 + $posts_480 + $posts_600 + $posts_1000 + $posts_2000 + $posts_3000;
     
    	$total_topic 	=	 has_created_topic($user_id);
    	$total_topic_10 =	($total_topic >= 10) ? true : false;
    	$total_topic_20 =	($total_topic >= 20) ? true : false;
    	$total_topic_50 =	($total_topic >= 50) ? true : false;
     
    	$total_topic_goal = $total_topic_10 + $total_topic_20 + $total_topic_50;
     
    	$total_topic_view = get_total_topic_views($user_id);
    	$total_topic_view_500 = ($total_topic_view >= 500) ? true : false;
    	$total_topic_view_1000 = ($total_topic_view >= 1000) ? true : false;
    	$total_topic_view_10000 = ($total_topic_view >= 10000) ? true : false;
     
    	$total_topic_views	= $total_topic_view_500 + $total_topic_view_1000 + $total_topic_view_10000;
     
    	$topic_replies = get_total_topic_replies($user_id);
    	$topic_replies_25 = ($topic_replies >= 25) ? true : false;
    	$topic_replies_50 = ($topic_replies >= 50) ? true : false;
    	$topic_replies_100 = ($topic_replies >= 100) ? true : false;
    	$topic_replies_500 = ($topic_replies >= 500) ? true : false;
     
    	$total_topic_replies = $topic_replies_25 + $topic_replies_50 + $topic_replies_100 + $topic_replies_500;
     
    	$has_poll = (has_created_poll($user_id)) ? true : false;
    	$has_voted_poll = (has_voted_poll($user_id)) ? true : false;
    	$has_avatar = ($member['user_avatar']) ? true : false;
    	$has_uploaded = (has_attachments($user_id)) ? true : false;
     
    	$randy_goals = $has_poll + $has_voted_poll + $has_avatar + $has_uploaded;
     
    	$has_friend = total_friends($user_id);
    	$friend_5 = ($has_friend >= 5) ? true : false;
    	$friend_10 = ($has_friend >= 10) ? true : false;
    	$friend_20 = ($has_friend >= 20) ? true : false;
    	$friend_50 = ($has_friend >= 50) ? true : false;
     
    	$friend_goals = $friend_5 + $friend_10 + $friend_20 + $friend_50;
     
    	$member_for = total_join_days($user_id);
    	$member_for_30 = ($member_for >= 30) ? true : false;
    	$member_for_60 = ($member_for >= 60) ? true : false;
    	$member_for_365 = ($member_for >= 365) ? true : false;
     
    	$member_goal = $member_for_30 + $member_for_60 + $member_for_365;
     
    	$per_day = total_join_days($user_id);
    	$post_per_day_5 = ($member['user_posts'] / $per_day >= 5) ? true : false;
    	$post_per_day_10 = ($member['user_posts'] / $per_day >= 10) ? true : false;
    	$post_per_day_20 = ($member['user_posts'] / $per_day >= 20) ? true : false;
     
    	$post_per_day_goal = $post_per_day_10 + $post_per_day_5 + $post_per_day_20;
     
    	$total_goals_reached = $total_post_goal + $total_topic_goal + $total_topic_views + $total_topic_replies + $randy_goals + $friend_goals + $member_goal + $post_per_day_goal;

    Basically its just counting it all up and giving me a number so i can put "goals reached x/33"

    Works great rite now but i know there must be a more efficient way to do it.

    Thanks,

    Ian

  2. #2
    SitePoint Member
    Join Date
    Jul 2009
    Posts
    6
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I would break each section into functions, and check the if statements starting from big to small and break, since a user with 3000 post must already have posts <3000, why have all those extra if statements. The same thing for the other sections.

  3. #3
    SitePoint Zealot
    Join Date
    Jan 2009
    Posts
    142
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by ThePHPMaster View Post
    I would break each section into functions, and check the if statements starting from big to small and break, since a user with 3000 post must already have posts <3000, why have all those extra if statements. The same thing for the other sections.
    Because you get a check once you reach 40 posts, 80 posts and i am trying to count up each check mark so im not sure how that would work


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •