TeddyKiller Posted April 19, 2010 Share Posted April 19, 2010 I have a function, though that doesn't matter as such. I have a piece of code, in which similar stuff (different collum names) get used. So imagine the code below.. 10 times, with different collum names. I'm needing to simplify it, shorten it etc. It is a huge mess, and 10times this is a nightmare. $profile, is details of the current profile being viewed. $userq, is the details of current user logged in. (YOU) $user, is details of the current profile being viewed's user. - Different to $profile, as $profile relates to the profile table, and $user refers to the users table. $detail, is an array of titles. if($profile->rel_status) { if($display['mar_sta_display'] == '1') { $html_marital = '<tr><td id="tdtitlestyle">' . $detail[4] . '</td><td id="tdgap"></td><td>' . ucwords($profile->rel_status) . '</td></tr>'; } else { if($display['mar_sta_display'] == '2') { $query = mysql_query("select * from friends where user_id = '$userq->id' and friend_id = '$id'"); if(mysql_num_rows($query) == 1) { $html_marital = '<tr><td id="tdtitlestyle">' . $detail[4] . '</td><td id="tdgap"></td><td>' . ucwords($profile->rel_status) . '</td></tr>'; } else { if($id == $userq->id) { $html_marital = '<tr><td id="tdtitlestyle">' . $detail[4] . '</td><td id="tdgap"></td><td>' . ucwords($profile->rel_status) . '</td></tr>'; } } } else { if($display['mar_sta_display'] == '3') { if($id == $userq->id) { $html_marital = '<tr><td id="tdtitlestyle">' . $detail[4] . '</td><td id="tdgap"></td><td>' . ucwords($profile->rel_status) . '</td></tr>'; } } } } } All help appreciated. Quote Link to comment Share on other sites More sharing options...
ignace Posted April 19, 2010 Share Posted April 19, 2010 I have a function, though that doesn't matter as such. I have a piece of code, in which similar stuff (different collum names) get used. So imagine the code below.. 10 times, with different collum names. Thanks, whatever I ate during lunch is now lying on the floor.. Why not just use a function for it? Your application is suffering from viscosity, which means that it's easier to build bridges around problems then to actually solve the problem. The added problem is that you are beyond the point-of-no-return in your application as rethinking your application structure would mean a complete rewrite. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 19, 2010 Author Share Posted April 19, 2010 ¬,¬ It is in a function. It's in the users details function, but that snippet is all I needed assistance with.. If 1, display to everyone, if 2, display to friends and the users profile owner, if 3.. display to owner only. I just can't think of the way around it. Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 19, 2010 Author Share Posted April 19, 2010 I created this functon.. so I dont have to repeat myself. function detail_permissions($permission, $display, $proid, $userid, $title) { $info_to_display = '<tr><td id="tdtitlestyle">' . $title . '</td><td id="tdgap"></td><td>' . ucwords($display) . '</td></tr>'; $query = mysql_query("select * from friends where user_id = '$userq->id' and friend_id = '$id'"); if($display) : if($permission == '1') : return $info_to_display; else : if($permiission == '2') : if(mysql_num_rows($query) == 1) : return $info_to_display; else : if($proid == $userid) : return $info_to_display; endif; endif; else : if($permission == '3') : if($proid == $userid) : return $info_to_display; endif; else : return false; endif; endif; endif; else : return false; endif; } Not tested, about to try it out now. EDIT: You access the functon via doing this.. $html_marital = detail_permissions($display['mar_sta_display'], $profile->rel_status, $id, $userq->id, $detail[4]); Although, no errors occur, but nothing gets displayed. hmm Quote Link to comment Share on other sites More sharing options...
TeddyKiller Posted April 19, 2010 Author Share Posted April 19, 2010 It's not getting the permission :/ edit: I spelt permission wrong in the 2nd bit. 'permiision' Fail. It works anywho. If anyone got any better suggestions. Post them through Quote Link to comment Share on other sites More sharing options...
dirkers Posted April 19, 2010 Share Posted April 19, 2010 Nearly as many lines as your function, but I think this is much easier to read: function detail_permissions($permission, $display, $proid, $userid, $title) { $info_to_display = '<tr><td id="tdtitlestyle">' . $title . '</td><td id="tdgap"></td><td>' . ucwords($display) . '</td></tr>'; $query = mysql_query("select * from friends where user_id = '$userq->id' and friend_id = '$id'"); $retVal = FALSE; if ($display) { switch ($permission) { case '1': $retVal = $info_to_display; break; case '2': $retVal = (mysql_num_rows($query) == 1 || $proid == $userid) ? $info_to_display : FALSE; break; case '3': $retVal = ($proid == $userid) ? $info_to_display : FALSE; break; } } return $retVal; } I hope it works, since I rewrote it without actually executing it. -D Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.