TP-Docs
HTML5 Icon HTML5 Icon HTML5 Icon
TP on Social Media

Recent

Welcome to TinyPortal. Please login or sign up.

Members
  • Total Members: 3,973
  • Latest: NonStop
Stats
  • Total Posts: 196,225
  • Total Topics: 21,352
  • Online today: 633
  • Online ever: 8,223 (February 19, 2025, 04:35:35 AM)
Users Online
  • Users: 1
  • Guests: 381
  • Total: 382
  • lurkalot

Why will this code not work in an article?

Started by insanemustang, October 19, 2006, 06:17:05 AM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

akulion

Quote from: insanemustang on October 19, 2006, 03:31:52 PM
Works as a standalone, but how do I incorporate that into the tp?

i have seen this problem before - but no solution was found for it
dont know what could be causing it

IchBin

Try including the script in a PHP article/block.

include('path/to/your/file.html');

jacortina

Just add "global $stack;" to the top of the block.

I think this is a quirk of separately eval'd code; it's like it's treated as if it's a discrete function. So, to share the data in $stack, you have to $global it.

insanemustang

You guys are good, got it working perfectly.

You can see a block "BF2 Player Stats" on the right side (http://www.loh-clan.net) and then a link below it to go into the article format.

Thanks for everything

D@ve

i too am trying to use this feed from bf2 tracker for my clans website, and what to add it in our new tp site that is in the works. but i am a noob when it comes to all this php and any help would be greatly appreciated. the code iam using worked great as long as your php isnt displaying errors, but since we moved our site to a new host and the php is set to display errors. the page is filled with lots of errors and just cant be used until its fixed. so i was hoping that someone could help me figure out the errors in this code so i may use it for the new tp site. here is the code i currenty use for the page its on <html>
<head>
<title>Specialist Clan BF2 Stats</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>

<body bgcolor="#000000" background="images/grid.jpg" text="#FFFFFF" link="#00CC00" vlink="#009900" alink="#009900">
<div align="center">
<?php
$clanid
="4534";

function
startTag($parser, $name, $attrs) {
global
$stack;

$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($stack,$tag);
}

function
cdata($parser, $cdata) {
global
$stack;

$stack[count($stack)-1]['cdata'] .= $cdata;  
}

function
endTag($parser, $name) {
global
$stack;

$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
array_pop($stack);
}


// Parse XML

$stack = array();
$claninfo = array();
$clanstats = array();
$playerstats = array();

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");

$xmllink="http://bf2tracker.com/livefeed/xml_clanprofile.php?clanid=$clanid";
$data = xml_parse($xml_parser,file_get_contents($xmllink));
if(!
$data) die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));

xml_parser_free($xml_parser);


// Get Data

// Get Clan Profile Data
for($i = 0; $i < sizeof($stack[0][children][0][children]); $i++) {
 
$valname=$stack[0][children][0][children][$i][name];
 
$claninfo[$valname]=$stack[0][children][0][children][$i][cdata];
}

// Get Clan Stats Data
for($i = 0; $i < sizeof($stack[0][children][1][children]); $i++) {
 
$valname=$stack[0][children][1][children][$i][name];
 
$clanstats[$valname]=$stack[0][children][1][children][$i][cdata];
}

// Get Player Data
for($i = 0; $i < sizeof($stack[0][children][2][children]); $i++) {
 for(
$x = 0; $x < sizeof($stack[0][children][2][children][$i][children]); $x++) {
 
$valname=$stack[0][children][2][children][$i][children][$x][name];
 
$value=$stack[0][children][2][children][$i][children][$x][cdata];
  if(
$valname=="PLAYERID") $pid=$value;
 
$playerstats[$pid][$valname]=$value;
 }
}

// Now we have 3 arrays with all stats and infos
// print_r($claninfo);
// print_r($clanstats);
// print_r($playerstats);

// Display Clan Info
echo "<b>Clan Info:</b><br>\n";

foreach(
$claninfo as $key => $value) {
echo
"$key: $value<br>\n";
}


// Display Clan Stats
echo "<br><br><b>Clan Info:</b><br>\n";

foreach(
$clanstats as $key => $value) {
echo
"$key: $value<br>\n";
}

// Display Player Stats
echo "<br><br><b>Player Stats:</b><br>\n";

foreach(
$playerstats as $key => $value) {
$playername=$playerstats[$key][PLAYERNAME];
$playerurl=$playerstats[$key][PLAYERSTATSURL];

if(
$playerstats[$key][PLAYERRANK]!="") $playerrank=$playerstats[$key][PLAYERRANK];
else
$playerrank=0;

if(
$playerstats[$key][PLAYERSTATUS]=="1") $statuspic="ponline.gif";
else
$statuspic="poffline.gif";

echo
"<img border=\"0\" src=\"./images/ranksmall_$playerrank.gif\" width=\"16\" height=\"16\"> <img border=\"0\" src=\"./images/$statuspic\" width=\"42\" height=\"16\"> <a target=\"_blank\" href=\"$playerurl\"><font size=\"2\" color=\"#FFFFFF\">$playername</font></a><br>";
}
?>
<font face="Arial, Helvetica, sans-serif"></font></div>
</body>
</html>

