As described above, you must divide and win in order to make your life easier (especially when you write code while playing with it in this great function). This works as simple as:
function file_put($number, $data) { $path = sprintf("C:/temp/wamp/www/file%d.txt", $number); file_put_contents($path, $data); }
for example, itβs just replacing a lot of repeating lines in which you just need a numbered file in which you put some line.
But you can also do this with more complex things, such as database operations. You will probably want to transfer the error handling from your point of view, as well as take care of the need to connect to the database when necessary, and a more flexible way to obtain data. This can be done by moving the (mildly obsolete) mysql_* functions into one or two classes of its own so that it disappears from view. This will greatly facilitate its use (which I will show first):
// Create your database object to use it later on: $config = array( 'server' => 'localhost', 'name' => 'root', 'password' => '', 'db' => 'test', ); $db = new MySql($config);
I named the database class MySql as it represents the mysql connection and it works with the old mysql extension. You only need to pass this database object to the function in your question. In combination with the file_put function file_put it will look like this:
function checkin(MySql $DB, $TechID, $ClientID, $SiteID) { $query = sprintf("SELECT `Type` FROM `Log` WHERE `TechID` = '%d' ORDER BY LogTime DESC LIMIT 1", $TechID); file_put(5, $query); $result1 = $DB->query("SELECT COUNT(*) FROM Log"); $result2 = $DB->query($query); foreach ($result1 as $row1) { list($count) = $row1; $data = "ClientID:$ClientID TechID:$TechID SiteID:$SiteID Count:$count" file_put(3, $data); foreach ($result2 as $row2) { file_put(4, $data); } } }
Nevertheless, the checkin function is close to a big one (already 12 lines of code), but much shorter than your first version, since it delegates work to write files and access the database. I hope this demo will be helpful. The following is a complete code example:
class MySqlException extends RuntimeException { } class MySql { private $server; private $name; private $password; private $db; private $connection; public function __construct(array $config) { $this->server = $config['server']; $this->name = $config['name']; $this->password = $config['password']; $this->db = $config['db']; } private function connect($server, $name, $password) { $this->connection = mysql_connect($server, $name, $password); if (!$this->connection) { $this->error("Unable to connect to '%s' as user '%s'", $server, $name); } } private function select($db) { if (!mysql_select_db($db, $this->connection)) { $this->error("Unable to select database '%s'", $db); } } private function close() { $this->connection && mysql_close($this->connection); } private function connectSelect() { $this->connect($this->server, $this->name, $this->password); $this->select($this->db); } public function query($query) { $this->connection || $this->connectSelect(); $result = mysql_query($query, $this->connection); if (!$result) { $this->error("Unable to execute query '%s'", $query); } return new MySqlResult($result); } private function error($format) { $args = func_get_args(); array_shift($args); $format .= ': %s'; $args[] = $this->connection ? mysql_error($this->connection) : mysql_error(); throw new MySqlException(vsprintf($format, $args)); } public function __destruct() { $this->close(); } } class MySqlResult implements Iterator, Countable { private $result; private $index = 0; private $current; public function __construct($result) { $this->result = $result; } public function fetch($result_type = MYSQL_BOTH) { $this->current = mysql_fetch_array($this->result, $result_type); return $this->current; } public function current() { return $this->current; } public function next() { $this->current && $this->fetch(); } public function key() { return $this->current ? $this->index : null; } public function valid() { return (bool)$this->current; } public function rewind() { $this->fetch(); } public function count() { return mysql_num_rows($this->result); } }