I have a php file that basically serves up random text. the random text can have its vote incremented or decremented by 1 (one). if the net vote is negative, the text is excluded from view, else show the text. Now i want ONLY text posted within the last week to be considered, else, echo "nothing to show now!"
This is the code i have:
$db = new PDO('mysql:host=localhost;dbname=db;charset=utf8', 'user', 'pass');
$stmt = $db->prepare('SELECT vote FROM voting where item = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
($stmt->execute(array($item)));
($row = $stmt->fetch(PDO::FETCH_ASSOC));
if ($row['vote'] < 0) {
$stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ?');
if ($stmt->execute(array($id))) {
while($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>";
echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
}
}
echo "<div class='loginreq'>This Post has been Voted Out because it achieved a negative vote balance. It can be voted back in by achieving a positive vote balance.</div>";
echo "<div class='loginreq'>NOTE: This post may have been voted out because it was misleading or offensive. vote-in at your own risk.</div>";
$stmt = $db->prepare('SELECT id,username,tag,message FROM mybq_post_txt where id = ?');
if ($stmt->execute(array($id))) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div>";
}
}
}
else
{
$stmt = $db->prepare('SELECT id,username,tag,message,timestamp FROM mybq_post_txt where id = ? AND timestamp > DATE_SUB(NOW() , INTERVAL 7 DAY)');
if ($stmt->execute(array($id))) {
while ($row = $stmt->fetch(PDO::FETCH_ASSOC))
{
echo "<p>", "<span class='tagtext'>author: </span>", ($row['username']), " ", "<span class='tagtext'>timestamp: </span>", ($row['timestamp']), "<br>";
echo "<span class='tagtext'>tag: </span>", ($row['tag']), "<br>";
echo "<p class='image_container'>", ($row['message']), "</p>";
echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div><br>";
}
}
}
edit: code now shortened to most relevant parts.