OOP newbie

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!

Moderator: General Moderators

Post Reply
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

OOP newbie

Post by Todlerone »

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.

Code: Select all

class DisplayGames{
    public $season;
    public $league;
    public $DisplayType;
    
    function __construct ($season, $league){
        $this->season = mysql_real_escape_string($season);  //edited after reply
        $this->league = mysql_real_escape_string($league);  //edited after reply
    }
    
    function set_season($new_season) {
        $this->season = $new_season;
    }   
    function set_league($new_league) {
        $this->league = $new_league;
    }   
 
    
    function find_GamesScores(){  
        $games=array();
        $query_sch = "SELECT * FROM ".$this->league."_schedule".$this->season." order by game_date";
        $query_sco = "SELECT * FROM ".$this->league."_scores".$this->season;
        
        $schedule_query = mysql_query ($query_sch);
        $scores_query = mysql_query ($query_sco);
                
        if($number_of_results_scores == 0 ){
            return false;
        }
        $result_schedule = result_to_array($schedule_query);
        $result_scores = result_to_array($scores_query);
    
        foreach($result_schedule as $rsch):
            foreach($result_scores as $rsco):
                if ($rsco[0] == $rsch[0]){
                    $games[$rsch[0]][0]=$rsch[0];//game_id
                    $games[$rsch[0]][1]=$rsch[1];//game_date
                    $games[$rsch[0]][2]=$rsch[2];//game_park
                    $games[$rsch[0]][3]=$rsch[3];//game_vis
                    $games[$rsch[0]][4]=$rsch[4];//game_home
                    $games[$rsch[0]][5]=$rsch[5];//game_week
                    $games[$rsch[0]][6]=$rsco[1];//game_vis1 score
                    $games[$rsch[0]][7]=$rsco[2];//game_home1 score
                    $games[$rsch[0]][8]=$rsco[3];//game_vis2 score
                    $games[$rsch[0]][9]=$rsco[4];//game_home2 score
                    $games[$rsch[0]][10]=$rsco[5];//game_status1
                    $games[$rsch[0]][11]=$rsco[6];//game_status2
                }
            endforeach;
        endforeach;
        $this->games = $games;  //edited after reply
        return $this->games;      //edited after reply
    }//end find_GamesScores
    
    function result_to_array($result){
        $res_array = array();
        for ($count=0;  $row = mysql_fetch_array($result); $count++){
            $res_array[$count] = $row;
        }
        return $res_array;
    }
}  // end class
TY
Last edited by Todlerone on Thu Jan 28, 2010 9:00 am, edited 8 times in total.
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: OOP newbie

Post by tr0gd0rr »

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`)
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

TY tr0gd0rr. I use this function to safeguard my queries.

Code: Select all

function safe_output($string){
    $string = trim($string);
    $string = mysql_real_escape_string($string);
    $string = htmlspecialchars($string);
    return $string;
}
Ty for the array info. More questions to come shortly.
Again TY
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

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`)
This ok?

Code: Select all

return $this->games = $games;
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/

TY
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: OOP newbie

Post by Apollo »

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.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

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?

TY Todlerone
User avatar
Apollo
Forum Regular
Posts: 794
Joined: Wed Apr 30, 2008 2:34 am

Re: OOP newbie

Post by Apollo »

Are you sure you need htmlspecialchars on a string that's being inserted to a database? (if yes, why?)

Still, if you're sure you need it, apply it first, and then apply mysql_real_escape_string.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

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;

TY
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

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

My code to start the class is:

Code: Select all

<?php
include_once ("***/class_hmspl.php");
$CG= new DisplayGames("09","hmspl");
print_r($CG->showgames()); //just want to see some results here
?>

Code: Select all

<?php
class DisplayGames{
    public $season;
    public $league;
    public $DisplayType;
    public $allgames;
    function __construct ($season, $league){
        $this->season = mysql_real_escape_string($season);
        $this->league =  mysql_real_escape_string($league);
    }
    
    function set_DisplayType($new_type) {
        $this->DisplayType = $new_type;
    }   
    function set_season($new_season) {
        $this->season = $new_season;
    }   
    function set_league($new_league) {
        $this->league = $new_league;
    }   
 
    function showgames() {
        return $this->find_GamesScores();
    }
    
    function find_GamesScores(){  
        $games=array();
        $schedule_query=array();
        $scores_query=array();
        $result_schedule=array();
        $result_scores=array();
        
        $query_sch = "SELECT * FROM ".$this->league."_schedule".$this->season." order by game_date";
        $query_sco = "SELECT * FROM ".$this->league."_scores".$this->season;
        
        $schedule_query = mysql_query ($query_sch);
        $scores_query = mysql_query ($query_sco);
                
        $result_schedule = result_to_array($schedule_query);
        $result_scores = result_to_array($scores_query);
    
        foreach($result_schedule as $rsch):
            foreach($result_scores as $rsco):
                if ($rsco[0] == $rsch[0]){
                    $games[$rsch[0]][0]=$rsch[0];//game_id
                    $games[$rsch[0]][1]=$rsch[1];//game_date
                    $games[$rsch[0]][2]=$rsch[2];//game_park
                    $games[$rsch[0]][3]=$rsch[3];//game_vis
                    $games[$rsch[0]][4]=$rsch[4];//game_home
                    $games[$rsch[0]][5]=$rsch[5];//game_week
                    $games[$rsch[0]][6]=$rsco[1];//game_vis1 score
                    $games[$rsch[0]][7]=$rsco[2];//game_home1 score
                    $games[$rsch[0]][8]=$rsco[3];//game_vis2 score
                    $games[$rsch[0]][9]=$rsco[4];//game_home2 score
                    $games[$rsch[0]][10]=$rsco[5];//game_status1
                    $games[$rsch[0]][11]=$rsco[6];//game_status2
                }
            endforeach;
        endforeach;
        $this->games = $games;
        return $this->games;
    }//end find_GamesScores
    
    function result_to_array($result){
        $res_array = array();
        for ($count=0;  $row = mysql_fetch_array($result); $count++){
            $res_array[$count] = $row;
        }
        $this->res_array = $res_array;
        return $this->res_array;
    }
}  // end class
?>
TY for any help.
Last edited by Todlerone on Thu Jan 28, 2010 9:01 am, edited 1 time in total.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

bump
User avatar
tr0gd0rr
Forum Contributor
Posts: 305
Joined: Thu May 11, 2006 8:58 pm
Location: Utah, USA

Re: OOP newbie

Post by tr0gd0rr »

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.
Todlerone
Forum Commoner
Posts: 96
Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada

Re: OOP newbie

Post by Todlerone »

TY very much my friend. Cheers to you and all you care about. It now works great. So onto OOP programming.
Post Reply