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
bob_the _builder
Forum Contributor
Posts: 131 Joined: Sat Aug 28, 2004 12:25 am
Post
by bob_the _builder » Mon Sep 18, 2006 8:39 pm
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
feyd
Neighborhood Spidermoddy
Posts: 31559 Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA
Post
by feyd » Mon Sep 18, 2006 8:42 pm
Looks like you're missing a "'." and possibly a few parens.
ibbo
Forum Commoner
Posts: 51 Joined: Tue Sep 19, 2006 6:20 am
Post
by ibbo » Tue Sep 19, 2006 7:00 am
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
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Tue Sep 19, 2006 7:40 am
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 » Tue Sep 19, 2006 7:40 am
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?
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Tue Sep 19, 2006 11:12 am
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 » Mon Sep 25, 2006 3:21 am
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
CoderGoblin
DevNet Resident
Posts: 1425 Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany
Post
by CoderGoblin » Mon Sep 25, 2006 5:04 am
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"';
RobertGonzalez
Site Administrator
Posts: 14293 Joined: Tue Sep 09, 2003 6:04 pm
Location: Fremont, CA, USA
Post
by RobertGonzalez » Thu Sep 28, 2006 3:58 am
Those brackets were correct. Ternary syntax is:
Code: Select all
$var = (if_this) ? do_this : do_else;
onion2k
Jedi Mod
Posts: 5263 Joined: Tue Dec 21, 2004 5:03 pm
Location: usrlab.com
Post
by onion2k » Thu Sep 28, 2006 5:15 am
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>";