|
| |
|
|
Giomanach
Posts: 6187 Joined: 11/19/2003 From: England Status: offline
|
PHP if else not working - 5/15/2009 8:01:18
Maybe I'm just going blind, but been trying to fix this for hours to no avail. Bascailly, I want it so that if the users IP address matches one stored in the database (attached to the right pollid) it will display results rather than the poll itself. Now, I know its pulling and doing all data processes correctly, but the if else doesnt match, regardless of whether I add or remove my IP address to the database. Here's the code: <?
//Connection File for Database
require("conn.php");
if(!$con){
echo ("Database Could Not Be Opened");
echo mysql_error();
exit;
}
//Get Users REAL IP Address
function getRealIpAddr()
{
if (!empty($_SERVER['HTTP_CLIENT_IP'])) //check ip from share internet
{
$ip=$_SERVER['HTTP_CLIENT_IP'];
}
elseif (!empty($_SERVER['HTTP_X_FORWARDED_FOR'])) //to check ip is pass from proxy
{
$ip=$_SERVER['HTTP_X_FORWARDED_FOR'];
}
else
{
$ip=$_SERVER['REMOTE_ADDR'];
}
return $ip;
}
$ipaddress = getRealIPAddr();
$pollid = $_GET['id'];
$savedip = mysql_query("SELECT * FROM results WHERE ipaddress='$ipaddress' AND poll_id='$pollid'");
while($ipad = mysql_fetch_array($savedip));
$storedip = $ipad['ipaddress'];
//If Users IP matches a record in teh Database - Show Poll Results
if ($storedip == $ipaddress){
$query = "SELECT result, COUNT(result) FROM results WHERE poll_id='$pollid' GROUP BY result";
$count = "SELECT COUNT(*) FROM results";
$result = mysql_query($query,$con) or die(mysql_error());
$result2 = mysql_query($count,$con) or die(mysql_error());
while($row = mysql_fetch_array($result2)){
$total = $row['COUNT(*)'];
}
?>
<div width="100%">
<?
// Print out result
while($row = mysql_fetch_array($result)){
$width = $row['COUNT(result)'];
$answer = $row['result'];
$percent = $width/$total;
$divwidth = $percent * 100;
?>
<? echo $answer; ?> - <? echo $width; ?><div style="background: #000; width: <? echo $divwidth; ?>%; height: 1.4em; color: #FFF; text-align: center; max-width: 100%;"></div>
<br />
<?
}
?>
</div>
<?
}
else
{
$query = "SELECT * FROM polls WHERE id=$pollid";
$poll = mysql_query($query,$con);
if(!$poll){
echo "Could Not Run Query: mysql_error()";
exit;
}
$row = mysql_fetch_object($poll);
echo ("<form method='post' action='subpoll.php'>");
echo ("<fieldset>");
echo ("$row->poll_name<br /> \n");
echo ("<input type='hidden' value='$row->id' name='id' /> \n");
echo ("<input type='hidden' value='$ipaddress' name='ip' /> \n");
echo ("<input type='radio' name='option' value='$row->answer1' /> <label>$row->answer1</label><br /> \n");
echo ("<input type='radio' name='option' value='$row->answer2' /> <label>$row->answer2</label><br /> \n");
echo ("<input type='submit' value='Cast Vote' /> <input type='reset' value='reset' /> \n");
echo ("</fieldset> \n");
echo ("</form> \n");
}
?>
Any ideas?
_____________________________
|
|
New Messages |
No New Messages |
Hot Topic w/ New Messages |
Hot Topic w/o New Messages |
Locked w/ New Messages |
Locked w/o New Messages |
|
Post New Thread
Reply to Message
Post New Poll
Submit Vote
Delete My Own Post
Delete My Own Thread
Rate Posts
|
|
|