[PHP]Vote rewards problem

Joined
Oct 6, 2008
Messages
1,394
Reaction score
198
This is my rewards page for vote, that no work, and don't know why

This is the full code
PHP:
<?php

class Rewards
{
	function Title()
	{
		?>
        	Rewards
        <?php
	}

	function GetRealmData()
	{
		//Get data to use in the form.
		$Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
		mysql_select_db(MYSQL_DB,$Con);
		
		$RealmInfo = "{";
		//get each realm
		$res = mysql_query("SELECT id,name,sqlhost,sqluser,sqlpass,chardb FROM realms",$Con);
		echo mysql_error();
		while($Row = mysql_fetch_array($res))
		{
			$RealmInfo .= $Row['id'].":{name:\"".$Row['name']."\"},";
		}
		if(strlen($Realminfo) > 1)
			$RealmInfo = substr($RealmInfo,0,strlen($RealmInfo)-1)."}";
		else
			$RealmInfo .='}';
		mysql_close($Con);
		return $RealmInfo;
	}
	
	function GetCharData()
	{
		//get data that will be used to select the character.
		$Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
		mysql_select_db(MYSQL_DB,$Con);
		
		$CharInfo = "{";
		$Index = 0;
		$res = mysql_query("SELECT id,sqlhost,sqluser,sqlpass,chardb FROM realms");
		while($Row = mysql_fetch_array($res))
		{
			$Con2 = mysql_connect($Row['sqlhost'],$Row['sqluser'],$Row['sqlpass'],true);
			mysql_select_db($Row['chardb'],$Con2);
			$res2 = mysql_query("SELECT id,charname FROM characters WHERE accountname = '{$_SESSION['vcp']['account']}'",$Con2);
			while($Row2 = mysql_fetch_array($res2))
			{
				$CharInfo .= $Index.":{id:".$Row2['id'].",realm:".$Row['id'].",name:\"".$Row2['charname']."\"},";
				$Index++;
			}
			mysql_close($Con2);
		}
		if(strlen($Charinfo) > 1)
			$CharInfo = substr($CharInfo,0,strlen($CharInfo)-1)."}";
		else
			$CharInfo .='}';
		mysql_close($Con);
		return $CharInfo;
	}
	
	function GetRewardData()
	{
		//rewards.. etc.
		$Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
		mysql_select_db(MYSQL_DB,$Con);
		
		$RewardInfo = "{";
		
		$res = mysql_query("SELECT id,realm,name,description,points FROM voterewards");
		while($Row = mysql_fetch_array($res))
		{
			$RewardInfo .= $Row['id'].":{realm:".$Row['realm'].",name:\"".$Row['name']."\",description:\"".$Row['description']."\",cost:".$Row['points']."},";
		}
		if(strlen($Rewardinfo) > 1)
			$RewardInfo = substr($RewardInfo,0,strlen($RewardInfo)-1)."}";
		else
			$RewardInfo .='}';
		$RewardInfo = str_replace("\r\n","<br />",$RewardInfo);
		mysql_close($Con);
		return $RewardInfo;
	}

