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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T12:38:47+00:00 2026-06-18T12:38:47+00:00

Attempting to change the colour for alternative lines in a table using CSS for

  • 0

Attempting to change the colour for alternative lines in a table using CSS for a PHP script. I have got the colour but the lines are repeating in my script. Is there anyway around this?

The PHP Script:

<?php

ini_set('display_errors',1); 
error_reporting(E_ALL);

//print_r(PDO::getAvailableDrivers()); 

$config['db'] = array( //This is the config array with the database details
    'host'              => 'localhost',
    'username'          => 'root',
    'password'          => '',
    'dbname'            => 'website'
); 


//echo '<table, th, td {border: 2px solid black; border-collapse="collapse";} >';
echo '<table id="customers"}>';
echo '<link rel="stylesheet" type="text/css" href="style.css">';

echo '<th> one </th>';
echo '<th> two </th>';
echo '<th> three </th>';
echo '<th> four </th>';


$db = new PDO('mysql:host=' . $config['db']['host'] . ';dbname=' . $config['db']['dbname'], $config['db']['username'], $config['db']['password']); //Instanciate an PDO Object


$query = $db->query("SELECT `articles`.`title`, `articles`.`hello`, `articles`.`id`,         `articles`.`name` FROM `articles` WHERE `articles`.`title` > 1 LIMIT 5");//running a query         from the database


while ($row = $query->fetch(PDO::FETCH_ASSOC)){
echo '<tr>';
echo '<td>';
echo $row["title"];
echo '</td>';
echo '<td>';
echo $row["hello"];
echo '</td>';
echo '<td>';
echo $row["id"];
echo '</td>';
echo '<td>';
echo $row["name"];
echo '</td>';
echo '</tr>';
echo '<tr class="alt">';
echo '<td>';
echo $row["title"];
echo '</td>';
echo '<td>';
echo $row["hello"];
echo '</td>';
echo '<td>';
echo $row["id"];
echo '</td>';
echo '<td>';
echo $row["name"];
echo '</td>';
echo '</tr>';

//print_r($query); //printing a query 

};

echo '</table>';

echo '<p> Returned ', $query->rowCount(), ' results due to limit set</p>';


//$query = $db->query("SELECT `title` FROM `articles`");//running a query from the database


//$query = $db->query("SELECT * FROM articles");


//print_r($query); //printing a query

//$stmt = $db->prepare($sql);
//$stmt->execute();
//$results = $stmt->fetchAll();

/* $query = $db->query("SELECT `id` FROM `articles`");//running a query from the database

while ($row = $query->fetch(PDO::FETCH_ASSOC)){
echo $row['title'], '<br>';
echo $row['id'], '<br>';
}

}

*/

The CSS Script:

#customers
{
font-family:"Trebuchet MS", Arial, Helvetica, sans-serif;
width:100%;
border-collapse:collapse;
}
#customers td, #customers th 
{
font-size:1.2em;
border:1px solid #98bf21;
padding:3px 7px 2px 7px;
}
#customers th 
{
font-size:1.4em;
text-align:left;
padding-top:5px;
padding-bottom:4px;
background-color:#A7C942;
color:#fff;
}
#customers tr.alt td 
{
color:#000;
background-color:#EAF2D3;
}
  • 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-18T12:38:48+00:00Added an answer on June 18, 2026 at 12:38 pm
    $alt=0;
    while ($row = $query->fetch(PDO::FETCH_ASSOC)){
     if($alt==0){
      echo '<tr>';
      $alt=1;
     }else{
      echo '<tr class="alt">';
      $alt=0;
     }
     echo '<td>';
     echo $row["title"];
     echo '</td>';
     echo '<td>';
     echo $row["hello"];
     echo '</td>';
     echo '<td>';
     echo $row["id"];
     echo '</td>';
     echo '<td>';
     echo $row["name"];
     echo '</td>';
     echo '</tr>';
    }
    

    Don’t repeat rows twice inside your while loop. Just do it once and alternate the class assignment.

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

Sidebar

Related Questions

I'm attempting to just change the background colour of a webpage upon page load.
I'm attempting to change the style of a button using the :checked and :unchecked
I'm attempting to make a parallax page, but when I change the placeholder image
I am attempting to change a listview item's background colour when a mouse hovers
I am currently attempting to change the css of specific dates inside a jQuery
Using Bootstrap's typeahead javascript plugin, I'm attempting to change the data-source attribute via jQuery's
I am attempting to get watchr running tests automatically as files change, and got
I have a WPF application, and I am attempting to style a TextBox using
I am attempting to change a plugin's options, on the fly. But I cannot
I'm attempting to change the built in .Net culture fr-CA, using the following method:

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.