Join 300,333 PHP Programmers for FREE! Get instant access to thousands of PHP experts, tutorials, code snippets, and more! There are 1,687 people online right now. Registration is fast and FREE... Join Now!
if(PRICE > UserMoney) { alert("You cannot afford this pokemon, please choose another or come back at a later time."); return; } if(SelectedBox == ''){ alert("YOU MUST CHOOSE A SLOT TO BUY FOR!"); return; } ConfirmBuy = confirm("You are about to purchase "+NAME+" for slot# "+SelectedBox+", continue?");
Have you noticed that the more code you write, the harder it becomes to keep track of everything? That's why we have functions. They help us reduce the complexity, but even with them, things still get convoluted, which is why we have OOP. Start thinking about using an object:
CODE
<?php class general { var $id; var $keyname= "id"; var $row; public $table = array("newtable","Another_table","A3rdTable","options","parts"); var $dblink; var $servername='localhost'; var $dbusername='root'; var $dbpassword='root'; var $dbname='newtest';
function __construct() { $this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL'); mysql_select_db($this->dbname,$this->dblink) or ('Could not open database '.mysql_error()); }
function read_table($tablenum) { $query = "SELECT * FROM ".$this->table[$tablenum]." where ".$this->keyname." = '".$this->id."'"; $dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error()); $this->row = mysql_fetch_array($dbdata); }
function update_table($tablenum) { $query = "update ".$this->table[$tablenum]." set "; foreach($this->row as $key => $value) { if(!is_numeric($key) && $key != "id") { $query .= $key." = '".$value."', "; } } $query = substr($query,0,-2)." where ".$this->keyname." ='".$this->id."'"; mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error()); }
Have you noticed that the more code you write, the harder it becomes to keep track of everything? That's why we have functions. They help us reduce the complexity, but even with them, things still get convoluted, which is why we have OOP. Start thinking about using an object:
CODE
<?php class general { var $id; var $keyname= "id"; var $row; public $table = array("newtable","Another_table","A3rdTable","options","parts"); var $dblink; var $servername='localhost'; var $dbusername='root'; var $dbpassword='root'; var $dbname='newtest';
function __construct() { $this->dblink = mysql_connect ($this->servername, $this->dbusername,$this->dbpassword) or die('CONNECTION ERROR: Could not connect to MySQL'); mysql_select_db($this->dbname,$this->dblink) or ('Could not open database '.mysql_error()); }
function read_table($tablenum) { $query = "SELECT * FROM ".$this->table[$tablenum]." where ".$this->keyname." = '".$this->id."'"; $dbdata = mysql_query($query,$this->dblink) or die('Error reading from '.$this->table[$tablenum]." ".mysql_error()); $this->row = mysql_fetch_array($dbdata); }
function update_table($tablenum) { $query = "update ".$this->table[$tablenum]." set "; foreach($this->row as $key => $value) { if(!is_numeric($key) && $key != "id") { $query .= $key." = '".$value."', "; } } $query = substr($query,0,-2)." where ".$this->keyname." ='".$this->id."'"; mysql_query($query,$this->dblink) or die("Error updating ".$this->table[$tablenum]." ".mysql_error()); }
It isn't. It's just scary, until you see what's going on. The class is made up of variables and functions. Debugging is easy because you can almost always easily tell which function isn't working right, so you've got a very small amount of code to deal with.
Here's a much simpler class, along with a couple of instances of it. You should be able to make sense of it easily. When you do, reread my general class above to see how it can read/write to a database.
CODE
<?php
class box { var $length; var $width; var $height; var $volume;
It isn't. It's just scary, until you see what's going on. The class is made up of variables and functions. Debugging is easy because you can almost always easily tell which function isn't working right, so you've got a very small amount of code to deal with.
Here's a much simpler class, along with a couple of instances of it. You should be able to make sense of it easily. When you do, reread my general class above to see how it can read/write to a database.
CODE
<?php
class box { var $length; var $width; var $height; var $volume;
No, it's all one class so you'd want to keep it together. You probably don't need all the functions it contains, but if you don't use them, they're just text, so no harm done.
The functions get a little more complicated because of the database. Note that the data is all stored in the class variable: $row, which is an array. ($this->row tells the object that it's dealing with one of its own variables and not a local function variable.)
By the way, the class by itself won't do anything. You need to call it, or generate an instance of it, which is what an object is.
Once you get the hang of OOP you'll realize it makes it 100x easier to keep track of and modify your code.
Things like web based games and such make OOP a necessity, I highly suggest you look into actually using it.
Yours, shane~
ya i have opp or varables in my site all ready lol to store peeps usernames and there pokemon lvl
but i hate using them lol
maybe this is to big to do lol
QUOTE(CTphpnwb @ 27 May, 2009 - 03:07 PM)
No, it's all one class so you'd want to keep it together. You probably don't need all the functions it contains, but if you don't use them, they're just text, so no harm done.
The functions get a little more complicated because of the database. Note that the data is all stored in the class variable: $row, which is an array. ($this->row tells the object that it's dealing with one of its own variables and not a local function variable.)
By the way, the class by itself won't do anything. You need to call it, or generate an instance of it, which is what an object is.
well i think id need a var to get the info from the database for 1 pokemon
cus the user has 6 slots and they got to pick what slot they wanna sell
so i have added a drop don menu on that code on the sell page
so then i need to make a var to get the info for the slot they picked then id have to store that info for that slot and add it to a diffrent table poke_sell so its going from user_pokemon to poke_sell then when its added to poke sell it has to dealte from user_pokemon
then the buy page had to view everything in poke_sell and if somthing is bought it has to give the money to the user who sold it ( i have a username collum )