	function Form()
	{
		?>
			<center><table>
				<tr>
					<td valign="top">
						<table cellspacing="2px">
							<tr valign="top">
								<td colspan="2">Select A Reward</td>
							</tr>
							<tr valign="top">
								<td>nMoney:</td>
								<td><span id="points"><?php echo $_SESSION['vcp']['points']; ?></span></td>
							</tr>
							<tr valign="top">
								<td width="75px">Server:</td>
								<td><select name="realm" id="realm" style="width:150px;" onchange="getReward(); getCharacter();">
				
							</tr>
							<tr valign="top">
								<td>Character:</td>
								<td><select name="character:" id="character" style="width:150px;">

							</tr>
							<tr valign="top">
								<td>Reward:</td>
								<td><select name="Reward:" id="reward" style="width:150px;" onchange="getInfo();">
		
							</tr>
							<tr valign="top">
								<td>Cost:</td>
								<td><span id="cost"></span> nMoney</td>
							</tr>
							<tr valign="top">
								<td colspan="2" align="right"><input id="purchase" type="button" value="Purchase" onclick="onPurchase();" /></td>
							</tr>
						</table>
					</td>
					<td valign="top">
						<div id="description" style="width:200px; height:200px; border:#000000 solid 1px; padding:2px;"></div>
					</td>
				</tr>
			</table>
			<script type="text/javascript">
			
				var Realm = document.getElementById("realm");
				var Character = document.getElementById("character");
				var Reward = document.getElementById("reward");
				var Description = document.getElementById("description");
				var Cost = document.getElementById("cost");
				var Points = document.getElementById("points");
				var Purchase = document.getElementById("purchase");
				
				var Realms = <?php echo $this->GetRealmData(); ?>;
				var Characters = <?php echo $this->GetCharData(); ?>;
				var Rewards = <?php echo $this->GetRewardData(); ?>;
				
				var PointCount = <?php echo $_SESSION['vcp']['points']; ?>;
				
		
				
				function getCharacters()
				{
					var i=0;
					Character.options.length = 0;
					for(var r in Characters)
					{
						if(Characters[r].realm == parseInt(Realm.value))
						{
							Character.options[i] = new Option(Characters[r].name,Characters[r].id);
							i++;
						}
					}
				}
				
				function getRewards()
				{
					
				
					var i=0;
					Reward.options.length = 0;
					for(var r in Rewards)
					{
						if(Rewards[r].realm == parseInt(Realm.value))
						{
							Reward.options[i] = new Option(Rewards[r].name,r);
							i++;
						}
					}
					getInfo();
				}
				
				function getInfo()
				{
					Description.innerHTML = Rewards[Reward.value].description;
					Cost.innerHTML = Rewards[Reward.value].cost;
				}
				
				function onPurchase()
				{
					if(Character.options.length == 0)
					{
						alert("You don't have a character on that realm!");
						return false;
					}
					if(Rewards[Reward.value].cost > PointCount)
					{
						alert("You don't have enough points!");
						return false;
					}
					if(!confirm("Are you sure you wish to spend\r\n"+Rewards[Reward.value].cost+" reward points?"))
						return false;
					Purchase.disabled = true;
					
					var R;
					var Sub = Rewards[Reward.value].cost;
					if(window.XMLHttpRequest)
					{
						R = new XMLHttpRequest();
					}
					else if(window.ActiveX)
					{
						R = new ActiveXObject("Microsoft.XMLHTTP");
					}
					R.onreadystatechange = function()
					{
						if(R.readyState == 4)
						{
							Purchase.disabled = false;
							if(R.responseText != "1")
							{
								alert("Transaction failed:\r\n"+R.responseText);
							}
							else
							{
								PointCount -= Sub;
								Points.innerHTML = PointCount;
							}
						}
					}
					R.open("POST","?act=spend",true);
					var params = "realm="+Realm.value+"&reward="+Reward.value+"&character="+Character.value;
					R.setRequestHeader("Content-type","application/x-www-form-urlencoded");
					R.setRequestHeader("Content-length",params.length);
					R.setRequestHeader("Connection","close");
					R.send(params);
				}
				
				function Initialize()
				{
				
					// Setup realm list, char list, etc.
					var i = 0;
					for(var r in Realms)
					{
						Realm.options[i] = new Option(Realms[r].name,r);
						i++;
					}
					
					i=0;
					for(var r in Characters)
					{
						if(Characters[r].realm == parseInt(Realm.value))
						{
							Character.options[i] = new Option(Characters[r].charname,Characters[r].id);
							i++;
						}
					}
					
					i=0;
					for(var r in Rewards)
					{
						if(Rewards[r].realm == parseInt(Realm.value))
						{
							Reward.options[i] = new Option(Rewards[r].name,r);
							i++;
						}
					}
					getCharacters();
					getRewards();
					getInfo();
				}
				Initialize();
				
			</script>
		<?php

	}

	function Content()
	{
	
		$this->Form();
		
		
		
	}

	function __construct()
	{
		include("html/main.php");
	}

}

?>

This is the part I think it's not working
PHP:
<table>
				<tr>
					<td valign="top">
						<table cellspacing="2px">
							<tr valign="top">
								<td colspan="2">Select A Reward</td>
							</tr>
							<tr valign="top">
								<td>nMoney:</td>
								<td><span id="points"><?php echo $_SESSION['vcp']['points']; ?></span></td>
							</tr>
							<tr valign="top">
								<td width="75px">Server:</td>
								<td><select name="realm" id="realm" style="width:150px;" onchange="getReward(); getCharacter();">
				
							</tr>
							<tr valign="top">
								<td>Character:</td>
								<td><select name="character:" id="character" style="width:150px;">

							</tr>
							<tr valign="top">
								<td>Reward:</td>
								<td><select name="Reward:" id="reward" style="width:150px;" onchange="getInfo();">
		
							</tr>
							<tr valign="top">
								<td>Cost:</td>
								<td><span id="cost"></span> nMoney</td>
							</tr>
							<tr valign="top">
								<td colspan="2" align="right"><input id="purchase" type="button" value="Purchase" onclick="onPurchase();" /></td>
							</tr>
						</table>
					</td>
					<td valign="top">
						<div id="description" style="width:200px; height:200px; border:#000000 solid 1px; padding:2px;"></div>
					</td>
				</tr>
			</table>

