Jump to content

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


Go to solution Solved by 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>';
Edited by filoaman

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