0

I find myself lately working with a ton of tabular data, I am more than comfortable writing the raw html but i'm wondering if there's an easier way (or library worth implementing) that helps reduce time writing html tags such as

<tr><td></td></tr>

I have created my own custom function, but I think ultimately it's not necessarily helping and potentially could be slowing down my script, now my project is small so maybe it could cope with that, examples:

echo '<tr class="test_class">
    <td>' . $content . '</td>
    <td>' . $second_content . '</td>
<tr/>';

here is an example with my current function:

tr("test_class");
   td(); echo $content; escape(td);
   td(); echo $second_content; escape(td);
escape(tr);

Looking forward to hearing peoples thoughts.

4
  • 2
    I recommend changing the 'escape' function name. It's not escaping a td, it's ending or closing the tag. Google 'backslash escape' if you're in doubt about the meaning. Commented Oct 9, 2012 at 13:43
  • that doesn't look like you're saving yourself any keystrokes. you're not writing html tags but you're keying in lots of function calls and echo statements. have you considered using zend framework? Commented Oct 9, 2012 at 13:43
  • I have thought about the zend framework several times but never seen any solid arguments for any framework, would it help in this situation? Commented Oct 9, 2012 at 13:47
  • I would use heredox syntax, that would make at least your example a lot easier to read: php.net/manual/en/… Commented Oct 9, 2012 at 13:52

4 Answers 4

2

There are multiple ways of doing this...

  1. write your own html helper library, that will contain classes, that can generate html elements based on their data source. For instance you could call them like:

    <?php
       HtmlHelper::Table("someArrayOfValues", "idOfTable", "styleOfTable");
    ?>
    

    This is a good reusable solution, if you implement this idea properly. I was playing with this myself few days ago, really it's simple.

  2. if you find 1. difficult, you can split the idea down... But not so deep like you've shown, but generate whole rows instead.

    <?php
        foreach ($myArray as $key => $value)
        {
             echo HtmlHelper::Row(...);
        }
    ?>
    
  3. Find some library, that provides this functionality. Can't help you on this one I'm afraid. I like to have control over the generated markup.

Hope you get the idea.

Sign up to request clarification or add additional context in comments.

Comments

2

If you have short_open_tags turned on (and assuming you can turn it on, if necessary), you can use the templating syntax, like this:

<table>
    <?php foreach($myList as $key => $value): ?>
    <tr>
        <td><?= $value["key1"] ?></td>
        <td><?= $value["key2"] ?></td>
        ...
    </tr>
    <?php endforeach; ?>
</table>

That might make your job easier, in terms of writing tabular data.

19 Comments

while also making your code more bound to needless environmental constraints.
While I like the detailed reply I would have to agree, I would never pidgeonhole my code as you never know what system it's going to run on :)
How is using short_open_tags a needless environmental constraint?
I see what you mean. There's plenty of ways to turn it on without having to administer the machine itself, like an .htaccess file. But, that's assuming you're using Apache. :)
@MattWhipple This is not an issue. Virtually any part of PHP may be disabled on a host, that doesn't mean you should avoid those pieces of the language. You might as well say "I avoid database access, because not every host provides a database". There is absolutely no reason to avoid <?= in your templates.
|
1

The biggest strength of PHP for web development is how much its made to do with few calls, and in particular for this case the echoing of content without the need to work through language constraints. So in general unless the case is really warranted, directly writing the html with the echoes will be the simplest solution that takes the most advantage of PHP, and simplicity is always a good thing.

That being said, if you have a lot of complex table generation, then the code would be more readable if use a library like: http://pear.php.net/package/HTML_Table/. Additionally if you were looking to do something like serialize an object into a table display, then creating a serializer that is made for that would be the solution most in-line with the functionality.

In the code above I'd suggest a transparent utility function certainly wouldn't hurt. But rather than the direction you're going if you consistently have the same number of columns then you could use an array which is joined with the table cell separation markup (a function that produces a row at a time).

Comments

0

it is more comfort to use some template engines. try twig or smarty

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.