0

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.

  • 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 '18 at 21:12
  • Sadly my company is still using v5.2.6 – doolittlet1 Nov 15 '18 at 21:16
  • 1
    That's criminally irresponsible. Regardless, it still means you have access to PDO. – miken32 Nov 15 '18 at 21:18
  • 1
    It would be much better to do this with CSS instead. https://stackoverflow.com/questions/3084261/alternate-table-row-color-using-css (The top answer there uses jQuery, which isn't necessary.) – Don't Panic Nov 15 '18 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 '18 at 21:26
  • @Don'tPanic I tried to use the CSS color nth child method but since this is an internal email server it's not utilizing it. So the basic approach is best. Thanks though! – doolittlet1 Nov 15 '18 at 21:26
  • Oh, sorry, I missed that it was an email. email HTML is crazy. I've only had to work with it once and it was a learning experience that I've managed to avoid since then. Good luck! ;-) – Don't Panic Nov 15 '18 at 21:30

1 Answers1

1

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>";
}
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55