コード例 #1
0
 function getList($user, $type = 1, $offset = 0, $limit = 10)
 {
     if (!is_numeric($type)) {
         throw new Exception("Type is not recognized");
     }
     $list = array();
     $offset = $offset * $limit;
     $stmCount = $this->db->getConn()->prepare("SELECT COUNT(*) as c from `measurement` where type=:t and user_id=:u order by date desc");
     $stmCount->bindParam(':t', $type);
     $stmCount->bindParam(':u', $user);
     $entryCount = $limit;
     if ($stmCount->execute()) {
         $row = $stmCount->fetch(PDO::FETCH_ASSOC);
         $entryCount = $row['c'];
     }
     $stmt = $this->db->getConn()->prepare("SELECT * from `measurement` where type = :t and user_id = :u order by date desc LIMIT :o , :l");
     $stmt->bindParam(":t", $type);
     $stmt->bindParam(":o", $offset, PDO::PARAM_INT);
     $stmt->bindParam(":l", $limit, PDO::PARAM_INT);
     $stmt->bindParam(":u", $user);
     if ($stmt->execute()) {
         $lst = $stmt->fetchAll(PDO::FETCH_OBJ);
         $index = 0;
         $entries = array($limit);
         foreach ($lst as $entry) {
             $row = new Measurement($entry);
             if ($index + 1 < count($lst)) {
                 $nxtEntry = $lst[$index + 1];
                 $row->difference = $entry->amount - $nxtEntry->amount;
             }
             if ($index < $limit) {
                 $entries[$index] = $row->asArray();
             }
             $index++;
         }
         $records = array_reverse($entries);
         $returnData = array();
         $returnData['entries'] = $records;
         $returnData['records'] = intVal($entryCount);
         return $returnData;
     }
 }