The problem is when I try to select server, character and rewards, that I don't get the options. Please help me!:rolleyes:

EDIT: I put the codes, that I think are not working too
 
Last edited:
This is my rewards page for vote, that no work, and don't know why

PHP:
<?php

class Rewards
{
    function Title()
    {
        ?>
            Rewards
        <?php
    }

    function GetRealmData()
    {
        //Get data to use in the form.
        $Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
        mysql_select_db(MYSQL_DB,$Con);
        
        $RealmInfo = "{";
        //get each realm
        $res = mysql_query("SELECT id,name,sqlhost,sqluser,sqlpass,chardb FROM realms",$Con);
        echo mysql_error();
        while($Row = mysql_fetch_array($res))
        {
            $RealmInfo .= $Row['id'].":{name:\"".$Row['name']."\"},";
        }
        if(strlen($Realminfo) > 1)
            $RealmInfo = substr($RealmInfo,0,strlen($RealmInfo)-1)."}";
        else
            $RealmInfo .='}';
        mysql_close($Con);
        return $RealmInfo;
    }
    
    function GetCharData()
    {
        //get data that will be used to select the character.
        $Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
        mysql_select_db(MYSQL_DB,$Con);
        
        $CharInfo = "{";
        $Index = 0;
        $res = mysql_query("SELECT id,sqlhost,sqluser,sqlpass,chardb FROM realms");
        while($Row = mysql_fetch_array($res))
        {
            $Con2 = mysql_connect($Row['sqlhost'],$Row['sqluser'],$Row['sqlpass'],true);
            mysql_select_db($Row['chardb'],$Con2);
            $res2 = mysql_query("SELECT id,charname FROM characters WHERE accountname = '{$_SESSION['vcp']['account']}'",$Con2);
            while($Row2 = mysql_fetch_array($res2))
            {
                $CharInfo .= $Index.":{id:".$Row2['id'].",realm:".$Row['id'].",name:\"".$Row2['charname']."\"},";
                $Index++;
            }
            mysql_close($Con2);
        }
        if(strlen($Charinfo) > 1)
            $CharInfo = substr($CharInfo,0,strlen($CharInfo)-1)."}";
        else
            $CharInfo .='}';
        mysql_close($Con);
        return $CharInfo;
    }
    
    function GetRewardData()
    {
        //rewards.. etc.
        $Con = mysql_connect(MYSQL_HOST,MYSQL_USER,MYSQL_PASS,true);
        mysql_select_db(MYSQL_DB,$Con);
        
        $RewardInfo = "{";
        
        $res = mysql_query("SELECT id,realm,name,description,points FROM voterewards");
        while($Row = mysql_fetch_array($res))
        {
            $RewardInfo .= $Row['id'].":{realm:".$Row['realm'].",name:\"".$Row['name']."\",description:\"".$Row['description']."\",cost:".$Row['points']."},";
        }
        if(strlen($Rewardinfo) > 1)
            $RewardInfo = substr($RewardInfo,0,strlen($RewardInfo)-1)."}";
        else
            $RewardInfo .='}';
        $RewardInfo = str_replace("\r\n","<br />",$RewardInfo);
        mysql_close($Con);
        return $RewardInfo;
    }