and you can see the errors here http://www.spcclan.com/spc/bf2_clan.php

IchBin

Its because of the <?php and ?> tags. If you can get rid of those and turn the whole code into echo'd HTML, it should work I think.

For example...
Instead of:
?>
<font face="Arial, Helvetica, sans-serif"></font></div>
</body>
</html>


Do this:
echo '
<font face="Arial, Helvetica, sans-serif"></font></div>
</body>
</html>';

D@ve

thanks ichbin, not really the help i was looking for. i figured out how to add as a php article just this code has too many errors and was wondering if anyone might know what is wrong with it. i dont much about any of this I just try try try. but to me seems like its trying to pull to deep from the xml file and their is nothing to pull from? short iam not sure haha

IchBin

If you want help, it might benefit you to post the errors you get instead of making anyone who wants to help take the time to replicate everything your doing.

JPDeni

This works without syntax errors:


$clanid="4534";

function startTag($parser, $name, $attrs) {
global $stack;

$tag=array("name"=>$name,"attrs"=>$attrs);
array_push($stack,$tag);
}

function cdata($parser, $cdata) {
global $stack;

$stack[count($stack)-1]['cdata'] .= $cdata;   
}

function endTag($parser, $name) {
global $stack;

$stack[count($stack)-2]['children'][] = $stack[count($stack)-1];
array_pop($stack);
}


// Parse XML

$stack = array();
$claninfo = array();
$clanstats = array();
$playerstats = array();

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "cdata");

$xmllink="http://bf2tracker.com/livefeed/xml_clanprofile.php?clanid=$clanid";
$data = xml_parse($xml_parser,file_get_contents($xmllink));
if(!$data) die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xml_parser)), xml_get_current_line_number($xml_parser)));

xml_parser_free($xml_parser);


// Get Data

// Get Clan Profile Data
for($i = 0; $i < sizeof($stack[0][children][0][children]); $i++) {
  $valname=$stack[0][children][0][children][$i][name];
  $claninfo[$valname]=$stack[0][children][0][children][$i][cdata];
}

// Get Clan Stats Data
for($i = 0; $i < sizeof($stack[0][children][1][children]); $i++) {
  $valname=$stack[0][children][1][children][$i][name];
  $clanstats[$valname]=$stack[0][children][1][children][$i][cdata];
}

// Get Player Data
for($i = 0; $i < sizeof($stack[0][children][2][children]); $i++) {
  for($x = 0; $x < sizeof($stack[0][children][2][children][$i][children]); $x++) {
   $valname=$stack[0][children][2][children][$i][children][$x][name];
   $value=$stack[0][children][2][children][$i][children][$x][cdata];
   if($valname=="PLAYERID") $pid=$value;
   $playerstats[$pid][$valname]=$value;
  }
}

// Now we have 3 arrays with all stats and infos
// print_r($claninfo);
// print_r($clanstats);
// print_r($playerstats);

// Display Clan Info
echo "<b>Clan Info:</b><br>\n";

foreach($claninfo as $key => $value) {
echo "$key: $value<br>\n";
}


// Display Clan Stats
echo "<br><br><b>Clan Info:</b><br>\n";

foreach($clanstats as $key => $value) {
echo "$key: $value<br>\n";
}

// Display Player Stats
echo "<br><br><b>Player Stats:</b><br>\n";

foreach($playerstats as $key => $value) {
$playername=$playerstats[$key][PLAYERNAME];
$playerurl=$playerstats[$key][PLAYERSTATSURL];

if($playerstats[$key][PLAYERRANK]!="") $playerrank=$playerstats[$key][PLAYERRANK];
else $playerrank=0;

if($playerstats[$key][PLAYERSTATUS]=="1") $statuspic="ponline.gif";
else $statuspic="poffline.gif";

echo '<img border="0" src="./images/ranksmall_' . $playerrank . '.gif" width="16" height="16"> <img border="0" src="./images/' . $statuspic . '" width="42" height="16"> <a target="_blank" href="' . $playerurl . '"><font size="2" color="#FFFFFF">' . $playername . '</font></a><br>';
}


You don't need all of the opening and closing html stuff. That's taken care of through TP. You can't have the <?php and ?> tags because the whole thing goes through an "eval" function. And, I think because of the "eval" thing all of the escaped quotes will cause you problems. Notice the difference between the last real line of the code I posted and the similar line in the one you posted.

I can't guarantee that the actual programming will work, :) but you won't get any syntax errors.

D@ve

Guys thanks for the help so far but i will drop this cause its not tp related and i understand this support is for tp in general and dont want to burn any bridges. just thought their might be a mind out their that new what was wrong with this standalone php page first before i added to the tp site "you guys already provided enough help for adding it". i didnt think i had any problems with it tell we moved our site to a host that will not turn off displaying php errors. which is a good thing really i guess, caught this. the creator of this page told me to just turn off the displaying of errors and move on. no other solution. i just need to learn more about php cause errors is something i dont want, displayed or not. they just lead to problems down the line.

IchBin sorry i didnt post or provide a pic with errors thought my link i added to the actual page would visually show the errors much better and in real time. http://www.spcclan.com/spc/bf2_clan.php

This website is proudly hosted on Crocweb Cloud Website Hosting.