I've been searching for this for about 2 hours and haven't found anything that specifically matches what I'm trying to do. It might not be possible, but I consider myself basic with php.

I have a php script that runs a MySQL query and uses that data to create an HTML email. The result is put into a while loop used to iterate and create HTML table rows for the n result rows. My primary goal is to use a separate array outside of the MySQL result array that contains an alternating bgcolor for each row, and put a function inside the MySQL result array that alternates between n colors that I choose. I am completely open to other solutions to make this use as little lines of code as possible.

Here is what the incomplete nonworking code basically looks like (using fake color names for readability):

$shade = array("red","blue","green");
$sql1 = "MySQL Query";
$sql1Handle = MySqlQuery($sql1,$DBLink);
while($sql1Data = mysql_fetch_row($sql1Handle))
{
    $htmlText .= "<tr bgcolor = \"".$shade[0]."\"><td>".$sql1Data[0]."</td><td>".$sql1Data[1]."</td><td>".$sql1Data[2]."</td></tr>";
}

Expected colored result would be (if the MySQL result had 7 rows):

red row $sql1Data[0] $sql1Data[1] $sql1Data[2]
blue $sql1Data[0] $sql1Data[1] $sql1Data[2]
green $sql1Data[0] $sql1Data[1] $sql1Data[2]
red $sql1Data[0] $sql1Data[1] $sql1Data[2]
blue $sql1Data[0] $sql1Data[1] $sql1Data[2]
green $sql1Data[0] $sql1Data[1] $sql1Data[2]
red $sql1Data[0] $sql1Data[1] $sql1Data[2]

I know I can accomplish this task declaring two or more variables as colors and putting an if statement inside the while loop to alternate on each row, but I want to see if this is possible. I use it quite often and I'd prefer to create a function to handle this and provide it with however many colors I want.

Thanks.

New contributor
doolittlet1 is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
  • I hope you're not serious about mysql_fetch_row(). Those functions have been outdated for a decade, and haven't even been included in PHP for 3 years now. – miken32 Nov 15 at 21:12
  • Sadly my company is still using v5.2.6 – doolittlet1 Nov 15 at 21:16
  • 1
    That's criminally irresponsible. Regardless, it still means you have access to PDO. – miken32 Nov 15 at 21:18
  • 1
    It would be much better to do this with CSS instead. stackoverflow.com/questions/3084261/… (The top answer there uses jQuery, which isn't necessary.) – Don't Panic Nov 15 at 21:18
  • @miken32 I just use what's available to me and learn based on other scripts I find working here to solve the problems I have in my workflow. Not really a programmer. – doolittlet1 Nov 15 at 21:26
up vote 1 down vote accepted

You can use a simple counter and use the mod operator % to cycle through them, using $current++ each time to move onto the next shade...

$shade = array("red","blue","green");
// Which is the current shade to use
$current = 0;
// Store count so that it is dynamic
$shadeCount = count($shade);
$sql1 = "MySQL Query";
$sql1Handle = MySqlQuery($sql1,$DBLink);
while($sql1Data = mysql_fetch_row($sql1Handle))
{
    $htmlText .= "<tr bgcolor = \"".$shade[$current++ % $shadeCount]."\"><td>".$sql1Data[0]."</td><td>".$sql1Data[1]."</td><td>".$sql1Data[2]."</td></tr>";
}
  • Simple and works perfectly! Don't even need a separate function. Thanks so much! – doolittlet1 Nov 15 at 21:22

Your Answer

doolittlet1 is a new contributor. Be nice, and check out our Code of Conduct.
 

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Not the answer you're looking for? Browse other questions tagged or ask your own question.