Welcome to the Browser-Base Game Zone forums!
<?php/** * information.php - Information class to handle a row array in an easy to use format. * Example: * $player = new Information('stats',$userID); * $opponent = new Information('stats',$opponentID); * $damage = $player->attack - $opponent->defense; * * @copyright Copyright (C) 2009 - ctrlGames, Inc. All rights reserved. * @license http://www.fsf.org/licensing/licenses/agpl-3.0.html */class Information { protected $data = array(); // Record of table row protected $tableName = null; // Table name protected $rowID = null; // Row ID public function __construct($tableName,$rowID){ $this->tableName = $tableName; if(is_numeric($rowID)){ $this->rowID = $rowID; Database::query("SELECT * FROM %s WHERE id = '%s'",$tableName,$rowID); $result = Database::getResults('a'); if(is_array($result)){ $this->data = $result; }else{ throw new Exception("No results from '$tableName' at row ID: $rowID"); } } } /** * @param (string) $var Name of var * @return (mixed) Null if the var does not exist, var's value if it does */ public function __get($var){ if(isset($this->data[$var])){ return $this->data[$var]; } return null; } /** * @param (string) $var Name of var to set * @param (mixed) $val Value of given var * @return void */ public function __set($var,$val){ if(isset($this->data[$var])){ $this->data[$var] = $val; Database::query("UPDATE %s SET %s = '%s' WHERE id = '%s'",$this->tableName,$var,$val,$this->rowID); } } /** * @param (string) $var Name of var to check if it's in $data * @return (bool) */ public function __isset($var){ return isset($this->data[$var]); }}?>
<?phprequire("information.php");$player = new Information('Player_Stats',$_SESSION['userID']);$opponent = new Information('Player_Stats',$opponentID);$damage = ($player->attack - $opponent->defense) * $player->awesomeness;$opponent->hp = $opponent->hp - $damage;if($opponent->hp <= 0){ echo "You killed him!";}else{ echo "Keep going...";}?>
<?php //for colors!! protected $data = array(); // Record of table row protected $tableName = null; // Table name protected $rowID = null;
<?php //for colors!! protected $data = array(), $tableName = null, $rowID = null;
<?php //Only for colors!! list($var1, $var2, $var3, $var4, $var5) = mysql_fetch_array(mysql_query("SELECT `var1`, `var2`, `var3`, `var4`, `var5` FROM `players` WHERE `id` = ". $userid));
that, or...$query = mysql_query( "SELECT field1, field2, field3 FROM table1 WHERE id = $id" );$data = mysql_fetch_object( $query );foreach( $data as $var => $value ){ $this->$var = stripslashes( $value );}Then your class would have all the requested variables as properties.
<?php //for colors!!mysql_fetch_array(); //for $blah['username']'mysql_fetch_assoc(); //for $blah['username'];mysql_fetch_object(); //for $blah->username;?>
$obj->variable
$obj['variable']
A lot of people use array, but for me object is faster to type.For example, for me it's easier to typeCode: [Select]$obj->variablethan it is to typeCode: [Select]$obj['variable']the " [' " and " '] " are clumsy keystrokes for me verses a quick "->"
It doesn't matter which is faster, it matters which leads to better code.
OK I follow you, but I think to simplify:--Crazy was asking, I think, which was faster from a script parsing standpoint (maybe thinking that if something is easier it comes with a speed/memory cost). To that, I have to honestly say I have no idea.--Harkins is basically saying, the difference is negligible and the focus should be put on making sure the rest of your code is lean and optimized.
--Crazy was asking, I think, which was faster from a script parsing standpoint (maybe thinking that if something is easier it comes with a speed/memory cost). To that, I have to honestly say I have no idea.
Until you have an actual measurement it's all just wanking.
Database::query("SELECT * FROM %s WHERE id = '%s'",$tableName,$rowID);$result = Database::getResults('a');
$query = sprintf("SELECT * FROM %s WHERE id = '%s'",$tableName,$rowID);$result = mysql_fetch_assoc(mysql_query($query));
[result] => array ( [field1] => 3 [field2] => something [field3] => 90 )
And I guess my database abstraction layer is confusing but this part:Code: [Select]Database::query("SELECT * FROM %s WHERE id = '%s'",$tableName,$rowID);$result = Database::getResults('a');equals this:Code: [Select]$query = sprintf("SELECT * FROM %s WHERE id = '%s'",$tableName,$rowID);$result = mysql_fetch_assoc(mysql_query($query));which means my table1 with field1, field2, field3 come out in an array like so:Code: [Select][result] => array ( [field1] => 3 [field2] => something [field3] => 90 )Which I thought was understood...
Won't sprintf() in every query, will show the page's down by a whole lot?
Quote from: Crazy-T on June 06, 2009, 02:06:51 AMWon't sprintf() in every query, will show the page's down by a whole lot?The way you do your database query is up to you it doesn't matter. The class itself allows you to pull an array of data from a row in a table using mysql_fetch_assoc() and __get() will allow you to use $instance->variable instead of $instance['variable'].That's the point at least.
alter view vwStats asselect stats_char.id, stats.name, stats.short, stats_char.value as total, stats_base_char.value as base, stats_char.id_charfrom statsjoin stats_char on stats_char.id_stat=stats.idjoin stats_base_char on stats_base_char.id_current=stats_char.idorder by stats.id
"SELECT (what you want) FROM vwStats WHERE id_char=$id"
Basically, I think it means he's a proceedural programmer who's opposed to object-oriented programming because it adds a few milliseconds of processing to achieve.You know, because modular code that solves a specific problem WELL doesn't make up for that difference by being reusable at all. It's another 'negative criticism' post similar to the one here that I kind of went off on.
However, I prefer LEGIBLE code to FAST code. I mean, BINARY is some incredibly fast code! But I sure as hell don't want to maintain it! lol