a little thing, giving me a hard time

Need help with an engine or coding not on the list? Need help with a game or the website and forums here? Direct all questions here.
Post Reply
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

a little thing, giving me a hard time

Post by Slav2 »

ok, well like im putting an overview of the city in my game, and like its echo'ing the buildings and thier lvls correctly, but it doesnt want to do the same with the troops. this is what i have with the builidngs that is working:

Code: Select all

<?php
echo "<br/><table><tr><td><font size='5'><b>    Current Village Summery</b></font></td></tr></table><br/>";
?>

<?php
echo "<table border='0' style='border-width: 1px; border-color:#000000; border-style: solid; margin-left: 68;'><tr><td><b><font size='4'>   Building  </font></b></td>" . "<td><b><font size='4'>  Level   </font></b></td></tr>";
?>

<?php


$buildingsinfo=("SELECT * from player_buildings WHERE userid='$uid' AND cityid='$cityview'");
	$buildingsinfo2=mysql_query($buildingsinfo) or die("could not get building levels");
	while($buildingsinfo3=mysql_fetch_array($buildingsinfo2))
{
	if ($buildingsinfo3['buildingtype'] == "townhall"){
		
		$filename = $buildingsinfo3['buildingtype'];
		$buildingid = $buildingsinfo3['buildingid'];
		
			$townhallinfo=mysql_query("SELECT level from buildings WHERE id='$buildingid'") or die("could not get townhall info");
			$level = mysql_fetch_array($townhallinfo);
			
				echo "<tr><td><center>" . $filename . "</center></td><td><center>" . $level['level']. "</center></td></tr>";
			
			}
			
	if ($buildingsinfo3['buildingtype'] == "warehouse"){
		
		$filename = $buildingsinfo3['buildingtype'];
		$buildingid = $buildingsinfo3['buildingid'];
		
			$townhallinfo=mysql_query("SELECT level from buildings WHERE id='$buildingid'") or die("could not get townhall info");
			$level = mysql_fetch_array($townhallinfo);
			
				echo "<tr><td><center>" . $filename . "</center></td><td><center>" . $level['level']. "</center></td></tr>";
			
			}
		
}
echo "</table>";
?>

ok. now im trying to kinda do the same thing for the troops, exept the table is different that for the buildings. in the buildings, every building has its own row, but in the troops, the table is like this:

id userid cityid axemen slinger spearmen archer swordmen scout ranger knight ram catapult
1 7 24 5 17 25 50 30 10 5 3 2 6

and this is how i tried to code this, but its not working:

Code: Select all

<?php
	$troops = mysql_query("SELECT * FROM player_troops WHERE userid='$uid' AND cityid='$cityview'");
	$troops2 = mysql_fetch_array($troops);
		echo $troops2['axemen'];
?>[/code

like i at least need it to display something out of that table before i go ahead and create the whole table. so im trying to echo the axemen that player has, and its not displaying anything....
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: a little thing, giving me a hard time

Post by Jackolantern »

I don't see why it is not working, unless I am looking right over the problem. More than likely, something is causing your SQL query to not pull up any results. As such, the first order of debugging is going to be echo'ing out the userID and cityID right before the SQL query to ensure the values are what you think they are. Once those values are printed out to the screen, you can look them up in phpMyAdmin and make sure they reference the data you want. If that checks out, we can go from there.

EDIT: It is also a good idea to add something like this directly after your SQL query:

Code: Select all

if (!$troops2) {
    echo "An issue has been encountered. No results found from database.";
    exit();
}
The "!$troops" part means "if no results in $troops2, then...".
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: a little thing, giving me a hard time

Post by Slav2 »

agh i gave up on it.....did everything that was possible, but still showing nothing....w/e ill just come back to it after a good nights rest and with sum luck find whats wrong....

btw guys, does can anyone give me any clues as to what might be wrong in this little part of code?

Code: Select all

<?php
include_once 'maintemplate.php';
?>

<?php

if(isset($_GET['tag'])) {$tag = $_GET['tag'];}
if(isset($_GET['type'])) {$type = $_GET['type'];}

$cost = mysql_query("SELECT * FROM buildings WHERE buildingtype='$type' AND level='1'");
$cost2 = mysql_fetch_array($cost);

	if ($cost2)
	{
		$insert="INSERT INTO player_buildings(cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityview', '$uid', '$cost2['id']', '$type', '$tag')";
		$insert2=mysql_query($insert)
	}
	
		if ($insert2)
		{
			$sub="UPDATE resources WHERE id='$cityview' AND userid='$uid'(wood, stone, iron, food, people, gold) VALUES ('$cost2['wood']', '$cost2['stone']', '$cost2['iron']', '$cost2['food']', '$cost2['people']', '$cost2['gold']')";
			$sub2=mysql_query($sub)
		}
		
			if ($sub2)
			{
			
				$findpoints="SELECT points FROM users WHERE id=$uid";
				$findpoints2=mysql_query($findpoints)
				
					$totalpoints = $findpoints2['points'] + $cost2['pluspoints']
				
				$addpoints="UPDATE users WHERE id='$uid'(points) VALUES ('$totalpoints')";
				$addpoints2=mysql_query($addpoints)
			}
	
?>
and the error its giving me is:

Parse error: syntax error, unexpected T_ENCAPSED_AND_WHITESPACE, expecting T_STRING or T_VARIABLE or T_NUM_STRING in /home2/royalemp/public_html/build.php on line 15
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: a little thing, giving me a hard time

Post by Jackolantern »

The problem is in the arrays you are referencing in the SQL statement. You are using single-quotes around each value, but then you are also using them in the indexes of the arrays. This is confusing PHP. I suggest to hold those values in regular variables and then use the regular variables in the query:

Code: Select all

$costid = $cost2['id'];
$insert="INSERT INTO player_buildings(cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityview', '$uid', '$costid', '$type', '$tag')";
That should fix the issue :)
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: a little thing, giving me a hard time

