Jump to content

PHP function inside loop


lilmer

Recommended Posts

Hello Good day,

 

I've made a Loop functions on a php and inside there is another function of loop. I did that because I'm not good on looping (loop within the loop), but what happen is the output of the function is placing outside the table. I already check the Quotes(') or something, but still it is placing outside. Anyone can explain to me why?

 

 

function alphaCount()

{

for ($i=65; $i<=90; $i++)

{

echo char($i);

}

}

for($i=1;$i<=10;$i++){

echo '<td class="char-'.$i.'-'.$this->alphaCunt.'"> </td>';

}

Link to comment
Share on other sites

The code is like this totally. .

 

so when i indicate this to a view page the output of the alphaCount() is not going inside the table row or outside the table.

 

 

 

 

public function alphaCount(){

for ($i=65; $i<=90; $i++) {

echo strtolower(chr($i));

}

}

 

public function dump(){

 

$target = $this->alphaCount();

 

$out = "<table id='sheet' class=\"$table_class\" cellspacing=0>";

if ($col_letters) {

$out .= "<thead>\n\t<tr>";

if ($row_numbers) {

$out .= "\n\t\t<th>&nbsp</th>";

}

for($i=1;$i<=$this->colcount($sheet);$i++) {

$style = " min-width:50px;width:" . ($this->colwidth($i,$sheet)*1) . "px;";

if ($this->colhidden($i,$sheet)) {

$style .= "display:none;";

}

$out .= "\n\t\t<th style=\"$style\">" .strtoupper($this->colindexes[$i]) . "</th>";

}

$out .= "</tr></thead>\n";

}

 

$out .= "<tbody>\n";

for($row=1;$row<=$this->rowcount($sheet);$row++) {

$rowheight = $this->rowheight($row,$sheet);

$row_class = " 'char-".$row. "-".$target." ' ";

$style = "height:" . ($rowheight*(4/3)) . "px;";

if ($this->rowhidden($row,$sheet)) {

$style .= "display:none;";

}

$out .= "\n\t<tr style=\"$style\">";

if ($row_numbers) {

$out .= "\n\t\t<th>$row</th>";

}

for($col=1;$col<=$this->colcount($sheet);$col++) {

// Account for Rowspans/Colspans

$rowspan = $this->rowspan($row,$col,$sheet);

$colspan = $this->colspan($row,$col,$sheet);

for($i=0;$i<$rowspan;$i++) {

for($j=0;$j<$colspan;$j++) {

if ($i>0 || $j>0) {

$this->sheets[$sheet]['cellsInfo'][$row+$i][$col+$j]['dontprint']=1;

}

}

}

if(!$this->sheets[$sheet]['cellsInfo'][$row][$col]['dontprint']) {

$style = $this->style($row,$col,$sheet);

if ($this->colhidden($col,$sheet)) {

$style .= "padding:1px 2px;display:none;";

}

$out .= "\n\t\t<td class=".$row_class."style=\"$style\"" . ($colspan > 1?" colspan=$colspan":"") . ($rowspan > 1?" rowspan=$rowspan":"") . ">";

$val = $this->val($row,$col,$sheet);

if ($val=='') { $val=" "; }

else {

$val = htmlentities($val);

$link = $this->hyperlink($row,$col,$sheet);

if ($link!='') {

$val = "<a href=\"$link\">$val</a>";

}

}

$out .= "<nobr>".nl2br($val)."</nobr>";

$out .= "</td>";

}

}

$out .= "</tr>\n";

}

$out .= "</tbody></table>";

return $out;

 

}

 

BTW: Thanks for the reply.

Link to comment
Share on other sites

The following line suggests that you're expecting alphaCount() to return the results:

 

<?php
$target = $this->alphaCount();
?>

 

The problem is that the function echos the results to the screen instead of returning them.

 

 

<?php
public function alphaCount(){
    for ($i=65; $i<=90; $i++) {
         echo strtolower(chr($i));
    }
}
?>

 

You might want to review the manual for returning values.

http://www.php.net/m...ning-values.php

Link to comment
Share on other sites

That depends; the code looks to be expecting a string.

 

<?php
$row_class = " 'char-".$row. "-".$target." ' ";
?>

 

The function could be modified to something like

 

<?php

public function alphaCount() {
    $output = '';
    for ($i=65; $i<=90; $i++) {
         $output .= chr($i);
    }
    return strtolower($output);
}
?>

 

Note that I moved the strtolower() function to the return statement so that it's only executed once versus 24 times. ;)

Edited by cyberRobot
Link to comment
Share on other sites

Now that I think about the function a little more, have you considered using range (http://php.net/manua...ction.range.php)? You could do something like

 

<?php
public function alphaCount() {
    return implode('', range('a', 'z'));
}
?>

 

If the string is always "abcdefghijklmnopqrstuvwxyz", you could just return the string.

 

<?php
public function alphaCount() {
    return 'abcdefghijklmnopqrstuvwxyz';
}
?>

Edited by cyberRobot
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.