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 8287637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T12:06:20+00:00 2026-06-08T12:06:20+00:00

Possible Duplicate: php: how to add odd/even loop in array I am generating a

  • 0

Possible Duplicate:
php: how to add odd/even loop in array

I am generating a table in php with the below code.

<?PHP
while ($row = $mydata->fetch())
{
  $tests[] = array(
  'a' => $row['a'], 
  'b' => $row['b']
  )
  ;
}

?>

Then the output code

<table>
  <tbody>
  <tr><th>#</th><th>a</th><th>b</th></tr>
  <?php foreach ($tests as $test): ?>
    <tr class="">
        <td></td>
        <td><?php htmlout($test['a']); ?></td>
        <td><?php htmlout($test['b']); ?></td>
    </tr>
<?php endforeach; ?>
  </tbody>
  </table>

which outputs

<table>
  <tbody>
  <tr><th>#</th><th>a</th><th>b</th></tr>
    <tr class="">
        <td></td><td>a content</td><td>b content</td>
    </tr>
    <tr class="">
        <td></td><td>a content</td><td>b content</td>
    </tr>
  </tbody>
  </table>

htmlout is the below custom function.

<?php
function html($text)
{
return htmlspecialchars($text, ENT_QUOTES, 'UTF-8');
}
function htmlout($text)
{
echo html($text);
}
?>

This is all working well but I can’t work out two things here.

  1. I want my rows to generate <tr class="odd"> and <tr class="even"> on alternate rows
  2. I want the first <td></td> within the <tr> to count show the row number of the data eg <td>1</td> in the first <tr class=""> <td>2</td> in the second etc.

I have looked at numerous examples such as this

$count = 1;
while ($count <= 10)
{
echo "$count ";
++$count;
}

But can’t work out how to implent it into my example or maybe I should use another method. I understand I can do the table rows in jQuery and in some browsers with css3 but would prefer a php solution in this case.

  • 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-06-08T12:06:22+00:00Added an answer on June 8, 2026 at 12:06 pm

    You can use something like this:

    <?php foreach ($tests as $i => $test): ?>
        <?php $class = ($i % 2 == 0) ? 'even' : 'odd'; ?>
        <tr class="<?php echo $class; ?>">
            <td><?php echo $i + 1; ?></td>
            <td><?php htmlout($test['a']); ?></td>
            <td><?php htmlout($test['b']); ?></td>
        </tr>
    <?php endforeach; ?>
    

    This leverages the fact that the array keeps a numerical index $i. So, the row number is really $i + 1, which we put into the first column. Then, we determine if the current row is even or odd based on whether or not $i is divisible by 2. If $i is divisible by 2, it’s an even row, otherwise it’s an odd row. We save the class string in $class, and put it in the <tr> tag.

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

Sidebar

Related Questions

Possible Duplicate: Add a prefix to each item of a PHP array I try
Possible Duplicate: Are PHP Associative Arrays ordered? If I add items to associative array
Possible Duplicate: Will PHP be slower if we add too many comments in code
Possible Duplicate: How to add .php extension using .htaccess url rewriting? I want to
Possible Duplicate: Close mysql connection (PHP) Please help me, I not know where add
Possible Duplicate: PHP get both array value and array key I am using Codeigniters'
Possible Duplicate: php loop through associative arrays i have a variable which prints an
Possible Duplicate: array_splice() for associative arrays How to add an array value to the
Possible Duplicate: PHP get all arguments as array? Within a javascript function arguments always
Possible Duplicate: Any php code to detect the browser with version and operating system?

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.