0

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);

?>
2
  • Sidenote: You do realize that your code produces the same array 3 times vertically. Is that the intention? Commented Feb 11, 2016 at 14:55
  • that is not the intention. I am into procedural programming and learning OOP by coding something on my own Commented Feb 12, 2016 at 7:45

3 Answers 3

3

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;">';
Sign up to request clarification or add additional context in comments.

2 Comments

Lovin' a well-explained answer. Especially the part about: "Don't just look at the rendered page when debugging, actually view the source". Something I also suggest many times. (Some) People don't really see as viewing their HTML source being an actual "tool".
There's something else about the OP's code which will produce: "no space between attributes" in HTML source. Reason: Misplaced semi-colons. solid black'; < which should read as solid black;'. Plus, they should add \n's after each of those in order to produce clean HTML. Right now, it reads as one long line and makes it quite difficult to debug/trace.
1

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

4 Comments

$tab= "<td Style='border:4px solid black; padding:'".$pad."'px;>"; will read as <td Style='border:4px solid black; padding:'110'px;>Jan</td> (with single quotes inside '110') in HTML source and willl also throw: "no space between attributes" in HTML source. It should read as $tab= "<td Style='border:4px solid black; padding:".$pad."px;'>";
@Fred-ii-, thank you for that fix. The air is stifling and hot here in our office, and my brain isn't working well
You're welcome Roman, cheers. See if management will put in an air conditioner ;-)
it's just because the others are afraid to get sick, therefore, all the windows are closed. But brain performance is getting down. Man, I would like to have my own cabinet )
0

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;
    }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.