Post by Slav2 »

awesome thanks! ok it fixed that prob, but no clue why its giving me another error.

Code: Select all

<?php
include_once 'maintemplate.php';
?>

<?php

if(isset($_GET['tag'])) {$tag = $_GET['tag'];}
if(isset($_GET['type'])) {$type = $_GET['type'];}

$cost = mysql_query("SELECT * FROM buildings WHERE buildingtype='$type' AND level='1'");
$cost2 = mysql_fetch_array($cost);

	if ($cost2)
	{
		$costid = $cost2['id'];
		$insert="INSERT INTO player_buildings(cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityview', '$uid', '$costid', '$type', '$tag')";
		$insert2=mysql_query($insert)
	}
	
		if ($insert2)
		{
					$wood = $cost2['wood'];
					$stone = $cost2['stone'];
					$iron = $cost2['iron'];
					$food = $cost2['food'];
					$people = $cost2['people'];
					$gold = $cost2['gold'];
			
			$sub="UPDATE resources WHERE id='$cityview' AND userid='$uid'(wood, stone, iron, food, people, gold) VALUES ('$wood', '$stone', '$iron', '$food', '$people', '$gold')";
			$sub2=mysql_query($sub)
		}
		
			if ($sub2)
			{
			
				$findpoints="SELECT points FROM users WHERE id=$uid";
				$findpoints2=mysql_query($findpoints)
				
					$totalpoints = $findpoints2['points'] + $cost2['pluspoints']
				
				$addpoints="UPDATE users WHERE id='$uid'(points) VALUES ('$totalpoints')";
				$addpoints2=mysql_query($addpoints)
			}
	
?>

Parse error: syntax error, unexpected '}' in /home2/royalemp/public_html/build.php on line 18


Like that '}' is supposed to be there.
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: a little thing, giving me a hard time

Post by Jackolantern »

You forgot a semicolon at the end of

Code: Select all

$insert2=mysql_query($insert)
PHP is expecting the semicolon to come before the closing brace.
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: a little thing, giving me a hard time

Post by Slav2 »

oh....wow i definately forgot all about that......thnk you for the help very much! i owe u one! :D
Thanks,

Slav2
User avatar
Jackolantern
Posts: 10891
Joined: Wed Jul 01, 2009 11:00 pm

Re: a little thing, giving me a hard time

Post by Jackolantern »

Most welcome :)
The indelible lord of tl;dr
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: a little thing, giving me a hard time

Post by Slav2 »

Agh!! i have no more errors popping up on the screen, but its not doing anything at all in the db! :(

Code: Select all

<?php
header("Refresh: 2; url='villageview.php");
include_once 'maintemplate.php';
?>

<?php

if(isset($_GET['tag'])) {$tag = $_GET['tag'];}
if(isset($_GET['type'])) {$type = $_GET['type'];}

$cost = mysql_query("SELECT * FROM buildings WHERE buildingtype='$type' AND level='1'");
$cost2 = mysql_fetch_array($cost);

	if ($cost2)
	{
		$costid = $cost2['id'];
		$insert="Update player_buildings WHERE cityid='$cityview' AND userid='$uid' AND tagid='$tag' SET (cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityview', '$uid', '$costid', '$type', '$tag')";
		$insert2=mysql_query($insert);
	}
	
		if ($insert2)
		{
					$wood = $cost2['wood'];
					$stone = $cost2['stone'];
					$iron = $cost2['iron'];
					$food = $cost2['food'];
					$people = $cost2['people'];
					$gold = $cost2['gold'];
			
			$sub="UPDATE resources WHERE id='$cityview' AND userid='$uid'(wood, stone, iron, food, people, gold) VALUES ('$wood', '$stone', '$iron', '$food', '$people', '$gold')";
			$sub2=mysql_query($sub);
		}
		
			if ($sub2)
			{
			
				$findpoints="SELECT points FROM users WHERE id=$uid";
				$findpoints2=mysql_query($findpoints);
				
				$playerspoints = $findpoints2['points'];
				$pointstoadd = $cost2['pluspoints'];
				
					$totalpoints = $playerspoints + $pointstoadd;
				
				$addpoints="UPDATE users WHERE id='$uid'(points) VALUES ('$totalpoints')";
				$addpoints2=mysql_query($addpoints);
			}
	
?>
so firstly i wonder if this is the correct way to set this up:

if ($cost2)
{
$costid = $cost2['id'];
$insert="Update player_buildings WHERE cityid='$cityview' AND userid='$uid' AND tagid='$tag' SET (cityid, userid, buildingid, buildingtype, tagid) VALUES ('$cityview', '$uid', '$costid', '$type', '$tag')";
$insert2=mysql_query($insert);
}

ITS NOT UPDATING THE DB.
Thanks,

Slav2
User avatar
Slav2
Posts: 96
Joined: Tue Jan 11, 2011 7:46 am

Re: a little thing, giving me a hard time

Post by Slav2 »

nvm i just figured it out!!! haha wow. i looked at halls code for urban realms and used that as a guide. this is what i changed it to:

if ($cost2)
{
$costid = $cost2['id'];
$insert="Update player_buildings SET cityid='$cityview', userid='$uid', buildingid='$costid', buildingtype='$type', tagid='$tag' WHERE cityid='$cityview' AND userid='$uid' AND tagid='$tag'";
$insert2=mysql_query($insert);
}
Thanks,

Slav2
Post Reply

Return to “Advanced Help and Support”