Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6999345
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T20:31:45+00:00 2026-05-27T20:31:45+00:00

So I’m generating an html table through my php to arrange my content. Unfortunately,

  • 0

So I’m generating an html table through my php to arrange my content.
Unfortunately, the code is ugly, hard to work with, and as we all know, tables should NEVER EVER be used for layout.

The problem is that based on the content I wanted to put on my page from my database, I couldn’t figure out any other option.

I would prefer to use divs or lists here, but I can’t figure out how best to do it. If the content wasn’t dynamic, it’d be easy, but it’s a little more complicated than that.

Here’s my php:

<?php
    $sql = 'SELECT * FROM titles';
    $result = $dbh->prepare($sql);
    $entries_per_row = 2;

    $i = 1;
    $j = 1;
    foreach ($dbh->query($sql) as $row) {           
        echo "<table class=\"category_list\">\r\n".
            "<tr>\r\n".
            "<td colspan=\"".$entries_per_row."\">\r\n".
            "<a id=\"sh".$i."\" href=\"#\" class=\"showhide\">-</a>&nbsp;<span class=\"title\">".$row['TitleName']."</span>\r\n".
            "</td>\r\n".
            "</tr>\r\n";

        $sql2 = 'SELECT * FROM categories WHERE TitleID = '.$row['TitleID'];
        $rows2 = array();
        foreach ($dbh->query($sql2) as $row2) {
            $rows2[] = $row2;
        }

        $rows2 = array_chunk($rows2, $entries_per_row);

        foreach ($rows2 as $row2) {
            echo "<tr id=\"".$i."tr".$j."\">\r\n";

            foreach($row2 as $element) {
                if ($element['CategoryName'] != '' && $element['CategorySummary'] != '') {
                    echo "<td class=\"cat\">\r\n".
                        "<a class=\"cat_title\" href=\"display.php?cat=".$element['CategoryName']."&page=1\">".$element['CategoryName']."</a><br />\r\n".
                        "<span class=\"cat_sum\">".$element['CategorySummary']."</span>\r\n".
                        "</td>\r\n";
                }
            }
            $j = $j + 1;
            echo "</tr>\r\n";
        }

        $i = $i + 1;
        echo "</table>\r\n";
    }
?>

Here’s the HTML that it outputs (indenting included):

<table class="category_list">
    <tr>
        <td colspan="2">
            <a id="sh1" href="#" class="showhide">-</a>&nbsp;<span class="title">Test Title 1</span>
        </td>
    </tr>
    <tr id="1tr1">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 1&page=1">Test Category 1</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 2&page=1">Test Category 2</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
    <tr id="1tr2">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 3&page=1">Test Category 3</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
</table>

<table class="category_list">
    <tr>
        <td colspan="2">
            <a id="sh2" href="#" class="showhide">-</a>&nbsp;<span class="title">Test Title 2</span>
        </td>
    </tr>
    <tr id="2tr3">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 4&page=1">Test Category 4</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 5&page=1">Test Category 5</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
    <tr id="2tr4">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 6&page=1">Test Category 6</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
</table>

<table class="category_list">
    <tr>
        <td colspan="2">
            <a id="sh3" href="#" class="showhide">-</a>&nbsp;<span class="title">Test Title 3</span>
        </td>
    </tr>
    <tr id="3tr5">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 7&page=1">Test Category 7</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 8&page=1">Test Category 8</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
    <tr id="3tr6">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 9&page=1">Test Category 9</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 10&page=1">Test Category 10</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
</table>

If anyone can suggest some php code that’ll arrange this correctly using divs or lists, I’d greatly appreciate it!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-27T20:31:46+00:00Added an answer on May 27, 2026 at 8:31 pm

    You can just refactor your HTML code with a very simple technique if you want to use divs instead. Just port every table tag into a div and give it it’s class. If you run across a table tag you think it does not belongs to the “table”, give it another class:

    From:

    <table class="category_list">
    <tr>
        <td colspan="2">
            <a id="sh1" href="#" class="showhide">-</a>&nbsp;<span class="title">Test Title 1</span>
        </td>
    </tr>
    <tr id="1tr1">
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 1&page=1">Test Category 1</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
        <td class="cat">
            <a class="cat_title" href="display.php?cat=Test Category 2&page=1">Test Category 2</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </td>
    </tr>
    

    To:

    <div class="table category_list">
    <div class="showhide-container">
        <a id="sh1" href="#" class="showhide">-</a>&nbsp;<span class="title">Test Title 1</span>
    </div>
    <div class="table-tr" id="1tr1">
        <div class="table-td cat">
            <a class="cat_title" href="display.php?cat=Test Category 1&page=1">Test Category 1</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </div>
        <div class="table-td cat">
            <a class="cat_title" href="display.php?cat=Test Category 2&page=1">Test Category 2</a><br />
            <span class="cat_sum">This is a test category. It will be deleted soon.</span>
        </div>
    </div>
    

    This refactoring is quite easy to do and you only need to change some of the strings in your output routine, the overall routine stays merely the same.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have this code to decode numeric html entities to the UTF8 equivalent character.
I want to count how many characters a certain string has in PHP, but
I would like to count the length of a string with PHP. The string
this is what i have right now Drawing an RSS feed into the php,
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have a text area in my form which accepts all possible characters from

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.