public function recommendations($num = 10) { if ($this->get_cluster()) { return $this->get_cluster()->recommendations($num); } else { return Article::find_all($num); } }
public function recommendations($num = 10) { if ($num == 0) { return array(); } // Append special chars to query string for variable // length inputs= $var_list = ":0"; for ($i = 1; $i < $this->size(); $i++) { $var_list .= ", :" . $i; } try { // Prepare the necessary queries $clicks = Database::prepare(str_replace("%list", $var_list, self::$SQL_REC)); // Execute the query for ($i = 0; $i < $this->size(); $i++) { $clicks->bindValue(":" . $i, $this->data["users"][$i], PDO::PARAM_STR); } $today = date("Y-m-d 00:00:00"); $clicks->bindValue(":today", $today, PDO::PARAM_STR); $clicks->execute(); // Find all the articles associated with the recommended // urls $clicks = $clicks->fetchAll(PDO::FETCH_ASSOC); foreach ($clicks as $info) { $recommendations[] = Article::find($info["url"]); } if (empty($clicks)) { $recommendations = array(); } // If there are less than $num recommended urls, fill the // remainder of the request with the newest articles $num_normal = $num - count($clicks); $articles = Article::find_all($num); for ($i = 0; $i < $num; $i++) { if (!in_array($articles[$i], $recommendations)) { $recommendations[] = $articles[$i]; } } return $recommendations; } catch (PDOException $e) { echo "Error: " . $e; return false; } }