Game Development Community

Problems with retrieving string from php script

by Ashwin · in Torque Game Builder · 06/06/2009 (1:33 am) · 1 replies

Hello everybody

I have created a function for getting high scores from a webserver and that data is being put in different guitextctrls to create a High Score table. However I am having problems with parsing the string with getWord().

the PHP script looks like this

<?php




include ('/home/xx/mysql.php');

$query = "SELECT * FROM userscores ORDER BY score DESC";
$result = mysql_query($query);
//$num = mysql_numrows($result);


for ($loopindex = 0;$loopindex < 10;$loopindex++)
{
	$thisusername = mysql_result($result, $loopindex, 'username');
	$thisscore = mysql_result($result, $loopindex, 'score');
	
	
	

	echo ($thisusername." ".$thisscore." ");
}
exit();
?>

the output in a browser is this:

"ashwin911 44 testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0"


I am using the GET function of tcpObject to get the string and getWord() to put each username and score in the right place.

function TCPGetScore()
{
	%obj = new TCPObject(TCPScoreGetter);
	
	
	%obj.connect("mywebsite.com:80");
	Canvas.setContent(highScore);

}


function TCPScoreGetter::onDNSResolved(%this)
{
}

function TCPScoreGetter::onDNSFailed(%this)
{}


function TCPScoreGetter::onConnected(%this)
{
	%httpCmd="GET /retrievescore.php HTTP/1.1\nHost: www.mywebsite.com:80\nUser-Agent: Torque/1.0 \nAccept: */*"; 
	
	echo(%httpCmd);
	%this.send(%httpCmd@ "\r\n\r\n");

function TCPScoreGetter::onLine(%this, %line)
{
	
	editNamesandScores( );
	echo(%line);
	
}

function editNamesandScores(%this)
{
	$name1=getWord(%line,0);
	userName1.setValue($name1);
	
	%score1=getWord(%line,1);
	userScore1.setValue(%score1);
	/////////////////////////////////////////////
	$name2=getWord(%line,2);
	userName2.setValue($name2);
	
	%score2=getWord(%line,3);
	userScore2.setValue(%score2);
	/////////////////////////////////////////////
	$name3=getWord(%line,4);
	userName3.setValue($name3);
	
	%score3=getWord(%line,5);
	userScore3.setValue(%score3);
	/////////////////////////////////////////////
	$name4=getWord(%line,6);
	userName4.setValue($name4);
	
	%score4=getWord(%line,7);
	userScore4.setValue(%score4);
	/////////////////////////////////////////////	
	$name5=getWord(%line,8);
	userName5.setValue($name5);
	
	%score5=getWord(%line,9);
	userScore5.setValue(%score5);
	/////////////////////////////////////////////
	$name6=getWord(%line,10);
	userName6.setValue($name6);
	
	%score6=getWord(%line,11);
	userScore6.setValue(%score6);
	/////////////////////////////////////////////	
	$name7=getWord(%line,12);
	userName7.setValue($name7);
	
	%score7=getWord(%line,13);
	userScore7.setValue(%score7);
	/////////////////////////////////////////////	
	$name8=getWord(%line,14);
	userName8.setValue($name8);
	
	%score8=getWord(%line,15);
	userScore8.setValue(%score8);
	/////////////////////////////////////////////
	$name9=getWord(%line,16);
	userName9.setValue($name9);
	
	%score9=getWord(%line,17);
	userScore9.setValue(%score9);
	/////////////////////////////////////////////
	$name10=getWord(%line,18);
	userName10.setValue($name10);
	
	%score10=getWord(%line,19);
	userScore10.setValue(%score10);
}	
//__End

Hoewever the echo(%line) output looks like this in the console:
1


d
ashwin911 44 
46
testaccount 0 testaccount 0 testaccount 0 testaccount 0 testaccount 0 
1c
testaccount 0 testaccount 0 
1c
testaccount 0 testaccount 0 
0
                           <--[empty line here]

the usernames and scores should be in one line and since the last line is an empty line the highscore table is just empty.

does anyone knows what I am doing wrong or is there a better way ?

I hope to get a reply soon.

regard

Ashwin

#1
06/07/2009 (1:28 am)
I have fixed it

the php script look like this

<?php




include ('/home/xx/mysql.php');

$query = "SELECT * FROM userscores ORDER BY score DESC";
$result = mysql_query($query);
$num = mysql_numrows($result);


for ($loopindex = 0;$loopindex < 10;$loopindex++)
{
	$thisusername = mysql_result($result, $loopindex, 'username');
	$thisscore = mysql_result($result, $loopindex, 'score');
	
	if ($loopindex==0)
	$line0=($thisusername." ".$thisscore);
	if ($loopindex==1)
	$line1=($thisusername." ".$thisscore);
	if ($loopindex==2)
	$line2=($thisusername." ".$thisscore);
	if ($loopindex==3)
	$line3=($thisusername." ".$thisscore);
	if ($loopindex==4)
	$line4=($thisusername." ".$thisscore);
	if ($loopindex==5)
	$line5=($thisusername." ".$thisscore);
	if ($loopindex==6)
	$line6=($thisusername." ".$thisscore);
	if ($loopindex==7)
	$line7=($thisusername." ".$thisscore);
	if ($loopindex==8)
	$line8=($thisusername." ".$thisscore);
	if ($loopindex==9)
	$line9=($thisusername." ".$thisscore);
	
	
}
echo($line0." ".$line1." ".$line2." ".$line3." ".$line4." ".$line5." ".$line6." ".$line7." ".$line8." ".$line9);
exit();
?>

and the torquescript looks like this
function TCPGetScore()
{
	%obj = new TCPObject(TCPScoreGetter);
	
	
	%obj.connect("whack-A-Robot.com:80");
	Canvas.setContent(highScore);

}


function TCPScoreGetter::onDNSResolved(%this)
{
}

function TCPScoreGetter::onDNSFailed(%this)
{
	
}


function TCPScoreGetter::onConnected(%this)
{

	
	
	%httpCmd="GET /retrievescore.php HTTP/1.1\nHost: www.whack-a-robot.com:80\nUser-Agent: Torque/1.0 \nAccept: */*"; 
	
	echo(%httpCmd);
	%this.send(%httpCmd@ "\r\n\r\n"); 
}

function TCPScoreGetter::onLine(%this, %line)
{
	
	
	if(getWordCount(%line) > 2)
		$highscores=%line;

	$name1=getWord($highscores,0);
	userName1.setValue($name1);
	
	%score1=getWord($highscores,1);
	userScore1.setValue(%score1);
	/////////////////////////////////////////////
	
etc........

	%score10=getWord($highscores,19);
	userScore10.setValue(%score10);
}
but if you know a better way to get high scores please post it