• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

How to use "simplexml_load_file".

MappingFOR

Web Developer
Joined
Jul 12, 2010
Messages
35
Reaction score
0
Hello, today... I will try to write English tutorial. ;P (Never trying before)


simplexml_load_file is a function which we can load XML file to PHP Variable.

Example 1
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');
?>

Nice! We load stages.xml to file, but what that variable have inside?

Example 2
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');
print_r($PHPVariable);
?>

SimpleXMLElement Object ( [world] => SimpleXMLElement Object ( [@attributes] => Array ( [id] => 0 [multiplier] => 1 ) [stage] => Array ( [0] => SimpleXMLElement Object ( [@attributes] => Array ( [minlevel] => 1 [maxlevel] => 5 [multiplier] => 5 ) ) [1] => SimpleXMLElement Object ( [@attributes] => Array ( [minlevel] => 6 [maxlevel] => 15 [multiplier] => 4 ) ) [2] => SimpleXMLElement Object ( [@attributes] => Array ( [minlevel] => 16 [maxlevel] => 35 [multiplier] => 3 ) ) [3] => SimpleXMLElement Object ( [@attributes] => Array ( [minlevel] => 36 [maxlevel] => 49 [multiplier] => 2 ) ) [4] => SimpleXMLElement Object ( [@attributes] => Array ( [minlevel] => 50 [multiplier] => 1 ) ) ) ) )

For begginer - that's the same as "ahdhG[sghgs]fgsh>fg]hfs[fs]", but I can see a new powers :D

Code:
SimpleXMLElement Object
(
 *[world] => SimpleXMLElement Object
  (
    [@attributes] => Array
    (
      [id] => 0
      [multiplier] => 1
    )
    [stage] => Array
    (
      [0] => SimpleXMLElement Object
      (
        [@attributes] => Array
        (
          [minlevel] => 1
          [maxlevel] => 5
          [multiplier] => 5
        )
      )
      [1] => SimpleXMLElement Object
      (
        [@attributes] => Array
        (
          [minlevel] => 6
          [maxlevel] => 15
          [multiplier] => 4
        )
      )
      [2] => SimpleXMLElement Object
      (
        [@attributes] => Array
        (
          [minlevel] => 16
          [maxlevel] => 35
          [multiplier] => 3
        )
      )
      [3] => SimpleXMLElement Object
      (
        [@attributes] => Array
        (
          [minlevel] => 36
          [maxlevel] => 49
          [multiplier] => 2
        )
      )
      [4] => SimpleXMLElement Object
      (
        [@attributes] => Array
        (
          [minlevel] => 50
          [multiplier] => 1
        )
      )
    )
  )
)

You see it now? ;]
But how to show that's tree..

It's easy:
Example 3
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');
print $PHPVariable->world->stage[0][minlevel]; // That's will show me first minimal stage level. ( 1 )
?>

Now, show all values in first stage.

Example 4
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');

$a = 0;
while(isset($PHPVariable->world->stage[$a])){
$stages[$a] = $PHPVariable->world->stage[$a][minlevel].' - '.$PHPVariable->world->stage[$a][maxlevel];
$a++;
}

foreach($stages as $stage){
print $stage.'<br/>';
}
?>

On screen with my DATA:

Code:
1 - 5
6 - 15
16 - 35
36 - 49
50 -

That's stages on my OTS.. but where's rate?

Example 5
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');

$a = 0;
while(isset($PHPVariable->world->stage[$a])){
$stages[$a]['mimx'] = $PHPVariable->world->stage[$a][minlevel].' - '.$PHPVariable->world->stage[$a][maxlevel];
$stages[$a]['rate'] = $PHPVariable->world->stage[$a][multiplier];

$a++;
}

foreach($stages as $stage){
print $stage['mimx'].' ('.$stage['rate'].')<br/>';
}
?>

+ FINAL

Example 6
PHP:
<?php
$PHPVariable = simplexml_load_file('stages.xml');

$a = 0;
while(isset($PHPVariable->world->stage[$a])){

$stages[$a]['mimx'] = $PHPVariable->world->stage[$a][minlevel].''.($PHPVariable->world->stage[$a][maxlevel] != "" ? ' - '.$PHPVariable->world->stage[$a][maxlevel].'' : '+');
$stages[$a]['rate'] = $PHPVariable->world->stage[$a][multiplier];

$a++;
}

foreach($stages as $stage){
print $stage['mimx'].' (x'.$stage['rate'].')<br/>';
}
?>

Code:
1 - 5 (x5)
6 - 15 (x4)
16 - 35 (x3)
36 - 49 (x2)
50+ (x1)


That simple way we can read stage on OTS.
We can read ALL XML FILES!

This is way to create a superprecision serverinfo.php site ;] With actually data.


How to read data from vocations.xml.

Read all name's + desc.
Example 7
PHP:
$PHPVariable = simplexml_load_file('vocations.xml');

$a = 0;
while(isset($PHPVariable->vocation[$a])){

$vocations[$a]['name'] = $PHPVariable->vocation[$a][name];
$vocations[$a]['desc'] = $PHPVariable->vocation[$a][description];

$a++;
}

foreach($vocations as $vocation){
print $vocation['name'].', He is '.$vocation['desc'].'.<br/>';
}

?>

My return.

Code:
Fighter, He is a fighter.
Scout, He is a scout.
Adept, He is a adept.


All vocation data:
Example 8
PHP:
<?php
$PHPVariable = simplexml_load_file('E:/WraithOTS [old]/data/XML/vocations.xml');


$a = 0;
while(isset($PHPVariable->vocation[$a])){

$loads = array(name,description,needpremium,gaincap,gainhp,gainmana,gainhpticks,gainhpamount,gainmanaticks,gainmanaamount,manamultiplier,attackspeed,soulmax,gainsoulticks,lessloss);

foreach($loads as $load){
$vocation[$a]["$load"] = $PHPVariable->vocation[$a][$load];
}

$a++;
}
?>

Now you can load ALL data from vocations...
Code:
$vocation[ID][WhatYouWant];

------------------------------------------------------

Ok.. that's end... sorry for my very bad english. That's my first english tut.

Copyright MappingFOR :D
 

Similar threads

Back
Top