NotionCommotion Posted August 5, 2015 Share Posted August 5, 2015 How can I cleanly and efficiently modify the following script so that I do not have to define the empty $user array twice? public function editRecord(){ $id=isset($_GET['id'])?$_GET['id']:null; if(is_null($id)) { $user=array('id'=>0,'firstname'=>'','lastname'=>'','username'=>'','access_id'=>10); } else { $sql='SELECT id,firstname,lastname,username FROM users WHERE id=? AND sites_id=?'; $stmt = $this->db->prepare($sql); $stmt->execute(array($id,$this->site->id)); if(!$user=$stmt->fetch(PDO::FETCH_ASSOC)){ $user=array('id'=>0,'firstname'=>'','lastname'=>'','username'=>'','access_id'=>10); } } $this->displayPage(__DIR__.'/templates/editRecord.html',array('user'=>$user,'access'=>$this->getList('access'))); } Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/ Share on other sites More sharing options...
ginerjm Posted August 5, 2015 Share Posted August 5, 2015 Initialize it at the start of your function? Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/#findComment-1518087 Share on other sites More sharing options...
Solution Ch0cu3r Posted August 5, 2015 Solution Share Posted August 5, 2015 (edited) Define the user array at the start of the function. Then overwrite the $user array with the result from the database if one was returned public function editRecord() { // default user array $user=array('id'=>0,'firstname'=>'','lastname'=>'','username'=>'','access_id'=>10); $id= isset($_GET['id']) ? $_GET['id'] : null; if(!is_null($id)) { $sql='SELECT id,firstname,lastname,username FROM users WHERE id=? AND sites_id=?'; $stmt = $this->db->prepare($sql); $stmt->execute(array($id,$this->site->id)); // if query returned a result, override $user array if($stmt->numCount() != 0) $user = $stmt->fetch(PDO::FETCH_ASSOC); } $this->displayPage(__DIR__.'/templates/editRecord.html',array('user'=>$user,'access'=>$this->getList('access'))); } Edited August 5, 2015 by Ch0cu3r Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/#findComment-1518088 Share on other sites More sharing options...
NotionCommotion Posted August 5, 2015 Author Share Posted August 5, 2015 Well, I feel rather silly. Thanks for the help! Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/#findComment-1518091 Share on other sites More sharing options...
NotionCommotion Posted August 6, 2015 Author Share Posted August 6, 2015 if($stmt->numCount() != 0) Do you mean rowCount()? http://php.net/manual/en/pdostatement.rowcount.php Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/#findComment-1518146 Share on other sites More sharing options...
Ch0cu3r Posted August 6, 2015 Share Posted August 6, 2015 Yes. Quote Link to comment https://forums.phpfreaks.com/topic/297647-eliminating-duplicated-code/#findComment-1518148 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.