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

  • Home
  • SEARCH
  • 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 3975700
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T04:42:59+00:00 2026-05-20T04:42:59+00:00

I’m trying to create an html table from database records. I want the table

  • 0

I’m trying to create an html table from database records. I want the table to be 4 cells wide, so I figured if the array index of the row I’m looping through is a multiple of 4, then add a tr tag at the beginning. However, it doesn’t seem to be working correctly. Can anyone help me out? (This is CodeIgniter, so the echo anchor, etc. just creates an a href tag.)

<table width="80%" border="1">
<tr> <!-- create initial tr tag, since we haven't started the loop yet -->
<?php foreach($projects as $index=>$project) : ?>
    <?php echo ((($index + 1) % 4 == 0) ? '<tr>' : ''); ?>
    <td>
    <?php echo anchor('project/view/'.$project->id, $project->project_name, 'title='.$project->project_name); ?>
    </td>
    <?php echo ((($index + 1) % 4 == 0) ? '</tr>' : ''); ?>
<?php endforeach; ?>
 </table>

The $index + 1 is because dividing by 0 (where the array index starts) causes an error. I echoed out the values of $index+1 on each row, and if I have 5 rows in my table I get 1, 2, 3, 4, 5. But the line that should create the tr tag if $index+1 does not divide evenly by 4 isn’t evaluating; I’m getting a single row table that just keeps getting wider.

Here’s what I’d expect to see as rendered code:

<table width="80%" border="1">
  <tr> 
    <td><a href="http://localhost/ignite/index.php/project/view/1" title=Basil's Beatnik Turtle>Basil's Beatnik Turtle</a></td> 
    <td><a href="http://localhost/ignite/index.php/project/view/2" title=Mr. Werewolf Genes>Mr. Werewolf Genes</a></td> 
    <td><a href="http://localhost/ignite/index.php/project/view/3" title=Romeo+Juliet>Romeo+Juliet</a></td> 
    <td><a href="http://localhost/ignite/index.php/project/view/4" title=Basic Hat>Basic Hat</a></td> 
  </tr>
  <tr>
    <!-- I'm not trying to auto-generate empty cells to fill in a final row that contains fewer than 4 at this point, although it would be nice -->
    <td><a href="http://localhost/ignite/index.php/project/view/5" title=Flutterby Hat>Flutterby Hat</a></td> 
  </tr>
</table> 

Here’s what I’m actually getting:

<table width="80%" border="1">
  <tr> 
    <td><a href="http://localhost/ignite/index.php/project/view/1" title=Basil's Beatnik Turtle>Basil's Beatnik Turtle</a></td> 
    <td><a href="http://localhost/ignite/index.php/project/view/2" title=Mr. Werewolf Genes>Mr. Werewolf Genes</a></td> 
    <td><a href="http://localhost/ignite/index.php/project/view/3" title=Romeo+Juliet>Romeo+Juliet</a></td> 
  <!-- note the lack of closing </tr> tag on previous chunk - plus it's only 3 cells, not 4 -->

  <tr>
    <td><a href="http://localhost/ignite/index.php/project/view/4" title=Basic Hat>Basic Hat</a></td>
  </tr>
  <!-- this previous chunk has both <tr> and </tr>, but only contains one <td> -->

    <td><a href="http://localhost/ignite/index.php/project/view/5" title=Flutterby Hat>Flutterby Hat</a></td> 
  <!-- the previous chunk is missing both <tr> and </tr> tags, and is only a single cell -->
</table>     

Basil’s Beatnik Turtle
Mr. Werewolf Genes
Romeo+Juliet
tag on previous chunk – plus it’s only 3 cells, not 4 –>

Basic Hat

and , but only contains one –>

Flutterby Hat
and tags, and is only a single cell –>

Where am I going wrong?

  • 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-20T04:43:00+00:00Added an answer on May 20, 2026 at 4:43 am

    Without knowing what the data that’s being returned from the SQL query looks like, it seems like $index is not what you are expecting it to be.

    I humbly suggest this alternative:

    <?
    $count = 1;
    foreach($projects as $index=>$project)
    {
        if($count % 4 == 0)
        {
             print "</tr><tr>";
        }
        print "<td>" . anchor('project/view/'.$project->id, $project->project_name, 'title='.$project->project_name) . "</td>";
        $count++;
    }
    ?>
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I'm new to using the Perl treebuilder module for HTML parsing and can't figure
Does anyone know how can I replace this 2 symbol below from the string
I am trying to loop through a bunch of documents I have to put
I have a bunch of posts stored in text files formatted in yaml/textile (from
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
i want to parse a xhtml file and display in UITableView. what is the

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.