Jump to content

Super noobie OO help!


sw9

Recommended Posts

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.