I’m trying to make a script that outputs user names based on weather they are assigned an odd or even value. I think I’ve managed to get the odd ones working but the even ones wont output. Here is what it looks like. The 'commentid' is the value which determines if they are to be assigned to odd or even.
<?php
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbname', $db);
$query2 = "SELECT * FROM comments";
$result2 = mysql_query($query2);
while ($row2 = mysql_fetch_assoc($result2))
if ( $row2['commentid'] % 2 )
{
echo $row2['name'];
echo "<br />";
}
elseif (!$row2['commentid'] % 2)
{
echo $row2['name'];
}
else
{
echo "";
}
?>
I think it has something to do with either the $row2['name'] or (!$row2['commentid'] % 2) or maybe I need to assign rows to strings or something but I can’t figure out what I’m doing wrong.
EDIT I really should have made it more clear exactly what I am trying to do with this script because while your answers make sense they dont solve my problem. On the same page as this script I have another script running. This script calls a value from a different table in the same database. It’s running on a timed cron event that resets daily so the value is different every day. It looks like this and is working as it should.
<?
$db = mysql_connect('localhost','username','pass') or die("Database error");
mysql_select_db('dbnamesameasother', $db);
$query = "SELECT pool FROM winners";
$result = mysql_query($query) or die(mysql_error());
while ($row = mysql_fetch_array($result))
if ( $row['pool'] % 2 )
{
echo "<h4>Result 1</h4>";
echo "<br />";
}
else
{
echo "<h4>Result 2</h4>";
echo "<br />";
}
?>
What I am trying to do is only call up the names associated with either the odd or even value based on the number the script above dictates. So if this script chooses Result 1, I only want to display the user names who would fall under Result 1.
Hope this helps