filoaman Posted June 21, 2013 Share Posted June 21, 2013 Hi friends I use this php code in order to create a simple html table from my data $start = 0; $end = $howmanyItemsOnArray; $split = 2; print "<table><tr>"; for($i = $start; $i < $end; $i++) { print "<td><a href=\"../../pages/".$pagesArray[$i]."\">".$clearPagesArray[$i]."</a></td>"; if(($i) % ($split) == $split-1){ print "</tr><tr>"; } } print"</tr></table>"; After the execution of the script i get this code <table><tr><td> <a href="../../pages/file01.html">File Title 01</a></td><td> <a href="../../pages/file02.html">File Title 02</a></td></tr><tr><td> <a href="../../pages/file03.html">File Title 03</a></td><td> <a href="../../pages/file04.html">File Title 04</a></td></tr><tr><td> <a href="../../pages/file05.html">File Title 05</a></td><td> <a href="../../pages/file06.php">File Title 06</a></td></tr><tr><td> <a href="../../pages/file07.html">File Title 07</a></td><td> <a href="../../pages/file08.html">File Title 08</a></td></tr><tr><td> <a href="../../pages/file09.html">File Title 09</a></td><td> <a href="../../pages/file10.php">File Title 10</a></td></tr><tr><td> <a href="../../pages/file11.html">File Title 11</a></td><td> <a href="../../pages/file12.html">File Title 12</a></td></tr><tr><td> <a href="../../pages/file13.html">File Title 13</a> </td></tr></table> Now what i want to for an application is store this html code to a string. Is this possible? I'm waiting for ideas. Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/ Share on other sites More sharing options...
Csharp Posted June 21, 2013 Share Posted June 21, 2013 You can easily replace your print statements with assignments. So your code would be like: $str = ''; $str.= "<td><a href=\"../../pages/".$pagesArray[$i]."\">".$clearPagesArray[$i]."</a></td>"; Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/#findComment-1437277 Share on other sites More sharing options...
filoaman Posted June 21, 2013 Author Share Posted June 21, 2013 I try this code but i get an empty string... I'm not sure if i express my question clearly. What i want finally is a string like this: $str='<table><tr><td><a href="../../pages/file01.html">File Title 01</a></td><td><a href="../../pages/file02.html">File Title 02</a></td></tr><tr><td><a href="../../pages/file03.html">File Title 03</a></td><td><a href="../../pages/file04.html">File Title 04</a></td></tr><tr><td><a href="../../pages/file05.html">File Title 05</a></td><td><ahref="../../pages/file06.php">File Title 06</a></td></tr><tr><td><a href="../../pages/file07.html">File Title 07</a></td><td><a href="../../pages/file08.html">File Title 08</a></td></tr><tr><td><a href="../../pages/file09.html">File Title 09</a></td><td><a href="../../pages/file10.php">File Title 10</a></td></tr><tr><td><a href="../../pages/file11.html">File Title 11</a></td><td><a href="../../pages/file12.html">File Title 12</a></td></tr><tr><td><a href="../../pages/file13.html">File Title 13</a></td></tr></table>'; Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/#findComment-1437282 Share on other sites More sharing options...
Csharp Posted June 21, 2013 Share Posted June 21, 2013 You just initialize with $str=''; once. Otherwise you will just get empty strings. Your code would be <?php $start = 0; $end = $howmanyItemsOnArray; $split = 2; $str = ''; $str.= "<table><tr>"; for($i = $start; $i < $end; $i++) { $str.= "<td><a href=\"../../pages/".$pagesArray[$i]."\">".$clearPagesArray[$i]."</a></td>"; if(($i) % ($split) == $split-1){ $str.= "</tr><tr>"; } } $str.="</tr></table>"; //Print or do whatever to str print($str); Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/#findComment-1437284 Share on other sites More sharing options...
filoaman Posted June 21, 2013 Author Share Posted June 21, 2013 Yes it works. The problem is that i initialize the $str more than once SOLVED Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/#findComment-1437287 Share on other sites More sharing options...
AbraCadaver Posted June 21, 2013 Share Posted June 21, 2013 Make sure you are using . dot = equals in the proper places below: $html = "<table><tr>"; for($i = $start; $i < $end; $i++) { $html .= "<td><a href=\"../../pages/".$pagesArray[$i]."\">".$clearPagesArray[$i]."</a></td>"; if(($i) % ($split) == $split-1){ $html .= "</tr><tr>"; } } $html .= "</tr></table>"; If you don't wan to change existing code, then use output buffering: ob_start(); // your print code $html = ob_get_contents(); //or ob_get_clean() depending if you want the html to actually display or not Link to comment https://forums.phpfreaks.com/topic/279432-create-html-code-from-a-php-scriptand-then-scrap-the-code-to-a-string/#findComment-1437288 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.