Is the cache enabled or do you need a file so that you can use it over and over again?

With PHP, I present an HTML table that looks something like this:

--------------------------------------------------
| title    | description         | actions       |
-------------------------------------------------|
| title1   | desc1               | edit / delete |
| title2   | desc2               | edit / delete |
| title3   | desc3               | edit / delete |
| title4   | desc4               | edit / delete |
--------------------------------------------------

      

This code works as follows (stripped down version! No thead

, tbody

etc. in this example):

<table>
  <?php foreach ( $tableData as $row ): ?>
  <tr>

    <!-- Render columns (title, description)
    <?php foreach ( $columnData as $column ): ?>
    <td>
      <?= $row[$column['name']]; ?>
    </td>
    <?php endforeach; ?>

    <!-- Render actions -->
    <?php foreach ( $actions as $action ): ?>
    <td>
      <?= include($action); // include the proper action button! ?>
    </td>
    <? endforeach; ?>

  </tr>
  <?php endforeach; ?>
</table>

      

This gives me the desired output. But I only have one problem. It gets slow when I have 1000 entries. I already noticed that this is because I am doing include

for each row of the table. When I delete include

everything is very fast.

The file included

contains some PHP logic. So I can't just do file_get_contents

. Well, I could, but then I have to use eval()

to parse the content. But I would rather not use this feature at all.

So now I'm wondering if it is possible to cache the included file? So PHP shouldn't actively include the actual file over and over, but gets it from its cache? Is this possible?

Or are there any better alternatives?

+3


source to share


5 answers


You can put all actions (functions) in one file and include this file once. In the loop, you will call different functions according to their name. An example of a variable function .



+1


source


How you either edit or delete when navigating to this page, it seems likely that the table changes (almost ...) every time you open it again, so caching might not be the best option.



An alternative would be to use pagination and add a show all button so that the full table is generated only when you really need it.

0


source


It looks like there is something wrong with your action renders.
They simply cannot be large enough to fit in separate files. To make a simple hyperlink, you just need a line of HTML in the template.
If you have too much logic in these renderers, then you need to move it to part of the business logic.

Confident icon . By real pagination I mean when only 100 records per call are processed, not all 1000, of which 900 are hidden.

0


source


One option is to take a "boilerplate" route. Extract all PHP from the file and replace the data pieces with "tags". Then you download the file once (file_get_contents) and replace the tags with data.

//template.html
Replace this template {variable} with {search} and {replace}.
{conditional_html}

//template.php
$tpl = file_get_contents('template.html');
$output = '';
foreach($record as $r) {
    $data = array('{variable}'=>$r['field1'], 
      '{search}'=>$r['field2'], 
      '{replace}'=>$r['field3'],
      '{contional_html}'=>($r['field4']='1A' ? 'Display Conditional' : '') };
    $output .= str_replace(array_keys($data), $data, $tpl);
}

      

This will prevent access to the files, but you create your own markup language for templates. Ideally, your business logic is separate from your display / rendering.

0


source


include_once () might be what you are looking for. Here's the link: http://php.net/manual/en/function.include-once.php

-1


source







All Articles