[Go] class with PHP (V)

Source: Internet
Author: User
Tags array explode join variables
Classes and PHP

Let's do the table body now:

<TR>
<?php

$Tbody->tdout ("Kreisler");
$Tbody->tdout ("Rod");
$Tbody->tdout ("Cortlandt");
$Tbody->tdout ("New York");
$Tbody->tdout ("USA");
?>
</TR>

But This is still getting kind of lengthy. Couldn ' t we save some more steps? How about trying this:

<?php

function trout ($message) {/*and NO comments about fish, please!;) * *

PRINT "<tr>\n";
$cells =explode ("|", $message);
$iterations =count ($cells);
$i = 0;
while ($i < $iterations) {
List ($message, $span) =explode (":", $cells [$i]);
if (strlen ($message) <1) $message = "";
if ($span) {
$this->tdout ($message, $span);
}else{
$this->tdout ($message);
}
$i + +;
}
PRINT "</tr>\n";

}

?>

wow! That ' s a little more complicated. Let ' s break it down:

Line 3 splits the "pipes" and stores the pieces in the array $cells. Line 4 stores the number of items (number of cells) in $iterations. Line 6 begins we loop to go through these cell items. Line 7 splits the "cell data" in the colon and stores them into the variables $message and $span. Line 8 checks to the If a message is included. If not then it sets message to the default. Line 9 checks to the if there is a span listed (i.e. the cell data had a colon with something behind it. If so, line C Alls Tdout with the and the number of cells it spans. If not, line calls Tdout with just the message (Tdout'll set $colspan to the default 1). Lastly, we close out the row.

What This means are we can pass a single string to trout that would contain all of the necessary information to print out the E Ntire row as long as the "Celldata[:colspan]|celldata[:colspan]|......celldata[:colspan]" in the format.

So, instead of the ' all ' work we did before, the headers and body of the table could is called like this:

<TABLE>
?
$Theader->trout ("name:2| Address:3 ");
$Theader->trout ("first| Last| city| state/province| Country ");
$Tbody->trout ("rod| kreisler| Cortlandt| New york| USA ");
?>
</TABLE>

Wow. That ' s a lot easier. But What if the data is in variables? Simply Join the array:

<?php

$message =join ($arry, "|");
$Tbody->trout ($message);

?>



Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.