Jump to content

Create html code from a php scriptand then scrap the code to a string...


filoaman

Recommended Posts

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.

 

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

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

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

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.