Correct syntax

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Correct syntax

Post by bob_the _builder »

Hi,

Whats the correct format for:

Code: Select all

echo '<option '.$row['maincat_id'].' == $maincat ? 'selected' : '' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';
Can't seem to get it right


Thanks
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post by feyd »

Looks like you're missing a "'." and possibly a few parens.
User avatar
ibbo
Forum Commoner
Posts: 51
Joined: Tue Sep 19, 2006 6:20 am

Post by ibbo »

Whats the output you are after.

Using the shortcut $var == $other ? 'selected' : ''; wont quite work how your wanting it to.

Try

Code: Select all

$string = '<option';
$var == $other ? $string .= 'selcted': '';
$string .= "value={$row['maincat_id'}>{$row['maincat_name']}</option>'";

echo $string;
ibbo
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Try

Code: Select all

echo "<option value=\"{$row['maincat_id']}\"".($row['maincat_id'] == $maincat ? 'selected' : '').">{$row['maincat_name']}</option>";
The if is simple... (condition ? true:false)
lecram
Forum Newbie
Posts: 20
Joined: Wed Sep 06, 2006 10:12 am

Post by lecram »

Code: Select all

echo '<option ';
if ($row['maincat_id'] == $maincat) {
echo 'selected value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';
} else {
echo 'value="something_else_or_default">something_else_or_default</option>';
}

is that what you are going for?
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

It looks like you are looping through a set of some sort. If that is the case, you could do something like this...

Code: Select all

<?php
//Loop construct
{
    $maincat_id = $row['maincat_id'];
    $selected = ($maincat_id == $maincat) ? ' selected="selected"' : '';

    echo '<option value="' . $maincat_id . '"' . $selected . '>' . $row['maincat_name'] . '</option>';
}
?>
bob_the _builder
Forum Contributor
Posts: 131
Joined: Sat Aug 28, 2004 12:25 am

Post by bob_the _builder »

Cant get the following to work, I echoed out the variables and they contain the data needed:

Code: Select all

$selected = ($_GET['filter'] == $filter) ? ' selected="selected"' : '';

echo '<select name="filter">
        <option value="new"' . $selected . '>New</option>
        <option value="archived"' . $selected . '>Archived</option>
      </select>';

Should it be working?


Thanks
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Post by CoderGoblin »

Brackets are wrong...

Code: Select all

$selected = ($_GET['filter'] == $filter ? ' selected="selected"' : '');
should work

or use

Code: Select all

$selected='';
if ($_GET['filter']== $filter) $selected='  selected="selected"';
User avatar
RobertGonzalez
Site Administrator
Posts: 14293
Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA

Post by RobertGonzalez »

Those brackets were correct. Ternary syntax is:

Code: Select all

$var = (if_this) ? do_this : do_else;
User avatar
onion2k
Jedi Mod
Posts: 5263
Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com

Post by onion2k »

Code: Select all

echo "<select name=\"filter\">";
echo "<option value=\"new\"".(($_GET['filter']=="new")?" selected":"").">New</option>";
echo "<option value=\"archived\"".(($_GET['filter']=="archived")?" selected":"").">Archived</option>";
echo "</select>";
Post Reply