Populating Select Option Drop-List With ENUM Values from Database

Hello.

As the subject stated, I am in a middle of nowhere as I stumbled upon an issue. It is a bit tricky (for me, yes lol) as I have been wandering around the Internet without any similar issue as mine.
I’ve made tables joint which combine two (2) tables for data display, however, I created an ‘ENUM’ field for my work and it is pretty hard for me to populate it inside a select drop-list.

The goal is to, display the current selection value from the database (NONE) and the other one would be (PAID). Initially, each row will hold (NONE) value but I want to ensure that changes can be made anytime it is needed. Basically, an interchangeable situation.

I made a fairly stupid and irrelevant logic in the code (refer below). I want to know a better way to do it.
Here’s the code;
[php]while( $rowresult = mysqli_fetch_array( $query ) )
{
echo “

”;
echo “”.$rowresult[‘id’]."";
echo “”.$rowresult[‘username’]."";
echo “”.$rowresult[‘useric’]."";
echo “”.$rowresult[‘userphone’]."";
echo “”.$rowresult[‘useremail’]."";
echo “”;
if( $rowresult['pay'] == 'NONE' )
{
	echo "<td><select name='nonepay'>";
          echo "<option value='".$rowresult['pay']."'>NONE</option>";
	echo "<option value='PAID'>PAID</option></select></td>";
}
else
{
	echo "<td><select name='paidpayment'>";
          echo "<option value='".$rowresult['pay']."'>PAID</option>";
	echo "<option value='NONE'>NONE</option></select></td>";
}
echo "</form>";
echo "</tr>";

}[/php]

Thank you. ::slight_smile:

You only want ONE dropdown, correct? And it should show whether they have paid or not, and be able to update that record?

Yes, the drop-list willl populate the remaining ‘ENUM’ option.

If your status is ‘PAID’ then the remaining option would be ‘NONE’ and vice versa. :wink:

[php]$selected = $rowresult[‘pay’] == ‘PAID’ ? ‘selected=“selected”’ : ‘’;

$select = <<<EOD

NONE
<option value=‘PAID’ {$selected}>PAID

EOD;[/php]

Displays a single dropdown. Defaults to NONE, if PAID is in the record, then that is the selected option.

Jeez. I forgot how useful ternary operator is, this is the second question that I asked that actually pretty easy to be solved just by using comparison method of ternary operator.

I felt so dumb but thank you for the solution. From now on, I wanted to apply ternary operation for each comparison just to make me stick to the fact that ternary operator is so useful.

Thanks. Karma given… A kiss would be for you to if I can meet you in person. :-*

[EDIT]

Well, as I received error because of EOD so I removed it and use old-fashioned of echoing each row, lol.

Don’t worry, I am going through the manual to understand that at the moment. Thanks for delivering the knowledge though. ;D

Sponsor our Newsletter | Privacy Policy | Terms of Service