Jump to content

Using $row and changing values


rotour

Recommended Posts

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!

Link to comment
Share on other sites

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!

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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'
Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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!

 

 

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.