• 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!

[Modern AAC] spells list (sorting by vocation)

Same script, but rewrited.
PHP:
<?php
$ots = POT::getInstance(); 
$ots->connect(POT::DB_MYSQL, connection()); 
$SQL = $ots->getDBHandle(); 

$vocations = array('None', 'Sorcerer', 'Druid', 'Paladin', 'Knight', 'Master Sorcerer', 'Elder Druid', 'Royal Paladin', 'Elite Knight'); 

echo  '<FORM ACTION="" METHOD=post> 
<TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=100%> 
<TR BGCOLOR='.$config['site']['vdarkborder'].'><TD CLASS=white><B>Spell Search</B></TD></TR> 
<TR BGCOLOR='.$config['site']['darkborder'].'><TD>Only for vocation: <SELECT NAME="vocation_id">'; 
foreach($vocations as $id => $vocation)
	echo '<option value="'.$id.'">'.$vocation.'</option>';
echo  '</SELECT>&nbsp;&nbsp;&nbsp;<INPUT TYPE=submit NAME="Submit" ALT="Submit" ></TD><TR> 
</TABLE></FORM><TABLE BORDER=0 CELLSPACING=1 CELLPADDING=4 WIDTH=100%><TR BGCOLOR='.$config['site']['vdarkborder'].'><TD CLASS=white><B><font CLASS=white><font color="yellow">Name</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Sentence</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Type<br/>(count)</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Mana</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Exp.<br/>Level</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Magic<br/>Level</font></B></TD><TD CLASS=white><B><font CLASS=white><font color="yellow">Soul</font></B></TD><TD CLASS=white><B><font color="yellow">PACC</font></B></TD><TD CLASS=white><B><font color="yellow">For<br/>Vocations:</font></B></TD></TR>'; 

$spells = $SQL->query('SELECT * FROM z_spells WHERE hide_spell = 0 ORDER BY name');

foreach($spells as $spell)  {
	$spell_vocations = explode(';', $spell['vocations']);
	$vocs = '';
	foreach($spell_vocations as $allowed)
		$vocs .= $vocations[$allowed].'<br />';
		
	if($_POST['vocation_id']) {
		$voc = $_POST['vocation_id'];
		$promoted = $voc > 4 ? $voc - 4 : false;
		
		if(in_array($voc, $spell_vocations) || in_array($promoted, $spell_vocations))
			echo '<tr bgcolor=""><td>'.$spell['name'].'</td><td>'.$spell['spell'].'</td><td>'.$spell['spell_type']. ($spell['conj_count'] ? '('.$spell['conj_count'].')' : '').'</td><td>'.$spell['mana'].'</td><td>'.$spell['lvl'].'</td><td></td><td>'.$spell['soul'].'</td><td>'.($spell['pacc'] ? 'yes' : 'no' ).'</td><td>'.$vocs.'</td></tr>';
	}else
		echo '<tr bgcolor=""><td>'.$spell['name'].'</td><td>'.$spell['spell'].'</td><td>'.$spell['spell_type'].'</td><td>'.$spell['mana'].'</td><td>'.$spell['lvl'].'</td><td></td><td>'.$spell['soul'].'</td><td>'.($spell['pacc'] ? 'yes' : 'no' ).'</td><td>'.$vocs.'</td></tr>';
	
}
echo  '</TABLE>'; 
?>

you must run it first to read spells.xml
PHP:
<?php
$ots = POT::getInstance(); 
$ots->connect(POT::DB_MYSQL, connection()); 
$SQL = $ots->getDBHandle();

$path = 'path/to/spells.xml'; 
$xml = simplexml_load_file($path);

$data = array();
foreach($xml->instant as $item) {
	$data['spell_type'] = 'instant';
	$data['name'] = $item['name'];
	$data['spell'] = $item['words'];
	$data['lvl'] = $item['lvl'];
	$data['mana'] = $item['mana'];
	$data['pacc'] = $item['prem'] ? $item['prem'] : 0;
	$data['vocations'] = '';
	if($item->vocation)
		foreach($item->vocation as $voc)
			$data['vocations'] .= $voc['id'].';';
			
	$data['vocations'] = substr($data['vocations'], 0, -1);
	
	$SQL->query(sprintf('INSERT INTO `%s` (`%s`) VALUES ("%s")', 'z_spells', implode('`, `', array_keys($data)), implode('", "', $data)));
}

$data = array();
foreach($xml->conjure as $item) {
	$data['spell_type'] = 'conjure';
	$data['name'] = $item['name'];
	$data['spell'] = $item['words'];
	$data['lvl'] = $item['lvl'];
	$data['soul'] = $item['soul'];
	$data['conj_count'] = $item['conjureCount'];
	$data['mana'] = $item['mana'];
	$data['pacc'] = $item['prem'] ? $item['prem'] : 0;
	$data['vocations'] = '';
	if($item->vocation)
		foreach($item->vocation as $voc)
			$data['vocations'] .= $voc['id'].';';
			
	$data['vocations'] = substr($data['vocations'], 0, -1);
	
	$SQL->query(sprintf('INSERT INTO `%s` (`%s`) VALUES ("%s")', 'z_spells', implode('`, `', array_keys($data)), implode('", "', $data)));
}?>

