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.
From what I understand, you're wanting to know if there were rows returned from your query (since the query already checks to see if there were entires from the last 7 days) and if so, display them, and if not, display your message.
To achieve this, you'll need something like this:
if($stmt->execute(array($id))){
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
do {
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>";
} while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
} else {
echo 'Nothing to show now!';
}
}
Once the statement is successfully executed, it attempts to fetch the first row. If it's successful, it falls into a do{}while
loop, with the same logic you had before. The difference is, if no row is fetched, then it will display your "Nothing to show!" message.
I also moved some stuff around to be a little more logical with the new code. Full code:
<?php
$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))){
// if there is a row to fetch, get it
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
do {
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>";
// moved these echos in here so that it won't display if there are no rows
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>";
// also moved this in here so it won't display if there are no rows.
$stmt = $db->prepare('SELECT id,username,tag,message FROM mybq_post_txt where id = ?');
if($stmt->execute(array($id))){
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
do {
echo "<div class='vot_updown2' id=" . 'vt_' .$table.($row['id']). "></div>";
} while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
}
}
} while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
// if no rows, show this message
} else {
echo 'Nothing to show now!';
}
}
} 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))){
if ($row = $stmt->fetch(PDO::FETCH_ASSOC)) {
do {
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>";
} while ($row = $stmt->fetch(PDO::FETCH_ASSOC));
// if no rows, show this message
} else {
echo 'Nothing to show now!';
}
}
}
?>
EDIT: updated code with the else statement
I guess you have to redirect to the login page when there's no session and if is session, there's no username.
First it check is session is set, thats false , and then check if myusername exists, and thats false.
if (!isset($_SESSION['myusername']) && $_SESSION['myusername'] == '')