lilmer Posted February 12, 2013 Share Posted February 12, 2013 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>'; } Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/ Share on other sites More sharing options...
Barand Posted February 12, 2013 Share Posted February 12, 2013 The browser outputs anything inside a table that is not between <td>..</td> tags (ie not cell content) above the table Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1411914 Share on other sites More sharing options...
Nodral Posted February 12, 2013 Share Posted February 12, 2013 You may want to check your spelling of the function in this line too echo '<td class="char-'.$i.'-'.$this->alphaCunt.'"> </td>'; Childish I know, but made me chuckle Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1411966 Share on other sites More sharing options...
Jessica Posted February 12, 2013 Share Posted February 12, 2013 I wonder if it's a pubic--I mean public-- function. Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1411968 Share on other sites More sharing options...
lilmer Posted February 13, 2013 Author Share Posted February 13, 2013 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> </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. Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1412182 Share on other sites More sharing options...
cyberRobot Posted February 13, 2013 Share Posted February 13, 2013 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 Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1412217 Share on other sites More sharing options...
lilmer Posted February 14, 2013 Author Share Posted February 14, 2013 Thanks. . Got it! so the code will be, then public function alphaCount(){ for ($i=65; $i<=90; $i++) { $output[] = strtolower(chr($i)); } return $output; } Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1412350 Share on other sites More sharing options...
cyberRobot Posted February 14, 2013 Share Posted February 14, 2013 (edited) 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 February 14, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1412418 Share on other sites More sharing options...
cyberRobot Posted February 14, 2013 Share Posted February 14, 2013 (edited) 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 February 14, 2013 by cyberRobot Quote Link to comment https://forums.phpfreaks.com/topic/274382-php-function-inside-loop/#findComment-1412421 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.