For who dont understand this second script, you have to run it one time only, just to add the spells to database, and the first one is to show the spells added to data base by this script.

Tested it and got some errors:

Code:
A PHP Error was encountered

Severity: Warning

Message: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "home/serv/data/spells/spells.xml"

Filename: pages/1.php

Line Number: 7

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: pages/1.php

Line Number: 10

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: pages/1.php

Line Number: 28
 
Dude we are talking about loading time, and its not better you could simply reload the spells.xml to add the new spells to sql table. Update is like once in a year, would it be hard for you to just simply click on a button to reload new spells?
If you're talking about load time and that the file is going to be update maybe once a year, then; Why having a whole table for such easy task? I'd rather make a static file acting 'cache' alike. or if you just dislike having static files as cache then; use memcache. But the simple fact of having a whole table just for showing spells is just plain stupid.

Just look at it this way, lemme explain your method:
Stablishing connection to the SQL server (when in most cases it is located at the same computer, so theres no need for that). --Status: done
Sending a query to the SQL server. --Status: done
SQL server parses the query --Status: done
Start operation 'loading requested data';
hazcache ?
no haz?: load data & cache data --Status: done
else
haz?: get redy to send cached data --Status: done
send data to user (herp which is most of the cases at localhost [I think i'm repeating myself lolz]... herp retarded usage derp derp) --Status: done
PHP receive data. --Status: done
User loop thru the data in order to be able to show such information. --Status: done
...and finally, send the data to the user web browser. --Status: tururu you're done

Kind of stupid just for showing a data that most of the cases doesn't change in months/years, isn't it?

Now look at mine:
file handler/memcached --Status: done
Load all dataxxx (file_handler only:[or load->send in chunks if you have a long list of spells]) --Status: done
Send to the browser. --Status: TURURU you're done

Hell, in case you think this method is a bit 'insecure' (n by insecure I mean, about the unknown possibilities of what is inside that file), you can serialize the parsed XML object and then loop thru it. This way it gives the script more 'understanding' of what you're sending to the user without killing the performance since it is already stored as an Object (with most of the PHP xml parsers it is an array, so it's even faster) inside the mentioned file/cache.

Why complicate everything when there is no need for that? Just like a wise man once said:
"Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius -- and a lot of courage -- to move in the opposite direction."

...Just in case you mind: No, I don't have the intention to help anyone with this post. Only guide you to the right path... or, at least thats what I think, c-ya.

z0mfg, just noticed I wrote a hell long of a post in just a few minutes.
 
But you know that spells.xml would load every time someone goes on that pages. I'm not talking about how bad that is. I'm talking about load time. SQL is much faster to get information from than a .xml file.
We can try it if you want :p
 
But you know that spells.xml would load every time someone goes on that pages. I'm not talking about how bad that is. I'm talking about load time. SQL is much faster to get information from than a .xml file.
We can try it if you want :p
You misunderstood every single part of my post. I never said parsing spells.xml every time the page loaded was faster nor mentioned it as an 'option' for keeping performance & resource usage low. Please, re-read my post. Also, my method is API friendly in case you want to have multiples web pages with that same exact data.
 
You misunderstood every single part of my post. I never said parsing spells.xml every time the page loaded was faster nor mentioned it as an 'option' for keeping performance & resource usage low. Please, re-read my post. Also, my method is API friendly in case you want to have multiples web pages with that same exact data.
Yeah ok, but I was talking about loading time :p
 
Tested it and got some errors:

Code:
A PHP Error was encountered

Severity: Warning

Message: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "home/serv/data/spells/spells.xml"

Filename: pages/1.php

Line Number: 7

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: pages/1.php

Line Number: 10

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: pages/1.php

Line Number: 28

Message: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "home/serv/data/spells/spells.xml"

should be: /home/serv/data/spells/spells.xml
 
Sry, I fixed the db, because I needed it for my website...
I truncated the table, then run the first php and then used the other one to see spells and when choosing between one vocation to another, it didn't work well...
 
something is messy got i get alot of errors, on line 10,20,70, etc.

something with $vocation_id

and config etc. I can see the spells under all the errors but you dont have this you know like "search" button so you can choose a vocation. thats what being bugged with all the errors.

I did exactly as directed
 
no, :x its very difficult sorting xml parsed values of files, so SQL is best option

How is it >HARD< to work with XML, making a cronjob that runs through the xml files every x-days (to update it) and insert the values into the database instead of using this "hardcoded" list of spells?
 
Im getting the errors the first guys got and no one answered.. on line 11, it cant get config for some reason.
 
A PHP Error was encountered

Severity: Notice

Message: Undefined variable: config

Filename: pages/spells.php

Line Number: 10

A PHP Error was encountered

Severity: Notice

Message: Undefined variable: config

Filename: pages/spells.php

Line Number: 11
 
Back
Top