Jump to content

Need help with selected item in a Select List


Garling
Go to solution Solved by Ch0cu3r,

Recommended Posts

I have a function that reads from a database and list to a webpage the results. What I want is to have the selected item on a list box show when a PHP variable is found, here is my code I have so far:

<?php
	
	while ($row = db2_fetch_assoc($stmt)){
		
			$WHMARK = $row['WHMARK']; 
			$WHHOTEL = $row['WHHOTEL']; 
			$WHSTATUS = $row['WHSTATUS'];
			if ($WHSTATUS == 1)
			{
				$Status = "Active";
			}
			else{
				$Status = "In-Active";
			}
  			echo '<tr>';
    		echo '<td>'.$WHMARK.'</td>';
    		echo '<td>'.$WHHOTEL.'</td>';
    		echo '<td>'.$Status.'</td>';
    		echo '<td>';
      		echo '<label>';
        	echo '	<select name="hotelActive" id="hotelActive">';
        	echo '	<option value="1"' <?php if($WHSTATUS == 1) ? 'selected="selected">';?>' echo '>Active</option>';       	
		echo '	<option value="0"' <?php if($WHSTATUS == 0) ? 'selected="selected">';?>' echo '>In-Active</option>';
        	echo '	</select>';
      		echo '</label>';
    		echo '</td>';
  		echo '</tr>';
  	}
?>  

I need option value 1 selected if the variable '$Status' is 1 and  option value 2 selected if the variable '$Status' is 2. This is PHP embedded in a HTML table. 

Link to comment
Share on other sites

Also this is incorrect syntax

        	echo '	<option value="1"' <?php if($WHSTATUS == 1) ? 'selected="selected">';?>' echo '>Active</option>';       	
		echo '	<option value="0"' <?php if($WHSTATUS == 0) ? 'selected="selected">';?>' echo '>In-Active</option>';

It should be

		echo ' <option value="1"' . (($WHSTATUS == 1) ? ' selected="selected"' ? '') . '>Active</option>';       
		echo ' <option value="0"' . (($WHSTATUS == 0) ? ' selected="selected"' ? '') . '>In-Active</option>';
Edited by Ch0cu3r
Link to comment
Share on other sites

  • Solution

Ch0cu3r

 

When I change my code to this, I am now getting an Internal Server Error unless I remove the php code. 

 

Sorry I had a typo. My code should of been

		echo ' <option value="1"' . (($WHSTATUS == 1) ? ' selected="selected"' : '') . '>Active</option>';       
		echo ' <option value="0"' . (($WHSTATUS == 0) ? ' selected="selected"' : '') . '>In-Active</option>';
Link to comment
Share on other sites

I think you need something like this:

        echo ' <option value="1"' ; if ($WHSTATUS == 1)  echo ' selected="selected"'; else echo '' ; echo '>Active</option>';       
	echo ' <option value="0"' ; if ($WHSTATUS == 0)  echo ' selected="selected"' ; else echo ''; echo '>In-Active</option>';
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.