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
Todlerone
Forum Commoner
Posts: 96 Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada
Post
by Todlerone » Fri Jan 29, 2010 9:32 am
Hello all again. Quick question. I have a class that populates an array of baseball games and scores (works great). I have extended this class into another class that will allow me to select different ways to display this array of information, ie, without scores, with scores, certain teams only, etc.etc... I get this error:
Warning: Invalid argument supplied for foreach() in /.../TCode/class_hmspl.php on line 90
Code: Select all
foreach($this->games as $g){
$this->date = $g[1];
$this->Weekday = date('D', strtotime($date));
$this->MonthNum = date ('n', strtotime($date));
$this->Dayofmonth = date ('d', strtotime($date));
//...more code under
The games array is populated in the first part of the class. Any help/suggestions would be appreaciated.
Last edited by
Todlerone on Fri Jan 29, 2010 12:39 pm, edited 1 time in total.
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Fri Jan 29, 2010 9:37 am
You'll have to show more code because $this->games is not an array.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Todlerone
Forum Commoner
Posts: 96 Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada
Post
by Todlerone » Fri Jan 29, 2010 9:40 am
This is the method from the first part of the class that populates the array. TY for your quick response.
Code: Select all
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);
$result_schedule = $this->result_to_array($schedule_query);
$result_scores = $this->result_to_array($scores_query);
foreach($result_schedule as $rsch){
$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
}
foreach($result_scores as $rsco){
if ($rsco[0] == $games[$rsco[0]][0]){
$games[$rsco[0]][6]=$rsco[1];//game_vis1 score
$games[$rsco[0]][7]=$rsco[2];//game_home1 score
$games[$rsco[0]][8]=$rsco[3];//game_vis2 score
$games[$rsco[0]][9]=$rsco[4];//game_home2 score
$games[$rsco[0]][10]=$rsco[5];//game_status1
$games[$rsco[0]][11]=$rsco[6];//game_status2
}
}
return $this->games = $games;
}//end find_GamesScores
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Fri Jan 29, 2010 9:44 am
There is no $this->games to return. There is only $games inside the function. You either need to return $games or set $this->games = $games. It's hard to tell without seeing how you are using all of this.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Fri Jan 29, 2010 9:53 am
AbraCadaver wrote: There is no $this->games to return. There is only $games inside the function. You either need to return $games or set $this->games = $games. It's hard to tell without seeing how you are using all of this.
Never mind, I see you are returning $this->games = $games. Didn't see that at first. Show the code where you are using the foreach, actually all code would help.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Todlerone
Forum Commoner
Posts: 96 Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada
Post
by Todlerone » Fri Jan 29, 2010 10:17 am
TY again.
Here is my calling code
Code: Select all
<?php
include_once ("TCode/class_hmspl.php");
$CG= new GetGamesScores("09","hmspl");
$G= new DisplayGames("scores");
$G->displaythem();
?>
This is the rest of it. The code itself will be fine tuned later. At this point I'm trying to wrap my head around classes and how variables/properties work between the methods and extended classes.
Code: Select all
<?php
class GetGamesScores{
public $season;
public $league;
function __construct ($season, $league){
$this->season = mysql_real_escape_string($season);
$this->league = mysql_real_escape_string($league);
}
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();
$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 = $this->result_to_array($schedule_query);
$result_scores = $this->result_to_array($scores_query);
foreach($result_schedule as $rsch){
$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
}
foreach($result_scores as $rsco){
if ($rsco[0] == $games[$rsco[0]][0]){
$games[$rsco[0]][6]=$rsco[1];//game_vis1 score
$games[$rsco[0]][7]=$rsco[2];//game_home1 score
$games[$rsco[0]][8]=$rsco[3];//game_vis2 score
$games[$rsco[0]][9]=$rsco[4];//game_home2 score
$games[$rsco[0]][10]=$rsco[5];//game_status1
$games[$rsco[0]][11]=$rsco[6];//game_status2
}
}
return $this->games = $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;
}
return $this->res_array = $res_array;
}
} // end class
// *********************************************************
class DisplayGames extends GetGamesScores{
function __construct ($display){
$this->display = mysql_real_escape_string($display);
$this->seasonstart = mktime( 0, 0, 0, 5, 10, 2009 )/86400;
$this->now = mktime(0, 0, 0, date("m") , date("d"), date("Y"))/86400;
$this->remainder= ((abs($this->now - $this->seasonstart)/7)- (floor(abs($this->now - $this->seasonstart)/7)));
if ($this->remainder ==0){
$this->weeknow = abs($this->now - $this->seasonstart)/7+1;
}elseif ($this->remainder >0.7){
$this->weeknow = floor(ceil(abs($this->now - $this->seasonstart)/7)+1.4);
}else {
$this->weeknow = ceil(abs($this->now - $this->seasonstart)/7);
}
$monHeaderColor = array(
"May" => "ADD8E6",
"June" => "999933",
"July" => "FF9933",
"August" => "66CC99",
"September" => "CCCC66");
$this->MonthNumInit==0;
}
function displaythem(){
$this->games = $games;
echo "<div id=\"results\">";
echo "<table border=\"0\" cellpadding=\"2\" cellspacing=\"0\">";
foreach($this->games as $g){
$this->date = $g[1];
$this->Weekday = date('D', strtotime($date));
$this->MonthNum = date ('n', strtotime($date));
$this->Dayofmonth = date ('d', strtotime($date));
$this->Monthheader = date('F', strtotime ($date));
$this->Location = $g[2];
$this->Visitor = $g[3];
$this->Home = $g[4];
$this->Week = $g[5];
$this->vis1=$g[6];
$this->home1=$g[7];
$this->vis2=$g[8];
$this->home2=$g[9];
$this->status1=$g[10];
$this->status2=$g[11];
if ($this->MonthNum != $this->MonthNumInit){
echo "<tr><td colspan=\"6\" bgcolor=\"#". $monHeaderColor[$this->Monthheader] ."\"> <strong>$this->Monthheader</strong></td></tr>";
$this->MonthNumInit = $this->MonthNum;
}
switch ($this->display){
case "schedule":
$this->dayofweek();
$this->showteams();
break;
case "nextweek":
$this->dayofweek();
$this->showteams();
break;
case "teams":
if ($this->Home != "OPN"){
$this->dayofweek();
$this->showteams();
$this->scores();
}
break;
case "thisweek":
$this->today = date('Y-m-d');
if ($this->Week==$this->weeknow){
if ($this->date < $this->today && $this->Home=="OPN"){
//do nothing
}else{
$this->dayofweek();
$this->showteams();
$this->scores();
}
}
break;
case "scores":
if ($this->Home != "OPN"){
$this->dayofweek();
$this->showteams();
$this->scores();
}
break;
}
} //($this->games as $g)
echo '</table>';
echo "</div>";
}
function set_display ($new_display) {
$this->display = $new_display;
}
function dayofweek(){
echo "<tr><td width=\"30\" align=\"right\">$this->Weekday</td>";
if($this->Monthheader == "May"){
echo "<td width=\"20\" align=\"center\" bgcolor=\"#ADD8E6\"><strong>$this->Dayofmonth</strong></td>";
}elseif ($this->Monthheader == "June") {
echo "<td width=\"20\" align=\"center\" bgcolor=\"#999933\"><strong>$this->Dayofmonth</strong></td>";
}elseif ($this->Monthheader == "July") {
echo "<td width=\"20\" align=\"center\" bgcolor=\"#FF9933\"><strong>$this->Dayofmonth</strong></td>";
}elseif ($this->Monthheader == "August") {
echo "<td width=\"20\" align=\"center\" bgcolor=\"#66CC99\"><strong>$this->Dayofmonth</strong></td>";
}elseif ($this->Monthheader == "September") {
echo "<td width=\"20\" align=\"center\" bgcolor=\"#CCCC66\"><strong>$this->Dayofmonth</strong></td>";
}
echo "<td width=\"35\" align=\"center\">$this->Location</td>";
}
function showteams(){
if ($this->Home and $this->Visitor == "OPN"){
echo "<td colspan=\"3\" align=\"center\" bgcolor=\"#66CCFF\">Open Date</td></tr>";
}else{
echo "<td width=\"30\" align=\"right\">$this->Visitor</td><td> @ </td>";
echo "<td width=\"30\" align=\"left\">$this->Home</td></tr>";
}
}
function scores(){
if (isset ($this->status1)){
if ($this->status1=="C"){ // for completed game-1 score
if ($this->vis1>$this->home1){ //vis1>home1
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td align=\"right\"><strong>$this->vis1</strong></td><td>@</td><td>$this->home1</td></tr>";
}elseif ($this->vis1<$this->home1){ //vis1<home1
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td align=\"right\">$this->vis1</td><td>@</td><td><strong>$this->home1</strong></td></tr>";
} else {
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td align=\"right\">$this->vis1</td><td>@</td><td>$this->home1</td></tr>";
}
}elseif ($this->status1=="FV1"){
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td align=\"right\">(F)$this->vis1</td><td>@</td><td><strong>$this->home1</strong></td></tr>";
}elseif ($this->status1=="FH1"){
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td align=\"right\"><strong>$this->vis1</strong></td><td>@</td><td>$this->home1(F)</td></tr>";
}elseif ($this->status1=="R1"){
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td colspan=\"3\" align=\"center\"><strong>Rain-Out</strong></td></tr>";
}elseif ($this->status1=="CANC"){
echo "<tr><td colspan=\"3\" align=\"right\">1)</td><td colspan=\"3\" align=\"center\"><strong>Cancelled</strong></td></tr>";
}
// game-2
if ($this->status2=="C"){ // for completed game-2 score $g[6]=vis2...$g[7]=home2
if ($this->home2>$this->vis2){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td align=\"right\"><strong>$this->home2</strong></td><td>@</td><td>$this->vis2</td></tr>";
}elseif ($this->home2<$this->vis2){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td align=\"right\">$this->home2</td><td>@</td><td><strong>$this->vis2</strong></td></tr>";
} else {
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td align=\"right\">$this->home2</td><td>@</td><td>$this->vis2</td></tr>";
}
}elseif ($this->status2=="FV2"){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td align=\"right\"><strong>$this->home2</strong></td><td>@</td><td>$this->vis2(F)</td></tr>";
}elseif ($this->status2=="FH2"){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td align=\"right\">(F)$this->home2</td><td>@</td><td><strong>$this->vis2</strong></td></tr>";
}elseif ($this->status2=="R2"){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td colspan=\"3\" align=\"center\"><strong>Rain-Out</strong></td></tr>";
}elseif ($this->status2=="D2"){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td colspan=\"3\" align=\"center\"><strong>Darkness</strong></td></tr>";
}elseif ($this->status2=="CANC"){
echo "<tr><td colspan=\"3\" align=\"right\">2)</td><td colspan=\"3\" align=\"center\"><strong>Cancelled</strong></td></tr>";
}
}else{
if ($this->date < $this->today){
echo "<tr><td colspan=\"3\" align=\"right\"> </td><td colspan=\"3\" align=\"center\"><font color=\"red\"><strong>Need Scores</strong></font></td></tr>";
}
}
}// end function scores
} // class DisplayGames extends GetGamesScores
?>
Todlerone
Forum Commoner
Posts: 96 Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada
Post
by Todlerone » Fri Jan 29, 2010 11:10 am
How do you access an array variable in the child class from the parent class?
AbraCadaver
DevNet Master
Posts: 2572 Joined: Mon Feb 24, 2003 10:12 am
Location: The Republic of Texas
Contact:
Post
by AbraCadaver » Fri Jan 29, 2010 12:51 pm
There are a lot of problems here.
1. I don't see a reason in this use to have a base class and a child class
2. You create an object for the base class and then a separate object for the child class which then already includes the base class
3. Nowhere in your first block of code have you called find_GamesScores() so $games is never set
I would start building one class. Once it is working then try and refactor and see if it makes sense to build a base class and child classes.
mysql_function(): WARNING : This extension is deprecated as of PHP 5.5.0, and will be removed in the future. Instead, the MySQLi or PDO_MySQLextension should be used. See also MySQL: choosing an API guide and related FAQ for more information.
Todlerone
Forum Commoner
Posts: 96 Joined: Sun Oct 28, 2007 10:20 pm
Location: Hamilton, Ontario, Canada
Post
by Todlerone » Wed Feb 10, 2010 5:37 pm
Great..TY for all the replies. I was able to get the class to work and I understand how to use them. WOW I love classes. With a basic understanding of classes where or what should I try to learn next. I know this may be a vague question but any suggestions on increasing my coding ability would be appreciated.
CHEERS