PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Hello everyone and TY in advance for any help/suggestions. I have alot of code on my baseball site that is function based only. My goal is to advance to the next level of PHP programming, ie, OOP. I have read several books, online tuts, etc, etc. I now want to take "baby steps into the code and have a sample I'd like to post. The class is going to be a basic database search and display what it finds. I later want to fine tune different types of displays. My sample code is the first step in setting up my array of games (and scores if present in the scores db). Just not sure on how to work with arrays within a class (from one method to another). I understand a regular $this-> expression.
Looks like you're getting it... the class is fine for beginning OOP. As far as storing arrays, it is just like storing other variables. For example, at the end of your find_GamesScores() function, you can say `$this->games = $games;`
Just don't forget to use `mysql_real_escape_string` on your query values (e.g. `$this->league`)
tr0gd0rr wrote:Looks like you're getting it... the class is fine for beginning OOP. As far as storing arrays, it is just like storing other variables. For example, at the end of your find_GamesScores() function, you can say `$this->games = $games;`
Just don't forget to use `mysql_real_escape_string` on your query values (e.g. `$this->league`)
My confusion is when to use $this->. Ultimately I only want my games array to be available within the class. In building the array I have one function calling another function and in regular non-OOP coding I understand how this is done, however, within a class do I need to use the $this feature to make the data available between functions or will the $variablename be ok? Not sure if I put my thought across properly/
Todlerone wrote:I use this function to safeguard my queries.
Beware, I'm not exactly sure how you're using this function, but it seems incorrect. What are you outputting to? If SQL, then you don't need the htmlspecialchars (and certainly not after mysql_real_escape_string, as htmlspecialchars may mess up its results). If HTML, then you don't need mysql_real_escape_string.
TY Apollo. I use that function from my admin section. It's used after values have been entered into a scores form. I use this safe guard the sql "insert" into the database. Sorry about the mis-quote above. Em I miss using that function for the insert into db?
Was still just wondering if my call to and from result_to_array has the right format, ie, should I be using $this->scores_query = $scores_query then have return $this->res_array = $res_array;
Again I'm just trying a small class to learn OOP. I'm just trying to understand the jumping around functions/methods in a class with variables/properties. Another little setback. I get a "Fatal error: Call to undefined function result_to_array()" on line 46
Todlerone wrote:I get a "Fatal error: Call to undefined function result_to_array()" on line 46
You need to write `$this->result_to_array();` Any time your `function abc() { }` is within a class, it needs to be called differently than a regular function. In your class, it should always be `->result_to_array()`. (If you created a static method, then it would be `DisplayGames::myStaticMethod()`).
One way to understand `$this` is to think of it the same way as `$CG = new DisplayGames(...);`. The `$this` is just like `$CG` but you always use `$this` inside the class regardless of what you name `$CG`. `$this` is kind of like a suitcase with all the information and abilities of the class and `$CG` is a name of a particular suitcase.