Jump to content

PHP IF ELSE not working


CorruptBlood

Recommended Posts

Hey everyone,

 

Here is the problem. The below code is running everytime in favor of the first parameter. If I switch the operator in the if statement the script NEVER runs the first parameter.

 

Basically I am trying to do the following:

 

1.) Check the current time.

2.) Check the last updated time via variable in MySQL database.

3.) Check the next update time via variable in MySQL database.

 

If next update is less than or equal to current time, run the functions listed, else run the one function.

 

roster.php:

<?php

$link = mysql_connect("hostname","username","password");
if (!$link)
{
	die('Could not connect: ' . mysql_error());
}

mysql_select_db("ryoka_coredb", $link);

function mineData() {
	$ch = curl_init();
	$fp = fopen("guildroster.xml", "w");

	curl_setopt($ch, CURLOPT_URL, "http://www.wowarmory.com/guild-info.xml?r=Mal%27Ganis&gn=Ryoka");
	curl_setopt($ch, CURLOPT_HEADER, 0);
	curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
	curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.1) Gecko/20061204 Firefox/2.0.0.1");
	curl_setopt($ch, CURLOPT_FILE, $fp);

	curl_exec($ch);
	curl_close($ch);
	fclose($fp);
}

function procData() {
	mysql_query("DELETE FROM roster_guild");
	$xmlDoc = new DOMDocument();
	$xmlDoc -> load( 'guildroster.xml' );

	$searchNode = $xmlDoc->getElementsByTagName( "character" );

	foreach( $searchNode as $character )
	{
		$charactername = $character->getAttribute('name');
		$characterlevel = $character->getAttribute('level');
		$characterclass = $character->getAttribute('classId');
		$characterrace = $character->getAttribute('raceId');
		$characterrank = $character->getAttribute('rank');
		$characterrankid = $character->getAttribute('rank');

		if ($characterclass == 1) {
			$characterclass = "Warrior";
		} elseif ($characterclass == 2) {
			$characterclass = "Paladin";
		} elseif ($characterclass == 3) {
			$characterclass = "Hunter";
		} elseif ($characterclass == 4) {
			$characterclass = "Rogue";
		} elseif ($characterclass == 5) {
			$characterclass = "Priest";
		} elseif ($characterclass == 6) {
			$characterclass = "Death Knight";
		} elseif ($characterclass == 7) {
			$characterclass = "Shaman";
		} elseif ($characterclass ==  {
			$characterclass = "Mage";
		} elseif ($characterclass == 9) {
			$characterclass = "Warlock";
		} elseif ($characterclass == 11) {
			$characterclass = "Druid";
		}

		if ($characterrace == 10) {
			$characterrace = "Blood Elf";
		} elseif ($characterrace == 2) {
			$characterrace = "Orc";
		} elseif ($characterrace == 6) {
			$characterrace = "Tauren";
		} elseif ($characterrace ==  {
			$characterrace = "Troll";
		} elseif ($characterrace == 5) {
			$characterrace = "Undead";
		}

		if ($characterrank == 0) {
			$characterrank = "Head Captain";
		} elseif ($characterrank == 1) {
			$characterrank = "Captain";
		} elseif ($characterrank == 2) {
			$characterrank = "Lieutenant";
		} elseif ($characterrank == 3) {
			$characterrank = "Soul Reaper";
		} elseif ($characterrank == 4) {
			$characterrank = "Human";
		} elseif ($characterrank == 5) {
			$characterrank = "Mod Soul";
		}

		mysql_query("INSERT INTO roster_guild (NAME, LEVEL, CLASS, RANK, RACE, RANKID) VALUES ('$charactername','$characterlevel','$characterclass','$characterrank','$characterrace','$characterrankid')");
	}

	$time = strtotime("+6 hours");
	$nextUpdate = date("mdyHis",$time);
	$currentTime = date("mdyHis");

	mysql_query("UPDATE roster_config SET value = '$currentTime' WHERE name = 'lastupdate'");
	mysql_query("UPDATE roster_config SET value = '$nextUpdate' WHERE name = 'nextupdate'");
}

function showData() {
	$fetch = mysql_query("SELECT * FROM roster_guild ORDER BY rankid, level DESC");

	echo "<table style='width: 100%;'>\n";
		echo "<tr>\n";
			echo "<td style='background-color: #333333; color: #bbbbbb; border-style: solid; border-width: 1px;  text-align: center; padding: 5px;'>Name</td>\n";
			echo "<td style='background-color: #333333; color: #bbbbbb; border-style: solid; border-width: 1px;  text-align: center; padding: 5px;'>Level</td>\n";
			echo "<td style='background-color: #333333; color: #bbbbbb; border-style: solid; border-width: 1px;  text-align: center; padding: 5px;'>Class</td>\n";
			echo "<td style='background-color: #333333; color: #bbbbbb; border-style: solid; border-width: 1px;  text-align: center; padding: 5px;'>Race</td>\n";
			echo "<td style='background-color: #333333; color: #bbbbbb; border-style: solid; border-width: 1px;  text-align: center; padding: 5px;'>Rank</td>\n";
		echo "</tr>\n";
	while($row = mysql_fetch_array($fetch)) {
		echo "<tr>\n";
			echo "<td style='color: #dddddd; border-style: solid; border-width: 1px; padding: 5px;'>" . $row['name'] . "</td>\n";
			echo "<td style='color: #dddddd; border-style: solid; border-width: 1px; padding: 5px;'>" . $row['level'] . "</td>\n";
			echo "<td style='color: #dddddd; border-style: solid; border-width: 1px; padding: 5px;'>" . $row['class'] . "</td>\n";
			echo "<td style='color: #dddddd; border-style: solid; border-width: 1px; padding: 5px;'>" . $row['race'] . "</td>\n";
			echo "<td style='color: #dddddd; border-style: solid; border-width: 1px; padding: 5px;'>" . $row['rank'] . "</td>\n";
		echo "</tr>\n";
	}
	echo "</table>\n";
}

$currentTime = date("mdyHis");
$nextUpdate = mysql_query("SELECT value FROM roster_config WHERE name='nextupdate'");

if ($nextUpdate < $currentTime) {
	mineData();
	procData();
	showData();
} else {
	showData();
}

mysql_close($link);

?>

 

Thanks in advance for the help. The if statement I am having problems with is at the bottom of the script. Depending on which way I flip the "<" or ">" operators I either get it will update all the time, or none of the time.

 

--Corruptblood

Link to comment
https://forums.phpfreaks.com/topic/167080-php-if-else-not-working/
Share on other sites

Sorry for the really late response, if I have echoed the two variables and they are in exactly the same format. If I flip the operator in my if statement it will ALWAYS post the "else" response. If I leave the operator the way it is it will ALWAYS post the "if" response. I just need to figure out a way that it will actually operate as a proper if, else statement.

When I echo them:

 

mdyHis = 080109185912

 

That is the format when I echo any of my dates and store them in the db. stored as VARCHAR (255)

 

Broken Down:

08 01 09 18 59 12 which is essentially: August 01, 2009 18:59:12 (24 hour format)

 

the current update and next update seem to be fine updating and adding the 6 hours.

Is there a reason your not storing them as DATE type in the database?  I suspect the comparison is where you are having the issue as though you believe the variable states a certain time comparing is against another format would cause an issue.

 

http://dev.mysql.com/doc/refman/5.0/en/datetime.html

 

That's where your problem lies - those aren't valid date formats.

 

You should store the date in date format in the database, and either work the comparison into your SQL query, or select the date from the database as a UNIX_TIMESTAMP() and then use time() to get the current time, and compare the dates in PHP using that.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.