0

My code is like

    echo("<td>$row[Status]</td>");
    echo("<td>
    <select name=\"choose\" id=\"Choose\" onChange=\"\">
    <option value=\"Unverified\">Unverified</option>
    <option value=\"Approved\">Approve</option>
    <option value=\"Decline\">Decline</option>
    <option value=\"Pending\">Pending</option>                   
    </select>
    </td>");

Here is what I want to do if ($row[Status] == Approved) then the code should change like this

.
.
<option value=\"Approved\" Selected>Approve</option>
.
.

The simplest way would be to create a simple if condition for all but there must be smarter way to do it. Any ideas?

I would prefer to keep it in php itself. And if you are going use a bit more complicated symbols like a => b Please explain why it is being used and what it does.

3 Answers 3

1

create an array of the possible values, and then create the options in the loop. that way, you only have to write one if condition that matches the current to the desired (here, approved) value

Sign up to request clarification or add additional context in comments.

2 Comments

if it is not possible in php can we do it using javascript?
Thanks to your Idea I found the answer.
0

Try this! It'll loop through the whole array, and select the selected option when reached. Is this what you were looking for?

(Hasn't been tested)

echo ("<select name=\"choose\" id=\"Choose\" onChange=\"\">");

$sel["Unverified"] = "Unverified";
$sel["Approved"] = "Approve";
$sel["Decline"] = "Decline";
$sel["Pending"] = "Pending";

$selected = "Approved";

foreach($sel as $key => $value) {
    if ($selected == $key) {
        echo ("<option value=\"$key\">$value</option>");
    }
    else {
        echo ("<option value=\"$key\" selected>$value</option>");
    }
}

echo ("</select>");

?>

3 Comments

Thanks, Can you tell what is => and how it is used in php.. I see it popping up here and there but I have no clue what its for and how to use it. And how did u get &loop and &key for the loop????
Oops, sorry! "$loop" was a mistake. I've edited it. Anyway... I think this can explain it better then I can: tizag.com/phpT/foreach.php
Thank you... that => been driving me nuts
0
    echo("<td>$row[Status]</td>");
    $search= "$row[Status]\"";
    $replace= "$row[status]\" Selected";
    $subject="<td>
    <select name=\"choose\" id=\"Reason\" onChange=\"\">
         <option value=\"Unverified\">Unverified</option>
         <option value=\"Approved\">Approve</option>
         <option value=\"Decline\">Decline</option>
         <option value=\"Pending\">Pending</option>                     
         </select>
         </td>";
$subject=str_replace($search, $replace, $subject);
echo $subject;

I used the above and got my desired result... wasted an hour not knowing that str_replace would return the edited value and not replace $subject. Thanks everyone.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.