Jump to content

php/mysql/xml issues with ordering


thephoneguy87

Recommended Posts

I cant figure out how to order by a set paramater      ie in my script online/offline.

I was told by a wise man (salathe) that i could put all the data into an array and then loop back and output. however i do not know how to do this. Any help would be greatly appreciated.

 

<?php
$hostname='*****';
$username='*****';
$password='*****';
$dbname='*****';
mysql_connect($hostname,$username,$password);
@mysql_select_db($dbname) or die( "Unable to select database");
$query="SELECT * FROM gamertags";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();

$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"first");

$i++;
$xml = simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=".urlencode($f1));

    {
        foreach( $xml->PresenceInfo as $lvl1 )

        {
            echo 'Status: '.$lvl1->Info.'<br />';
		echo 'Online/Offline: '.$lvl1->StatusText.'<br />';
		echo 'Game: '.$lvl1->Title.'<br />';
		echo 'Likes Sex: Yes'.'<br />';
        }
    
    } 

    {
        foreach( $xml->Gamertag as $lvl2 )

        {
            echo 'Gamertag: '.$lvl2.'<br />';
        }
    
    } 

	    {
        foreach( $xml->ProfileUrl as $lvl3 )

        {
            echo 'Page Url: '."<a href=\"$lvl3\">Profile url</a>".'<br />';
        }
    
    } 
{
	echo("<a href=\"http://live.xbox.com/profile/FriendsMgmt.aspx?gt=".urlencode($f1)."&act=Add\">Add as friend</a>".'<br />');
	echo("<a href=\"http://live.xbox.com/profile/MessageCenter/SendMessage.aspx?gt=".urlencode($f1)."\">$Send Message</a>".'<br /><br />');
}
}




?> 

Link to comment
https://forums.phpfreaks.com/topic/199889-phpmysqlxml-issues-with-ordering/
Share on other sites

not quite sure I got this right, but if you want to put the offlines after the onlines, you could just add an if statement in you foreach loop:

foreach( $xml->PresenceInfo as $lvl1 ){

if($lvl1->StatusText == "Online"){
	echo 'Status: '.$lvl1->Info.'<br />';
	echo 'Online/Offline: '.$lvl1->StatusText.'<br />';
	echo 'Game: '.$lvl1->Title.'<br />';
	echo 'Likes Sex: Yes'.'<br />';

} else {
	$arrayForLater[] = $lvl1;

}
}

 

then, after this foreach loop, you just have to go through the offlines:

foreach ($arrayForLater as $id => $lvl1) {

echo 'Status: '.$lvl1->Info.'<br />';
echo 'Online/Offline: '.$lvl1->StatusText.'<br />';
echo 'Game: '.$lvl1->Title.'<br />';
echo 'Likes Sex: Yes'.'<br />';
}

That doesnt work actually. It just doesnt show the data for that one part of it.

 

I recoded the main part a bit maybe that will help. :D

Thanks for trying :D.

 

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Untitled Document</title>
</head>

<body>
<table border="2" cellspacing="2" cellpadding="2">
<tr>
<th><font face="Arial, Helvetica, sans-serif">Tag</font></th>
<th><font face="Arial, Helvetica, sans-serif">Online?</font></th>
<th><font face="Arial, Helvetica, sans-serif">Game</font></th>
<th><font face="Arial, Helvetica, sans-serif">URL</font></th>
<th><font face="Arial, Helvetica, sans-serif">Gamertag</font></th>
<th><font face="Arial, Helvetica, sans-serif">Add</font></th>
<th><font face="Arial, Helvetica, sans-serif">Send MSG</font></th>
</tr>
<?php
$hostname='----';
$username='-----';
$password='-----';
$dbname='------';
mysql_connect($hostname,$username,$password);
@mysql_select_db($dbname) or die( "Unable to select database");
$query="SELECT * FROM gamertags";
$result=mysql_query($query);

$num=mysql_numrows($result);

mysql_close();
$i=0;
while ($i < $num) {

$f1=mysql_result($result,$i,"first");

$i++;
$xml = simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=".urlencode($f1));


    {
        foreach( $xml->PresenceInfo as $lvl1 )

        {
            $status = 'Status: '.$lvl1->Info.'<br />';
		$online = 'Online/Offline: '.$lvl1->StatusText.'<br />';
		$Game = 'Game: '.$lvl1->Title.'<br />';
		$sex = 'Likes Sex: Yes'.'<br />';
        }
    
    } 

    {
        foreach( $xml->Gamertag as $lvl2 )

        {
            $tag = 'Gamertag: '.$lvl2.'<br />';
        }
    
    } 

	    {
        foreach( $xml->ProfileUrl as $lvl3 )

        {
            $url = "<a href=\"$lvl3\">Profile url</a>".'<br />';
        }
    
    } 
{
	$add = ("<a href=\"http://live.xbox.com/profile/FriendsMgmt.aspx?gt=".urlencode($f1)."&act=Add\">Add as friend</a>".'<br />');
	$msg = ("<a href=\"http://live.xbox.com/profile/MessageCenter/SendMessage.aspx?gt=".urlencode($f1)."\">$Send Message</a>".'<br />');
}
// Spot for styled data here 
{
//			echo $status,$online,$Game,$sex,$tag,$url,$add,$msg; ?>


<tr>
<td><font face="Arial, Helvetica, sans-serif"><? echo $status; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $online; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $Game; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $sex; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $tag; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $add; ?></font></td>
<td><font face="Arial, Helvetica, sans-serif"><? echo $msg; ?></font></td>
</tr>
<?
}

}
?>
</table>
</body>
</html>

what output are you getting ?

 

and... on an other matter...

try to avoid this kind of inline styling in your html:

 

<table border="2" cellspacing="2" cellpadding="2">
<th><font face="Arial, Helvetica, sans-serif">Tag</font></th>
[...]

 

do use CSS, inline if you like:

<table style="border:2px solid #000000; margin:2px; padding:2px;">
<th style="font-family:Arial, Helvetica, sans-serif;">Tag</th>
[...]

Oh... just got it...

in fact, it is because you print out everything right away

 

try something like this:

while ($i < $num) {

$f1=mysql_result($result,$i,"first");

$i++;
$xml[] = simplexml_load_file("http://xboxapi.duncanmackenzie.net/gamertag.ashx?GamerTag=".urlencode($f1));
}

 

then, after that, do a foreach loop on $xml and then you'll be able to put them aside as I explained above

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.