php loop array through separate array's while loop










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.










share|improve this question






















  • 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. stackoverflow.com/questions/3084261/… (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















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.










share|improve this question






















  • 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. stackoverflow.com/questions/3084261/… (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













0












0








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.










share|improve this question














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.







php mysql arrays while-loop






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 21:07









doolittlet1doolittlet1

384




384












  • 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. stackoverflow.com/questions/3084261/… (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

















  • 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. stackoverflow.com/questions/3084261/… (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
















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





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





Sadly my company is still using v5.2.6

– doolittlet1
Nov 15 '18 at 21:16




1




1





That's criminally irresponsible. Regardless, it still means you have access to PDO.

– miken32
Nov 15 '18 at 21:18





That's criminally irresponsible. Regardless, it still means you have access to PDO.

– miken32
Nov 15 '18 at 21:18




1




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 '18 at 21:18






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 '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





@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












1 Answer
1






active

oldest

votes


















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>";






share|improve this answer























  • Simple and works perfectly! Don't even need a separate function. Thanks so much!

    – doolittlet1
    Nov 15 '18 at 21:22










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327906%2fphp-loop-array-through-separate-arrays-while-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









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>";






share|improve this answer























  • Simple and works perfectly! Don't even need a separate function. Thanks so much!

    – doolittlet1
    Nov 15 '18 at 21:22















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>";






share|improve this answer























  • Simple and works perfectly! Don't even need a separate function. Thanks so much!

    – doolittlet1
    Nov 15 '18 at 21:22













1












1








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>";






share|improve this answer













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>";







share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 15 '18 at 21:14









Nigel RenNigel Ren

28k61934




28k61934












  • Simple and works perfectly! Don't even need a separate function. Thanks so much!

    – doolittlet1
    Nov 15 '18 at 21:22

















  • Simple and works perfectly! Don't even need a separate function. Thanks so much!

    – doolittlet1
    Nov 15 '18 at 21:22
















Simple and works perfectly! Don't even need a separate function. Thanks so much!

– doolittlet1
Nov 15 '18 at 21:22





Simple and works perfectly! Don't even need a separate function. Thanks so much!

– doolittlet1
Nov 15 '18 at 21:22



















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53327906%2fphp-loop-array-through-separate-arrays-while-loop%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

ReactJS Fetched API data displays live - need Data displayed static

政党