sw9 Posted March 27, 2008 Share Posted March 27, 2008 Hi, I am just starting to learn OO PHP, and I'm struggling with how to deal with arrays. I am sure this code will look kinda ridiculous to many of you, but I am just having a hard time wrapping my head around everything. So for my first project I decided to try to build a very simple table. However, I can't get the class to recognize the array I am trying to put into a function, and I always get this as my output: Warning: Invalid argument supplied for foreach() in /home/retnkes9/public_html/westford-farms.php on line 185 Westford Farm Series I would greatly appreciate some guidance on what I am doing so totally wrong! Here is my code: <?php class Page { public $page; public $cols = array(); function Page(){ $this->page = ''; $this->cols = $array; } function addCaption($desc){ $this->page .= <<<EOD <table class="new" summary="Videos from $desc"> <caption class="new">$desc</caption> EOD; } // This is the function I am having trouble with.... function addCols($cols) { $this->page .= <<<EOD <tr class="new"> EOD; foreach($this->cols as $key=>$value){ $this->page .= <<<EOD <th scope="col" class="new">$value</th> EOD; } $this->page .= <<<EOD </tr> EOD; } function get(){ return $this->page; } } $wf = new Page(); // Add the table caption and rows $wf->addCaption('Westford Farm Series'); $wf->addCols('Episode', 'Production Date', 'Video-On-Demand'); echo $wf->get(); Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/ Share on other sites More sharing options...
BlueSkyIS Posted March 27, 2008 Share Posted March 27, 2008 how i might change things: function addCols($cols) { $this->page .= "<tr class='new'>; foreach($cols as $value){ $this->page .= "<th scope='col' class='new'>$value</th>"; } $this->page .= "</tr>"; } $wf->addCols(array('Episode', 'Production Date', 'Video-On-Demand')); Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/#findComment-502392 Share on other sites More sharing options...
DyslexicDog Posted March 27, 2008 Share Posted March 27, 2008 Are developing in php4 or php5? Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/#findComment-502395 Share on other sites More sharing options...
sw9 Posted March 27, 2008 Author Share Posted March 27, 2008 BlueSky, Thanks; that works great. Much appreciated. DyslexicDog I am on PHP 5; is there something glaringly bad in my code that has to do with 4? Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/#findComment-502402 Share on other sites More sharing options...
DyslexicDog Posted March 27, 2008 Share Posted March 27, 2008 function Page(){ $this->page = ''; $this->cols = $array; } Should be... function __construct(){ $this->page = ''; $this->cols = $array; } Also in that same function you call a variable to doesn't exist anywhere else in your code $this->cols = $array; Can you explain what you are trying to do there? Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/#findComment-502455 Share on other sites More sharing options...
sw9 Posted March 27, 2008 Author Share Posted March 27, 2008 Thanks for the heads up on the __construct(). $cols is an array I am populating when I call the addCols() method from this class...it seems to be working well now, but if I'm making more noobie errors I am happy to learn about them! Link to comment https://forums.phpfreaks.com/topic/98193-super-noobie-oo-help/#findComment-502584 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.