    function Form()
    {
        ?>
            <center><table>
                <tr>
                    <td valign="top">
                        <table cellspacing="2px">
                            <tr valign="top">
                                <td colspan="2">Select A Reward</td>
                            </tr>
                            <tr valign="top">
                                <td>nMoney:</td>
                                <td><span id="points"><?php echo $_SESSION['vcp']['points']; ?></span></td>
                            </tr>
                            <tr valign="top">
                                <td width="75px">Server:</td>
                                <td><select name="realm" id="realm" style="width:150px;" onchange="getReward(); getCharacter();">
                
                            </tr>
                            <tr valign="top">
                                <td>Character:</td>
                                <td><select name="character:" id="character" style="width:150px;">

                            </tr>
                            <tr valign="top">
                                <td>Reward:</td>
                                <td><select name="Reward:" id="reward" style="width:150px;" onchange="getInfo();">
        
                            </tr>
                            <tr valign="top">
                                <td>Cost:</td>
                                <td><span id="cost"></span> nMoney</td>
                            </tr>
                            <tr valign="top">
                                <td colspan="2" align="right"><input id="purchase" type="button" value="Purchase" onclick="onPurchase();" /></td>
                            </tr>
                        </table>
                    </td>
                    <td valign="top">
                        <div id="description" style="width:200px; height:200px; border:#000000 solid 1px; padding:2px;"></div>
                    </td>
                </tr>
            </table>
            <script type="text/javascript">
            
                var Realm = document.getElementById("realm");
                var Character = document.getElementById("character");
                var Reward = document.getElementById("reward");
                var Description = document.getElementById("description");
                var Cost = document.getElementById("cost");
                var Points = document.getElementById("points");
                var Purchase = document.getElementById("purchase");
                
                var Realms = <?php echo $this->GetRealmData(); ?>;
                var Characters = <?php echo $this->GetCharData(); ?>;
                var Rewards = <?php echo $this->GetRewardData(); ?>;
                
                var PointCount = <?php echo $_SESSION['vcp']['points']; ?>;
                
        
                
                function getCharacters()
                {
                    var i=0;
                    Character.options.length = 0;
                    for(var r in Characters)
                    {
                        if(Characters[r].realm == parseInt(Realm.value))
                        {
                            Character.options[i] = new Option(Characters[r].name,Characters[r].id);
                            i++;
                        }
                    }
                }
                
                function getRewards()
                {
                    
                
                    var i=0;
                    Reward.options.length = 0;
                    for(var r in Rewards)
                    {
                        if(Rewards[r].realm == parseInt(Realm.value))
                        {
                            Reward.options[i] = new Option(Rewards[r].name,r);
                            i++;
                        }
                    }
                    getInfo();
                }
                
                function getInfo()
                {
                    Description.innerHTML = Rewards[Reward.value].description;
                    Cost.innerHTML = Rewards[Reward.value].cost;
                }
                
                function onPurchase()
                {
                    if(Character.options.length == 0)
                    {
                        alert("You don't have a character on that realm!");
                        return false;
                    }
                    if(Rewards[Reward.value].cost > PointCount)
                    {
                        alert("You don't have enough points!");
                        return false;
                    }
                    if(!confirm("Are you sure you wish to spend\r\n"+Rewards[Reward.value].cost+" reward points?"))
                        return false;
                    Purchase.disabled = true;
                    
                    var R;
                    var Sub = Rewards[Reward.value].cost;
                    if(window.XMLHttpRequest)
                    {
                        R = new XMLHttpRequest();
                    }
                    else if(window.ActiveX)
                    {
                        R = new ActiveXObject("Microsoft.XMLHTTP");
                    }
                    R.onreadystatechange = function()
                    {
                        if(R.readyState == 4)
                        {
                            Purchase.disabled = false;
                            if(R.responseText != "1")
                            {
                                alert("Transaction failed:\r\n"+R.responseText);
                            }
                            else
                            {
                                PointCount -= Sub;
                                Points.innerHTML = PointCount;
                            }
                        }
                    }
                    R.open("POST","?act=spend",true);
                    var params = "realm="+Realm.value+"&reward="+Reward.value+"&character="+Character.value;
                    R.setRequestHeader("Content-type","application/x-www-form-urlencoded");
                    R.setRequestHeader("Content-length",params.length);
                    R.setRequestHeader("Connection","close");
                    R.send(params);
                }
                
                function Initialize()
                {
                
                    // Setup realm list, char list, etc.
                    var i = 0;
                    for(var r in Realms)
                    {
                        Realm.options[i] = new Option(Realms[r].name,r);
                        i++;
                    }
                    
                    i=0;
                    for(var r in Characters)
                    {
                        if(Characters[r].realm == parseInt(Realm.value))
                        {
                            Character.options[i] = new Option(Characters[r].charname,Characters[r].id);
                            i++;
                        }
                    }
                    
                    i=0;
                    for(var r in Rewards)
                    {
                        if(Rewards[r].realm == parseInt(Realm.value))
                        {
                            Reward.options[i] = new Option(Rewards[r].name,r);
                            i++;
                        }
                    }
                    getCharacters();
                    getRewards();
                    getInfo();
                }
                Initialize();
                
            </script>
        <?php

    }

    function Content()
    {
    
        $this->Form();
        
        
        
    }

    function __construct()
    {
        include("html/main.php");
    }

}

?>
The problem is when I try to select server, character and rewards, that I don't get the options. Please help me!:rolleyes:

can you narrow down which function(s) causes the problem?

You're using interesting tactics to say the least ;)
 
