/** * get all news feed. * * @access public * @param integer $pageNum * @return array * */ public function getAll($pageNum = 1) { $pagination = Pagination::pagination("newsfeed", "", [], $pageNum); $offset = $pagination->getOffset(); $limit = $pagination->perPage; $database = Database::openConnection(); $query = "SELECT newsfeed.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, newsfeed.content, newsfeed.date "; $query .= "FROM users, newsfeed "; $query .= "WHERE users.id = newsfeed.user_id "; $query .= "ORDER BY newsfeed.date DESC "; $query .= "LIMIT {$limit} OFFSET {$offset}"; $database->prepare($query); $database->execute(); $newsfeed = $database->fetchAllAssociative(); return array("newsfeed" => $newsfeed, "pagination" => $pagination); }
/** * get all posts * * @access public * @param integer $pageNum * @return array Associative array of the posts, and Pagination Object. * */ public function getAll($pageNum = 1) { $pagination = Pagination::pagination("posts", "", [], $pageNum); $offset = $pagination->getOffset(); $limit = $pagination->perPage; $database = Database::openConnection(); $query = "SELECT posts.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, posts.title, posts.content, posts.date "; $query .= "FROM users, posts "; $query .= "WHERE users.id = posts.user_id "; $query .= "ORDER BY posts.date DESC "; $query .= "LIMIT {$limit} OFFSET {$offset}"; $database->prepare($query); $database->execute(); $posts = $database->fetchAllAssociative(); $this->appendNumberOfComments($posts, $database); return array("posts" => $posts, "pagination" => $pagination); }
/** * get all files. * * @access public * @param integer $pageNum * @return array * */ public function getAll($pageNum = 1) { //get pagination object $pagination = Pagination::pagination("files", "", [], $pageNum); $offset = $pagination->getOffset(); $limit = $pagination->perPage; $database = Database::openConnection(); $query = "SELECT files.id AS id, files.filename, users.id AS user_id, users.name AS user_name, files.extension AS format, files.hashed_filename, files.date "; $query .= "FROM users, files "; $query .= "WHERE users.id = files.user_id "; $query .= "ORDER BY files.date DESC "; $query .= "LIMIT {$limit} OFFSET {$offset}"; $database->prepare($query); $database->execute(); $files = $database->fetchAllAssociative(); return array("files" => $files, "pagination" => $pagination); }
/** * get all users in the database * * @access public * @param string $name * @param string $email * @param string $role * @param integer $pageNum * @return array * */ public function getUsers($name = null, $email = null, $role = null, $pageNum = 1) { //validate user inputs $validation = new Validation(); if (!$validation->validate(['User Name' => [$name, 'alphaNumWithSpaces|maxLen(30)'], 'Email' => [$email, 'email|maxLen(50)'], 'Role' => [$role, 'inArray(admin, user)']])) { $this->errors = $validation->errors(); return false; } //in $options array, add all possible values from user, and their name parameters //then applyOptions() method will see if value is not empty, then add it to our query $options = [$name => "name = :name ", $email => "email = :email ", $role => "role = :role "]; //get options query $options = $this->applyOptions($options, "AND "); $options = empty($options) ? "" : "WHERE " . $options; $values = []; if (!empty($name)) { $values[":name"] = $name; } if (!empty($email)) { $values[":email"] = $email; } if (!empty($role)) { $values[":role"] = $role; } //get pagination object so that we can add offset and limit in our query $pagination = Pagination::pagination("users", $options, $values, $pageNum); $offset = $pagination->getOffset(); $limit = $pagination->perPage; $database = Database::openConnection(); $query = "SELECT id, name, email, role, is_email_activated FROM users "; $query .= $options; $query .= "LIMIT {$limit} OFFSET {$offset}"; $database->prepare($query); $database->execute($values); $users = $database->fetchAllAssociative(); return array("users" => $users, "pagination" => $pagination); }
/** * get all comments of a post * * @access public * @param array $postId * @param integer $pageNum * @param integer $commentsCreated * @return array Associative array of the comments, and Pagination Object(View More). * */ public function getAll($postId, $pageNum = 1, $commentsCreated = 0) { //Only for comments, We use $commentsCreated //What's it? Whenever we create a comment, It will be added in-place to the current comments in current .php page, //So, we need to track of those were created, and skip them in the Pagination($offset & $totalCount). $options = "WHERE comments.post_id = :post_id "; $pagination = Pagination::pagination("comments", $options, [":post_id" => $postId], $pageNum, $commentsCreated); $offset = $pagination->getOffset() + $commentsCreated; $limit = $pagination->perPage; $database = Database::openConnection(); $query = "SELECT comments.id AS id, users.profile_picture, users.id AS user_id, users.name AS user_name, comments.content, comments.date "; $query .= "FROM users, posts, comments "; $query .= "WHERE comments.post_id = :post_id "; $query .= "AND posts.id = comments.post_id "; $query .= "AND users.id = comments.user_id "; $query .= "ORDER BY comments.date DESC "; $query .= "LIMIT {$limit} OFFSET {$offset}"; $database->prepare($query); $database->bindValue(':post_id', (int) $postId); $database->execute(); $comments = $database->fetchAllAssociative(); //you can have post with no comments yet! return array("comments" => $comments, "pagination" => $pagination); }
<?php $obj = new Pagination(); $data = $obj->pagination(); ?> <h1 class="courses_title">Popular courses</h1> <!-- courses section --> <div class="course-list"> <!-- course first section --> <?php if (isset($data) && !empty($data)) { $i = 0; foreach ($data as $row) { $i++; if ($i == 5) { break; } ?> <div class = "column <?php if ($i % 4 == 0) { echo 'last'; } ?> "> <div class="column-content"> <!-- course first section img --> <div class="col-img"> <a href="course_info.php?id=<?php echo $row->id; ?>
<div class="course-list"> <!-- course first section --> <?php $obj = new Pagination(); $q = $obj->pagination(); if (isset($q) && !empty($q)) { $i = 0; foreach ($q as $row) { $i++; ?> <div class = "column <?php if ($i % 4 == 0) { echo "last"; } ?> "> <div class="column-content"> <!-- course first section img --> <div class="col-img"> <a href="course_info.php?id=<?php echo $row->id; ?> "> <img src="<?php echo $row->course_img; ?> " alt="<?php echo $row->img_alt; ?> " /> </a>
<?php $obj = new Course(); $data = $obj->getAll(); $pag = new Pagination(); $dataPag = $pag->pagination(); ?> <div class="row"> <h1>Courses</h1> </div> <div class="course-list"> <?php if (isset($dataPag) && !empty($dataPag)) { $no = 0; foreach ($dataPag as $row) { $no++; ?> <div class = "column <?php if ($no % 4 == 0) { echo "last"; } ?> "> <div class="column-content"> <!-- course first section img --> <div class="col-img"> <a href="course-info.php?id=<?php echo $row->id; ?>