Jump to content

remove column function with php


alex12345

Recommended Posts

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.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.