0

I am working on a control-panel for a VoIP program and I've been working on it for a while. It started off as a small project that I just did because it was fun, but over time I added more and more features and it has now become something quite complex.

I have a file called functions.php and it contains functions for handling login-requests, fetching a users avatar, getting memberlists, etc. It also performs any SQL operations required to perform these functions.

What I have noticed over time is that this file has become quite big, and there is a lot of code that is repeated -- especially SQL operations.

Here is an example of my get_memberlist() function:

function get_memberlist($mysqli, $start = 0, $stop = 10, $sortby = 'username', $order = 'desc', $filter = null) {
    if (isset($_SESSION['user_id'], $_SESSION['username'], $_SESSION['login_string'])) {
        if ($sortby == 'nickname') $sortby = 'last_nickname';
            if ($stmt = $mysqli->query("SELECT * FROM users WHERE (user_id LIKE '$filter' OR email LIKE '%$filter%' OR last_nickname LIKE '%$filter%' OR username LIKE '%$filter%') ORDER BY $sortby $order LIMIT $start , $stop")) {
                if($stmt->num_rows != 0) {
                    $row = array();
                    while($r = $stmt->fetch_array(MYSQLI_ASSOC)) {
                    $row[] = $r;
                }
                return $row;
            }
        }
        return $row;
    }
}

which is also quite similar to my getServerLogs() function:

function getServerLogs($mysqli, $limit, $start, $end, $query, $filter) {
    if ($stmt = $mysqli->query("SELECT * FROM server_logs WHERE `date` >= $start AND `date` <= $end AND is_query = $query AND message LIKE '%$filter%' ORDER BY id DESC LIMIT $limit")) {
        if($stmt->num_rows != 0) {
          $row = array();
          while($r = $stmt->fetch_array(MYSQLI_ASSOC)) {
               $row[] = $r;
            }
        }
        return $row;
    }
}

This seems inefficient and has repetitive code.

So my question is this: From a design perspective, how should I write the various functions and how the database operations?

Should I separate functions from database operations and have just a few generic db functions to call? Perhaps the best approach is to just have a db-file and send the query to be performed as a parameter?

Thanks.

  • Hmm, something model-view-controller would be the way to go I guess. What you describe would be a very simplified approach to a model layer which would already be a step ahead to separate your business and database model from the rest of the app. (Also unrelated: I am no PHP dev, but those functions look quite confusing and seem to return $row in various situation without assigning a value to this variable. Maybe guard clauses would help clean that up a bit by avoiding to nest if/then statements too deep) – thorsten müller Jun 22 '15 at 9:13
  • @thorstenmüller, yes I have begun looking into rewriting my entire project in Laravel which would encorporate the mvc model, but I thought it might still be good to know for any future (smaller) projects of how to approach this. What seems to be confusing though? – Chris Jun 22 '15 at 9:41
  • yes, a framework would be a good idea. Once you worked with one you will get the idea. At least for tasks like those it will give you all you need (beyond that you would have to look into 'design patterns' and OOP but this is a larger thing.). Confusing to me is that if some of those if conditions fail, you still do return $row while no value was assigned to it (which happens only deeper inside those nested if/then). – thorsten müller Jun 22 '15 at 10:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Browse other questions tagged or ask your own question.