Query all items and ignore if condition
I current have two functions which perform queries on my database; the
first one performs a query on all my entries and the second has a
specification (for version control).
Database
id | version_id
---------------
1 | 1
2 | 2
3 | 1
4 | 1
Code
public function post($postid)
{
global $db;
$query = "SELECT * FROM news WHERE id='$postid'";
return $db->select($query);
}
public function version_count()
{
global $db;
$query = "SELECT * FROM news WHERE post_id='1' ORDER BY version_id
DESC LIMIT 1";
return $db->select($query);
}
To display my entries, I've tried the following, which does as expected,
giving me all entries:
$posts_array = $query->post($post);
foreach ( $counted_versions as $post )
{
echo $post->id . "-";
}
However, I only want to echo rows 2 and 4, since 2 is unique and 4 is the
most recent.
At first, I thought this logic would work :
$posts_array = $query->post($post);
$counted_versions = $query->version_count();
foreach ( $counted_versions as $counted )
{
foreach ( $posts_array as $post )
{
echo $post->id . "-";
}
}
But rather, I get 1-2-3-4- and if I switch my foreach loops around, I get
4-4-4-4-, whereas I would expect 2-4 or at the very least 1-4 (or 4-2 or
4-1).
Anybody have a way to get a similar result?
No comments:
Post a Comment