-
Notifications
You must be signed in to change notification settings - Fork 424
Open
Description
Why can't we get fetch all data form table and just return that data, then in while reading data just pass that data into to json_encode something like this?
In models/Post.php
// Get Posts
public function read() {
// Create query
$query = 'SELECT c.name as category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
FROM ' . $this->table . ' p
LEFT JOIN
categories c ON p.category_id = c.id
ORDER BY
p.created_at DESC';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Execute query
$stmt->execute();
return $stmt;
}Above code to
// Get Posts
public function read() {
// Create query
$query = 'SELECT c.name as category_name, p.id, p.category_id, p.title, p.body, p.author, p.created_at
FROM ' . $this->table . ' p
LEFT JOIN
categories c ON p.category_id = c.id
ORDER BY
p.created_at DESC';
// Prepare statement
$stmt = $this->conn->prepare($query);
// Execute query
$stmt->execute();
return $stmt->fetchAll(PDO::FETCH_ASSOC);
}Then in api/post/read.php
$stmt = $post->read();
$count = Count($stmt);
if($count>0){
echo json_encode( $stmt);
}
else {
echo json_encode(
array('message' => 'No Posts Found')
);
}So that we can cut short the coding right..
Metadata
Metadata
Assignees
Labels
No labels