rotour Posted November 6, 2010 Share Posted November 6, 2010 Hi, I'm using the following code: <td>$row[color]</td> and the possible values for this field are 1, 2 and 3. But I want the return to be red for 1, blue for 2, and green for 3. How do I write it so that instead of getting a row of 1's, 2's and 3's, I change those numbers to their corresponding colors? Thanks! Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/ Share on other sites More sharing options...
Pikachu2000 Posted November 6, 2010 Share Posted November 6, 2010 This will work, and will not echo anything if the value isn't 1, 2, or 3. echo '<td>'; echo $row['color'] == 1 ? 'red' : $row['color'] == 2 ? 'blue' : $row['color'] == 3 ? 'blue' : ''; echo '</td>'; Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1130957 Share on other sites More sharing options...
rotour Posted November 6, 2010 Author Share Posted November 6, 2010 Thanks so much for the reply--but for some reason it's incorrectly operating as if the value is always 3, which I'm sure it isn't, as I have other tables pulling in the same data, displaying that value correctly. Here's how I've implemented it below (the variable I'm using it on is ws_status): while($row = mysql_fetch_array($rs)) { echo '<tr>'; echo'<td width=\"150\">'.$row[ws_name].'</td>'; echo '<td>'; echo $row['ws_status'] == 1 ? 'one' : $row['ws_status'] == 2 ? 'two' : $row['ws_status'] == 3 ? 'three' :''; echo'</td>'; echo'<td>'.$row[ws_id].'</td>'; echo'<td>'.$row[ws_acct_mgr].'</td>'; echo'<td>'.$row[second_acct_mgr].'</td>'; echo'<td>'.number_format($row[fb_start]).'</td>'; echo'<td>'.number_format($row[tw_start]).'</td>'; echo'<td>'.number_format($row[ms_start]).'</td>'; echo'<td>'.number_format($row[yt_start]).'</td>'; echo'<td>'.number_format($row[web_start]).'</td>'; echo'<td>'.$row[agent].'</td>'; echo'<td>'.$row[ws_mobile_type].'</td>'; echo'<td>'.$row[carrier].'</td>'; echo'<td>'.$row[phone_model].'</td>'; echo'</tr>'; } Any ideas? Thank you again! Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1130966 Share on other sites More sharing options...
rotour Posted November 6, 2010 Author Share Posted November 6, 2010 By the way, does your solution still work if it's in a while loop? Is that what's breaking things? Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1130969 Share on other sites More sharing options...
Lexicon Posted November 6, 2010 Share Posted November 6, 2010 does it help by changing it from $row[word] to $row['word'] by adding the apostrophe things Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1130973 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2010 Share Posted November 6, 2010 Using nested Ternary operators does not do what you think. It is usually better to use a look up table (array) to map values - $lookup[1] = 'red'; $lookup[2] = 'blue'; $lookup[3] = 'green'; //... additional key/value pairs as needed.... echo '<td>' . isset($lookup[$row['color']]) ? $lookup[$row['color']] : '' . '</td>'; Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1130976 Share on other sites More sharing options...
Pikachu2000 Posted November 6, 2010 Share Posted November 6, 2010 I don't normally nest ternaries, and I didn't test that as thoroughly as I should have . . . Sorry about that, PFM is right. Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1131029 Share on other sites More sharing options...
Adam Posted November 6, 2010 Share Posted November 6, 2010 does it help by changing it from $row[word] to $row['word'] by adding the apostrophe things In answer to this by the way, not quoting the index will make PHP think you're referencing a constant. If it's not found it will assume the string value, but if you have notice level errors enabled you'll get a notice like: Notice: Use of undefined constant foo - assumed 'foo' Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1131051 Share on other sites More sharing options...
rotour Posted November 6, 2010 Author Share Posted November 6, 2010 Thanks so much, PFM--that did the trick. I did have to change one thing, though, which I thought I should mention here. For some reason, when I posted exactly what you posted, the <td> and </td> preceding and following the isset function did not render at all. I put the <td> in the echo line before and the </td> in the echo line after and it worked like a charm. Here's the snippet: echo'<td width=\"150\">'.$row[ws_name].'</td><td>'; echo isset($lookup[$row['ws_status']]) ? $lookup[$row['ws_status']] : ''; echo'</td><td>'.$row[ws_id].'</td>'; Just out of curiosity, any idea why this happened? In any case, thanks again for the solution, and all for chiming in and sparing me a good deal of frustration. Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1131076 Share on other sites More sharing options...
rotour Posted November 6, 2010 Author Share Posted November 6, 2010 One further question: if I wanted to drop a bgd color into the <td> based on its contents, like so that if ws_status below was in a different colored table cells for values 1, 2 and 3--how would I do that? echo'<td width=\"150\">'.$row[ws_name].'</td><td>'; echo isset($lookup[$row['ws_status']]) ? $lookup[$row['ws_status']] : ''; echo'</td><td>'.$row[ws_id].'</td>'; Maybe the $row function isn't the best method of doing what I'm trying to do. I'm not tied to it if there's a better way. Thanks again! Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1131081 Share on other sites More sharing options...
PFMaBiSmAd Posted November 6, 2010 Share Posted November 6, 2010 Just out of curiosity, any idea why this happened? LOL, I just looked at the 'view source' of what that code produces and got the same missing data. It's likely that the strings concatenated with the ternary operator became part of the ternary statement (the leading <td> string became part of the condition and the trailing </td> string became part of the false/empty string) and only the result of all of that was returned and echoed. In fact I just tested this by using an invalid index number with the original code. You get an invalid index error because the '<td>' string concatenated with the isset() condition results in a true value and the $lookup[$row['color']] is evaluated with a nonexistent index and produces an error. You can use echo with commas (instead of concatenation) to get it to work properly as that separates the pieces into separate operands to the echo statement and delineates where the ternary operator starts and stops - echo '<td>',isset($lookup[$row['color']]) ? $lookup[$row['color']] : '','</td>'; Quote Link to comment https://forums.phpfreaks.com/topic/217913-using-row-and-changing-values/#findComment-1131090 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.