Welcome to the Browser-Base Game Zone forums!
<?//WEAPONS -NAME / MIN DMG / MAX DMG$weapon[1] = array("Spear",10,15);$weapon[2] = array("Axe",15,20);$weapon[3] = array("Hammer",20,25);//....?>
<?//we include file with weaponsinclude("weapons.php");//example is to show list of all weapons possible with attack ratesfor ($j=1; $j<=3; $j++) {echo $weapon[$j][0]." (".$weapon[$j][1]." - ".$weapon[$j][2].") <br>";}?>
$weapon['Sword']['dmg'] = 5;$weapon['Sword']['cost'] = 10;$weapon['Axe']['dmg'] = 10;$weapon['Axe']['cost'] = 20;
$weapon[] = array( 'name'=>'Sword', 'dmg'=>5, 'cost'=>10 );$weapon[] = array( 'name'=>'Axe', 'dmg'=>10, 'cost'=>20 );
In this article I will try to explain how to write static data into arrays instead of database tables. Doing that you will not need additional queries to select weapons,units, buildings description and/or statistics.
$rs = $adodb->("SELECT username, weaponid FROM players ORDER BY username");while( $row = $rs->FetchRow() ) { echo $row['username'], " has weapon ", $weapons[$row['weaponid']]['name'], "<br />";}
$rs = $adodb->("SELECT username, weaponlu.name FROM players, weaponlu WHERE players.weaponid = weaponlu.name ORDER BY username");while( $row = $rs->FetchRow() ) { echo $row['username'], " has weapon ",$row['name'], "<br />";}
$rs = $adodb->("SELECT CONCAT(username, ' has weapon ', weaponlu.name, '<br />') as lineout FROM players, weaponlu WHERE players.weaponid = weaponlu.name ORDER BY username");while( $row = $rs->FetchRow() ) { echo $row['lineout'];}
OK i made some tests. I selected all units from table units (14 entries), there every unit have building and technology that needs to be built/researched to be able to build units. Buildings table has 22 entries and technology has 30.
SELECT m.id,m.owner,m.city,m.slikca,u.username,c.name FROM map m LEFT JOIN users u ON u.id = m.owner LEFT JOIN city c ON c.user_id = m.owner WHERE m.id BETWEEN $id1s AND $id1e OR m.id BETWEEN $id2s AND $id2e OR m.id BETWEEN $id3s AND $id3e OR m.id BETWEEN $id4s AND $id4e OR m.id BETWEEN $id5s AND $id5e OR m.id BETWEEN $id6s AND $id6e OR m.id BETWEEN $id7s AND $id7e OR m.id BETWEEN $id8s AND $id8e OR m.id BETWEEN $id9s AND $id9e OR m.id BETWEEN $id10s AND $id10e OR m.id BETWEEN $id11s AND $id11e
mysql> select count(*) from mobiles;+----------+| count(*) |+----------+| 13554 | +----------+1 row in set (1.22 sec)mysql> select count(*) from mobiles;+----------+| count(*) |+----------+| 13554 | +----------+1 row in set (0.00 sec)
If this was C then arrays would won. We keep forgetting PHP is an interpreted language (at least I keep forgetting where ever line of code is very slow no matter how we write it (maybe Zend Optimizer or IonCube would speed it up, but if I remember correcly these cost $1000, so it is out of the question for small/mid size games anyway).
No it would not be faster in C, nor C++, nor Assembly language or any other language you can think of. Look at what Saljutin's test showed. When comparing only the loops it was equal in task, however, what does take time is allocating the memory to store the array. No matter what language you chose it still takes time to allocate memory, period. So one for one mixing database and array storage is slower then just putting it into the database. With apps written in C etc you have an application memory space where you can hold that memory until the app is finished executing (unlike PHP where it's allocated on every page hit).
No it would not be faster in C, nor C++, nor Assembly language or any other language you can think of. Look at what Saljutin's test showed. When comparing only the loops it was equal in task, however, what does take time is allocating the memory to store the array. No matter what language you chose it still takes time to allocate memory, period. So one for one mixing database and array storage is slower then just putting it into the database.
SQL was biggest and baddest performance bottleneck of PHP web programming.