Jump to content

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/240692-while-_getid-change-td-bgcolor/
Share on other sites

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.

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>

 

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

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

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.

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.

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.