Coders Section Rules
5. Don't just dump your problem and hope we sort it out for you!
Coders section is not a general 'I need something fixed so lets ask you guys to fix it for me' forum! If you have a specific problem, for instance a typecast that's giving you trouble or a regex that's not working correctly, feel free to ask. But do not dump here 50 lines of code with the message 'its not working, fix please', or expect us to write your scripts for you!
s-p-n is quite right.
 
Just do it step by step, and then post the part of the code that is not working. And the output error if there is one.
 
I just put all the code, and the part I think it's not working, but I'm not sure.


Okay, we want to help you. I need to know, are the SQL queries loading from the DB, was the javascript working before, or do I have to investigate that as well. Is the OOP a new update which may have caused this.

Well basically, narrow down your code blocks {} and give me something to work with :8:

Edit: Okay, So I did notice something with your form. There's a couple <select> menus that aren't getting closed.

So add </select> tags to close them, otherwise the whole form after that point will get messed up.
 
Still no work, and I add the select here
PHP:
?>
			<center><table>
				<tr>
					<td valign="top">
						<table cellspacing="2px">
							<tr valign="top">
								<td colspan="2">Select A Reward</td>
							</tr>
							<tr valign="top">
								<td>nMoney:</td>
								<td><span id="points"><?php echo $_SESSION['vcp']['points']; ?></span></td>
							</tr>
							<tr valign="top">
								<td width="75px">Server:</td>
								<td><select name="realm" id="realm" style="width:150px;" onchange="getReward(); getCharacter();"></select> //Add </select> here
				
							</tr>
							<tr valign="top">
								<td>Character:</td>
								<td><select name="character:" id="character" style="width:150px;"></select> //Add </select> here

							</tr>
							<tr valign="top">
								<td>Reward:</td>
								<td><select name="Reward:" id="reward" style="width:150px;" onchange="getInfo();"></select> //Add </select> here
		
							</tr>
							<tr valign="top">
								<td>Cost:</td>
								<td><span id="cost"></span> nMoney</td>
							</tr>
							<tr valign="top">
								<td colspan="2" align="right"><input id="purchase" type="button" value="Purchase" onclick="onPurchase();" /></td>
							</tr>
						</table>
					</td>
					<td valign="top">
						<div id="description" style="width:200px; height:200px; border:#000000 solid 1px; padding:2px;"></div>
					</td>
				</tr>
			</table>
 
i dont see the point of using sessions... you use a vote systeem.. and make it this complicated? there are easy'r ways and also safe for this! all the functions arent needed and again... sessions?
 
Well, Caja, it helped the cause regardless of what you may or may not think.

I agree with jonapl.Also, I advise against using OOP simply for the sake of using OOP. It doesn't make you cool, it's actually a useless downfall for most things.

So let me see..

Are you using firefox or IE?

Either way, you should check for JavaScript errors to make sure that's not the problem.

In firefox:
Click Tools -> Error Console

In IE:
The bottom left corner of the browser might have a yellow warning sign. Click that to view the JS error.

If that's not the problem, it's now time to check & make sure the MySQL functions are the problem.

I need you to echo out a statement proving one way or the other.

Let me know your results; Hope that helps. ;)
 
No, I don't use Firefox, or IE, y use custom, but I see the error console, and there is no error.
And the tables are not wrong, I check about 100 times, and nothing.
I put here, maybe are wrong.
Realms table
Code:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for realms
-- ----------------------------
DROP TABLE IF EXISTS `realms`;
CREATE TABLE `realms` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(255) NOT NULL,
  `sqlhost` varchar(255) NOT NULL,
  `sqluser` varchar(255) NOT NULL,
  `sqlpass` varchar(255) NOT NULL,
  `chardb` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB AUTO_INCREMENT=2 DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `realms` VALUES ('1', 'nflyff(realm name)', 'localhost(host)', 'root(mysqluser)', '**passhide**(mysqlpass)', 'pruebas(mysqldb');

Voterewards:
Code:
SET FOREIGN_KEY_CHECKS=0;
-- ----------------------------
-- Table structure for voterewards
-- ----------------------------
DROP TABLE IF EXISTS `voterewards`;
CREATE TABLE `voterewards` (
  `id` int(11) NOT NULL default '0',
  `realm` varchar(255) NOT NULL,
  `itemid` varchar(255) NOT NULL,
  `points` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `cost` varchar(255) NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1;

-- ----------------------------
-- Records 
-- ----------------------------
INSERT INTO `voterewards` VALUES ('1(id)', 'nFlyff(realm name)', '2029(item id)', '100(price points)', 'Bloody knuckle for Bilposter lvl120(description)', '100(price cost)');

P.S: Thanks to all people are helping me with this problem. If anyone want the full vote panel pm me. I just finish all, only I get this error
 
Back