seany123 Posted June 29, 2011 Share Posted June 29, 2011 i have this html table <table cellpadding="2px" cellspacing='2px' border="0px" align="center"> <td width="20px" align="center" id="#">#</td> <td width="20px" align="center" id="A">A</td> <td width="20px" align="center" id="B">B</td> <td width="20px" align="center" id="C">C</td> <td width="20px" align="center" id="D">D</td> <td width="20px" align="center" id="E">E</td> <td width="20px" align="center" id="F">F</td> <td width="20px" align="center" id="G">G</td> <td width="20px" align="center" id="H">H</td> <td width="20px" align="center" id="I">I</td> <td width="20px" align="center" id="J">J</td> <td width="20px" align="center" id="K">K</td> <td width="20px" align="center" id="L">L</td> <td width="20px" align="center" id="M">M</td> <td width="20px" align="center" id="N">N</td> <td width="20px" align="center" id="O">O</td> <td width="20px" align="center" id="P">P</td> <td width="20px" align="center" id="Q">Q</td> <td width="20px" align="center" id="R">R</td> <td width="20px" align="center" id="S">S</td> <td width="20px" align="center" id="T">T</td> <td width="20px" align="center" id="U">U</td> <td width="20px" align="center" id="V">V</td> <td width="20px" align="center" id="W">W</td> <td width="20px" align="center" id="X">X</td> <td width="20px" align="center" id="Y">Y</td> <td width="20px" align="center" id="Z">Z</td> </table> the page will get a $_GET['id'] value of 0-9 OR A-Z and i was wondering if i could set the TD bgcolor on the specific td according to the $_GET['id'] value? else if not $_GET['id'] dont change anything. Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/ Share on other sites More sharing options...
fugix Posted June 29, 2011 Share Posted June 29, 2011 how are you incorporating $_GET['id'] with this table? Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236278 Share on other sites More sharing options...
cyberRobot Posted June 29, 2011 Share Posted June 29, 2011 Instead of hard coding the table column tags, you could create them with a foreach loop and the range() function: echo '<table cellpadding="2" cellspacing="2" border="0" align="center">'; foreach(range('A', 'Z') as $currLetter) { echo "<td width='20' align='center' id='$currLetter'"; if($_GET['id'] == $currLetter) { echo ' style="background-color:#F0F;"'; } echo ">$currLetter</td>"; } echo '</table>'; Note the if() statement in the middle which assigns a background color if $_GET['id'] matches the current letter being displayed. If you want to go with this type of solution, you'll just need to add a second foreach loop for the numbers. Also, the code provided is missing the open and close tag for a table row (<tr></tr>). I didn't add them since I'm not sure where you want them. Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236279 Share on other sites More sharing options...
seany123 Posted June 29, 2011 Author Share Posted June 29, 2011 Instead of hard coding the table column tags, you could create them with a foreach loop and the range() function: echo '<table cellpadding="2" cellspacing="2" border="0" align="center">'; foreach(range('A', 'Z') as $currLetter) { echo "<td width='20' align='center' id='$currLetter'"; if($_GET['id'] == $currLetter) { echo ' style="background-color:#F0F;"'; } echo ">$currLetter</td>"; } echo '</table>'; Note the if() statement in the middle which assigns a background color if $_GET['id'] matches the current letter being displayed. If you want to go with this type of solution, you'll just need to add a second foreach loop for the numbers. Also, the code provided is missing the open and close tag for a table row (<tr></tr>). I didn't add them since I'm not sure where you want them. that works brilliantly, except i have an extra td at the start: <td width="20px" align="center" id="#">#</td> Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236295 Share on other sites More sharing options...
fugix Posted June 29, 2011 Share Posted June 29, 2011 just modify the code a bit. echo '<table cellpadding="2" cellspacing="2" border="0" align="center"> <td width="20px" align="center" id="#">#</td>'; foreach(range('A', 'Z') as $currLetter) { echo "<td width='20' align='center' id='$currLetter'"; if($_GET['id'] == $currLetter) { echo ' style="background-color:#F0F;"'; } echo ">$currLetter</td>"; } echo '</table>'; Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236304 Share on other sites More sharing options...
seany123 Posted June 29, 2011 Author Share Posted June 29, 2011 just modify the code a bit. echo '<table cellpadding="2" cellspacing="2" border="0" align="center"> <td width="20px" align="center" id="#">#</td>'; foreach(range('A', 'Z') as $currLetter) { echo "<td width='20' align='center' id='$currLetter'"; if($_GET['id'] == $currLetter) { echo ' style="background-color:#F0F;"'; } echo ">$currLetter</td>"; } echo '</table>'; yes i can do that, but then when $_GET['id'] = '#' it wont change the bgcolor of that td Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236307 Share on other sites More sharing options...
cyberRobot Posted June 29, 2011 Share Posted June 29, 2011 that works brilliantly, except i have an extra td at the start: <td width="20px" align="center" id="#">#</td> You could create another if() which tests to see if $_GET['id] contains 0-9. Maybe something like this: echo '<td width="20" align="center" id="#"'; if($_GET['id'] == '0' || $_GET['id'] == '1') { echo ' style="background-color:#F0F;"'; } echo '>#</td>'; Of course, you'll need to add the rest of the number tests $_GET['id'] == '2', etc. Also, I'm sure there is a more efficient way to do the number tests. Note that the above code is untested. Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236311 Share on other sites More sharing options...
seany123 Posted June 29, 2011 Author Share Posted June 29, 2011 that works brilliantly, except i have an extra td at the start: <td width="20px" align="center" id="#">#</td> You could create another if() which tests to see if $_GET['id] contains 0-9. Maybe something like this: echo '<td width="20" align="center" id="#"'; if($_GET['id'] == '0' || $_GET['id'] == '1') { echo ' style="background-color:#F0F;"'; } echo '>#</td>'; Of course, you'll need to add the rest of the number tests $_GET['id'] == '2', etc. Also, I'm sure there is a more efficient way to do the number tests. Note that the above code is untested. thinking about it i should of been able to do that myself. but anyway. thankyou. Quote Link to comment https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/#findComment-1236331 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.