Jump to content

Recommended Posts

I'm trying to right this code

 

 <option <?php echo $pageName == 'Home' ? 'selected="selected"' : ''; ?>>Home</option> 

 

In PHP

 

Here is my attempt I know it's wrong but I can't get it to work.

 

echo "<option> {$row["menuName"]} " == " {$row["menuName"]} " ? " 'selected=\"selected\"'" : " '' > {$row["menuName"]}</option>";

 

Instead of the == 'home' i want to echo the menuName and the same goes for at the end of the >

 

In the html, in the source code this is all I get

 

<select name="menuName" value="options">




		 '' </option> '' </option>            </select>

 

Thanks for any help.

Link to comment
https://forums.phpfreaks.com/topic/226254-trying-to-write-a-select-box-in-php/
Share on other sites

You want to turn that inline statement into a variable you can manipulate or echo at will?

 

Very simple;

 

First we move the echo to the front (as you have done).

Remove any php tags (which you have done).

 

Then what you made the mistake with was the "concatenation", ie, joining two variables or strings together.

All we do is find any code that seems dynamic, like the "Ternary Conditional" that tests if pagename is equal to "home":

 

Generally, "Ternary conditionals" are written like this:

$var = ($condition)? true : false;

 

So putting this into an echo statement:

echo "<option ".($pageName == 'Home') ? 'selected="selected"' : ''.">Home</option>

 

hope this helps

To expand on that, I find it prefereable to set the selected value independantly of the echo statement. Makes the code cleaner and more readable. Your original code doesn't state the value that the options are being checked against to determine the selected value. ChemicalBliss used $pageName in his example, you will need to determine what that value is and set it accordingly.

 

Anyway, here is my take (assuming this is performed in a loop from a DB query)

<?php

$menuOptions = '';
while($row = mysql_fetch_assoc($result))
{
    $selected = ($row["menuName"]==$pageName) ? ' selected="selected"' : '';
    $menuOptions .= "<option value=\"{$row["menuName"]}\"{$selected}>{$row["menuName"]}</option>\n";
}
?>
<select name="menuName" value="options">
<?php echo $menuOptions; ?>
</select>

That sort of helps but the

 

<option <?php echo $pageName == 'Home' ? 'selected="selected"' : ''; ?>>Home</option> 

  is what I used for anothe site adding the values in manually.

 

On this new site they will be added on the fly from the database.  So 'home' can't manually be typed in.

 

If I do

 <option> {$row["menuName"]} </option>"; 

  That will print <option>Home</option>

 

So now I need to convert

 

 <option <?php echo $pageName == 'Home' ? 'selected="selected"' : ''; ?>>Home</option>

 

Into what I attempted

 

echo "<option> {$row["menuName"]} " == " {$row["menuName"]} " ? " 'selected=\"selected\"'" : " '' > {$row["menuName"]}</option>";

 

 

But with the correct PHP if that makes sense?

Take a look at what you just said :P.

 

You want the exact functionality of

echo "<option ".($pageName == 'Home') ? 'selected="selected"' : ''.">Home</option>";

 

But you need the condition to be dynamic, very simple again, change the "hard-coded" value into a variable like so:

echo "<option ".($pageName == $row["menuName"]) ? 'selected="selected"' : ''.">Home</option>";

 

As mjdamato stated;

a) $pageName wil have to be set (Where are you getting the current page from, or the page you want to be the default - is it a config setting?).

b) It is easier to read to not use an echo straight away but to use a variable or array as he has done in his example.

 

hope this helps

mjdamato example works but in the source code it comes out as

<select name="menuName" value="options">
<option value="Home">Home</option>
<option value="About">About</option>
</select>

 

How can we make that so it says

 

<select name="menuName" value="options">
<option selected="selected">Home</option>
<option value="About">About</option>
</select>

 

So the one you are on says selected?

First of all you want to start by writing valid HTML code. That is not even close. There is no value parameter for a select tag. And the selected option in your example doesn't have a value. The code we provided will do as you ask - but YOU need to determine how you are going to identify the "selected" option. As stated in our previous posts you need to set a value some how.

 

But, here is a more length code example. I will leave it to you to figure out how to implement in your specific code;

 

<?php

//Set $pageName to the value that should be pre-selected
//In this example I am hard coding the variable because I
//don't know how it should be determined in your code
$pageName = 'Home';

//Create array of values for select list - loop uses foreach
//This can also be done with a DB query and using do/while loop
// as shown in previous responses above
$optionsAry = array('Home', 'About');

//Create the HTML for the options
$menuOptions = '';
foreach($optionsAry as $optionValue)
{
    $selected = ($optionValue==$pageName) ? ' selected="selected"' : '';
    $menuOptions .= "<option value=\"{$optionValue}\"{$selected}>{$optionValue}</option>\n";
}
?>
<select name="menuName" value="options">
<?php echo $menuOptions; ?>
</select>

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.