Jump to content

Simplify code


KillGorack
Go to solution Solved by Psycho,

Recommended Posts

Simple code but has a lot of if then statements..

can it be simplified somehow?

within a database class.

    function columninfo($table, $col = NULL)
    {
      if($this->CheckTable($table)){
        if($col == NULL){
          $err = false;
        }elseif($this->Checkfield($table, $col)){
          $err = false;
        }else{
          $err = true;
        }
        if($err == false){
          $stmt = $this->db->prepare('SELECT * FROM '.$table);
          $stmt->execute();
          $numCol = $stmt->columnCount();
          $rtrn = array();
          for ($i = 0; $i < $numCol; $i++) {
              if($col == NULL || $stmt->getColumnMeta($i)['name'] == $col){
                $rtrn[] = $stmt->getColumnMeta($i);
              }
          }
          if(count($rtrn) == 1){
            $rtrn = $rtrn[0];
          }
          return $rtrn;
        }else{
          return false;
        }
      }else{
        return false;
      }
    }

<edit>

Sorry if the second parameter is left out, just return info on all the columns. If it's supplied return for just that column.

</edit>

Edited by KillGorack
Link to comment
Share on other sites

Suggestion #1:

if ($condition) {
	// ...
} else {
	return $value;
}

// is the same as

if (!$condition) {
	return $value;
}
// ...

Suggestion #2:

if ($condition_1) {
	// thing
} else if ($condition_2) {
	// same thing
}

// is the same as

if ($condition_1 || $condition_2) {
	// thing
}

 

I have two more, but let's start with those and see what you do.

Link to comment
Share on other sites

  • Solution

Here 0is how I would handle that

function columninfo($table, $col = NULL)
{
    //State the error condition check
    if(!$this->CheckTable($table)){ return false; }
    //State the error condition check
    if($col!=NULL && !$this->Checkfield($table, $col)){ return false; }
    
    //No errors
    $stmt = $this->db->prepare('SELECT * FROM '.$table);
    $stmt->execute();
    $numCol = $stmt->columnCount();
    $colAry = array();
    for ($i = 0; $i < $numCol; $i++) {
        if($stmt->getColumnMeta($i)['name'] == $col) {
            //Return type for selected column
            return $stmt->getColumnMeta($i)[0];
        } else {
            //Append current column to return arrray
            $colAry[] = $stmt->getColumnMeta($i);
        }
    }
    //Return array for all columns
    return $colAry;
}

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.