alex12345 Posted April 12, 2014 Share Posted April 12, 2014 Hi, I am new to PHP programming and trying to learn it. I came across one particular exercise on the web and tried to code it. I have created a table with php and i am trying to add rows, delete rows , add columns and delete columns. I have managed to write functions for add rows and remove rows, and add columns, but i am unable to make my function for remove column to work. Can you please help me with is function? <html> <head> <title>object method example</title> </head> <body> <?php class Table { var $table_array=array(); var $headers=array(); var $cols; function Table ($headers) { $this->headers=$headers; $this->cols=count($headers); } function addRow ($row) { if(count($row)!=$this->cols) return false; array_push($this->table_array,$row); return true; } function addRowAssocArray($row_assoc) { $row=array(); foreach($this->headers as $header) { if(!isset($row_assoc[$header])) $row_assoc[$header]=""; $row[]=$row_assoc[$header]; } array_push($this->table_array,$row); return true; } function output() { print "<pre>"; foreach($this->headers as $header) print "<b>$header</b> "; print "\n"; foreach($this->table_array as $y) { foreach($y as $xcell) print "$xcell "; print "\n"; } print "</pre>"; } function rmRow($rm_row) { $j=0; if(count($rm_row)!=$this->cols) return false; foreach($this->table_array as $x) { $i=0; $count=0; foreach($x as $value) { if($rm_row[$i]==$value) $count++; if($count==3) { unset($this->table_array[$j]); return true; } $i++; } $j++; } return false; } function rmRowAssocArray($rm_assoc_array) { $j=0; foreach($this->table_array as $x) { $row_temp=array(); $i=0; $count=0; foreach($x as $value) { $row_temp[]=$value; } foreach($this->headers as $header) { if(!isset($row_assoc[$header])) $row_assoc[$header]=""; if($row_temp[$i]==$rm_assoc_array[$header]) $count++; if($count==3) { unset($this->table_array[$j]); return true; } $i++; } $j++; } return false; } function addCol($col,$val) { $j=0; $this->headers[]=$col; foreach($this->table_array as $x) { $row_temp=array(); foreach($x as $value) { $row_temp[]=$value; } $row_temp[]=$val; $this->table_array[$j]=$row_temp; $j++; } } function rmCol($col) { if(count($col)!=$this->cols) return false; $j=0; if($col!=$this->headers) return false; foreach($this->table_array as $x) { $count=0; foreach($x as $value) { $count++; if($count==3) { unset($this->table_array[$j]); return true; } } $j++; } return false; } } $test = new table (array("a","b","c")); $test->addRow (array(1,2,3)); $test->addRow (array(5,6,7)); $test->addRowAssocArray(array('b'=>0,'a'=>6,'c'=>3)); $test->output(); //$test->addCol('d', ""); if($test->rmRow(array(1,2,3))) //if($test->rmRowAssocArray(array('c'=>7,'a'=>5,'b'=>6))) { print "After removing the row with rmRow function"; print "\n"; $test->output(); } else print "unable to remove"; /*print "\n"; print "After adding a column"; print "\n"; $test->output();*/ ?> </body> </html> The rmCol needs to take the parameter $col as the header name and delete the whole row from the table. Quote Link to comment https://forums.phpfreaks.com/topic/287721-remove-column-function-with-php/ Share on other sites More sharing options...
trq Posted April 12, 2014 Share Posted April 12, 2014 I'm not sure where your learning php from, but that example is not a good one. Can I suggest you start here: http://www.phptherightway.com ? Quote Link to comment https://forums.phpfreaks.com/topic/287721-remove-column-function-with-php/#findComment-1475911 Share on other sites More sharing options...
alex12345 Posted April 12, 2014 Author Share Posted April 12, 2014 Thank you for the suggestion.. I will go through the website. But since i have already got into this.. Can you please help me with this function ? Quote Link to comment https://forums.phpfreaks.com/topic/287721-remove-column-function-with-php/#findComment-1475912 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.