Rajesh
Rajesh

Reputation: 195

How to call a function in another function with arguments - PHP class

The following code works fine. In the class file, there are four arguments for the function setRowCol($r,$c, $v, $pa) in which $pa is an argument for padding which is passed inside the function startCol($p). But it doesn't work when i set padding like the following

public function startCol($p) {
            $tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";                
            return $tab;
        }

and works fine if I give the value directly like

$tab= "<td Style='border:4px solid black; padding:110px;>";             

But I would like to keep padding value as an argument. Is there a solution for this?

Full Class File

class CreateTable {

    public $rows;
    public $cols;
    public $val = array();
    public $pad;

    public function setRowCol($r,$c, $v, $pa) {
        $this->rows = $r;
        $this->cols = $c;
        $this->val = $v;
        $this->pad = $pa;       
        echo $this->startTable();
        for($i=0; $i<$this->rows && $i<sizeof($this->val); $i++) {
            echo $this->startRow();         
            for($j=0; $j<$this->cols && $j<sizeof($this->val); $j++) {
                echo $this->startCol($this->pad);           
                echo $this->val[$j];
                echo $this->endCol();           
        }
        echo $this->endRow();
        }
        echo $this->endTable();
    }

    function startTable() {
        $tab = "<table Style='border:1px solid black';>";               
        return $tab;
    }

    function endTable() {       
        $tab= "</table>";
        return $tab;
    }

    function startRow() {       
        $tab= "<tr Style='border:1px solid black';>";               
        return $tab;
    }

    function endRow() {             
        $tab= "</tr>";      
        return $tab;
    }

    public function startCol($p) {                          
        $tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";                
        return $tab;
    }
    function endCol() {                         
        $tab= "</td>";      
        return $tab;
    }
}
?>

File

<?php

include "CreateTable.php";

$arr = array("Jan", "feb","mar", "apr");

$tab = new CreateTable();
echo $tab->setRowCol(3,10, $arr, 110);

?>

Upvotes: 0

Views: 41

Answers (3)

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Consider the following approach:

public function startCol($p = 0) {
        $pad = (!$p)? $this->pad : $p; 
        $tab= "<td Style='border:4px solid black; padding:".intval($pad)."px;'>";
        return $tab;
}

If the argument was not passed in then use the internal $this->pad value

Upvotes: 1

David
David

Reputation: 218828

When you do this:

$tab= "<td Style='border:4px solid black; padding:110px;>"; 

The result is this:

<td Style='border:4px solid black; padding:110px;>

Which is technically invalid because it's missing a closing quote at the end of an attribute, but the browser is probably correcting that for you when rendering.

But when you do this:

$tab= "<td Style='border:4px solid black; padding:'".$p."'px;>";

The result is this:

<td Style='border:4px solid black; padding:'100'px;>

Which is much more invalid and probably confusing the browser.

I don't know why you put those single-quotes in there, but they produce invalid markup. Don't just look at the rendered page when debugging, actually view the source which was returned by the server.

Basically, don't add the extra quotes, and do add the closing quote:

$tab= "<td Style='border:4px solid black; padding:".$p."px;'>";

Or, even better, use double-quotes in the markup since that's what HTML technically calls for:

$tab= '<td Style="border:4px solid black; padding:'.$p.'px;">';

Upvotes: 3

Doomer
Doomer

Reputation: 75

You have one too many ' and one ' is misplaced. Try like this:

public function startCol($p) {
        $tab= "<td Style='border:4px solid black; padding:" . $p . "px;'>";                
        return $tab;
    }

Upvotes: 0

Related Questions