Jump to content

How can I simplify/shorten this code


TeddyKiller

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/199055-how-can-i-simplifyshorten-this-code/
Share on other sites

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.

¬,¬ 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.

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.