Note that there are some explanatory texts on larger screens.

plurals
  1. POthe correct way to convert this procedural php to OOP
    primarykey
    data
    text
    <p>I created a personal project to try and learn PHP, it works fine in my hashed together procedural version and to advance my PHP skills I want to convert it to use OOP and also PDO instead of the old mysql connection.</p> <p>I have read a lot about OOP in PHP but I'm struggling with implementing it correctly "in the real world".</p> <p>In my current version I repeat the following code over and over for different years, each year has these two versions for the corresponding fixture and the opposite fixture. So it seems like the ideal chance to implement a class and objects for each year- I think.</p> <pre><code>//deciding if last time they played with the same team at home was &gt;2.5 $sql_samefix_last = mysql_query("SELECT fthg, ftag FROM 2012_2013 WHERE HomeTeam='$home_team' and AwayTeam='$away_team'"); $result_samefix_last = mysql_fetch_array($sql_samefix_last); if (!$result_samefix_last) { $comments[] = "both teams weren't in the league last season, so can't be added to the over or under totals."; $league12_13 = "none"; } if ($result_samefix_last) { $fthg = intval($result_samefix_last['fthg']); $ftag = intval($result_samefix_last['ftag']); $last_goals_total = $fthg + $ftag; $league12_13 = "block"; if ($last_goals_total &gt;= 2.5) { $comments[] = "Last seasons match had more then 2.5 goals"; $over = $over + 3; $under = $under - 3; } elseif ($last_goals_total &lt; 2.5) { $comments[] = "Last seasons match was less than 2.5 goals"; $over = $over - 3; $under = $under + 3; } else { $comments[] = "both teams weren't in the league last season, so the matching fixture can't be added to the over or under totals."; } } //end //deciding if last time they played with opposite away and home &gt;2.5 $sql_samefix_opp = mysql_query("SELECT fthg, ftag FROM 2012_2013 WHERE HomeTeam='$away_team' and AwayTeam='$home_team'"); $result_samefix_opp = mysql_fetch_array($sql_samefix_opp); if (!$result_samefix_opp) { $comments[] = "both teams weren't in the league last season, so can't be added to the over or under totals."; $league12_13_opp = "none"; } if ($result_samefix_opp) { $fthg_opp = intval($result_samefix_opp['fthg']); $ftag_opp = intval($result_samefix_opp['ftag']); $opp_goals_total = $fthg_opp + $ftag_opp; $league11_12_opp = "block"; if ($opp_goals_total &gt;= 2.5) { $comments[] = "Last seasons reverse match had more then 2.5 goals"; $over = $over + 2.5; $under = $under - 2.5; } elseif ($opp_goals_total &lt; 2.5) { $comments[] = "Last seasons revers match was less than 2.5 goals"; $over = $over - 2.5; $under = $under + 2.5; } else { //$comments[] = "both teams weren't in the league last season, so the opposite fixture can't been added to the over or under totals."; } } //end </code></pre> <p>and this is the code from the view page that displays the results of the above code, again this is repeated for each year over and over.</p> <pre><code>&lt;div class="results-box" style="display:&lt;?= $league12_13 ?&gt;;"&gt; &lt;p class="season"&gt;2012 - 2013&lt;/p&gt; &lt;p class="score"&gt;&lt;?= $fthg ?&gt; - &lt;?= $ftag ?&gt;&lt;/p&gt; &lt;p class="score"&gt;&lt;?= $fthg_opp ?&gt; - &lt;?= $ftag_opp ?&gt;&lt;/p&gt; &lt;/div&gt;&lt;!-- /results-box --&gt; </code></pre> <p>This is my attempt at creating a class, my thinking was I create new objects for each year with the $table for the query and the $home_team and $away_team . But Im not sure if Im going in the right direction :-/</p> <pre><code>class fixtureChecker { public $table; public $home_team; public $away_team; public function __construct($table, $home_team, $away_team) { $this-&gt;table = $table; $this-&gt;home_team = $home_team; $this-&gt;away_team = $away_team; } public function doCheck() { try { $con = new PDO( DB_DSN, DB_USERNAME, DB_PASSWORD ); //our new PDO Object $con-&gt;setAttribute( PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION ); // most secure, fires an exception and hides tha data that can be exploited $stmt = $con-&gt;prepare( "SELECT fthg, ftag FROM :table WHERE HomeTeam = :home_team and AwayTeam = :away_team" ); $stmt-&gt;bindParam(':table', $this-&gt;table, PDO::PARAM_STR); $stmt-&gt;bindParam(':home_team', $this-&gt;home_team, PDO::PARAM_STR); $stmt-&gt;bindParam(':away_team', $this-&gt;away_team, PDO::PARAM_STR); $stmt-&gt;execute(); $resultArray = $stmt-&gt;fetchAll(); return $resultArray; } catch (PDOException $e) { $error[] = "I'm sorry there is a problem with your database connection, check the logs.."; //DISPLAYS A FRIENDLY ERROR file_put_contents( 'dbErrors.txt', $e-&gt;getMessage() . " " . date("d-m-Y H:i:s",time()) . PHP_EOL, FILE_APPEND ); //WRITES THE FULL ERROR TO A TEXT FILE } $conn = null; } </code></pre> <p>Any advice on this is great as Im just looking to learn as much as possible about the correct way to code PHP rather than hashing it together until it works, badly.</p> <p>Thanks</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload