bob_the _builder Posted September 19, 2006 Share Posted September 19, 2006 Hi,Whats the correct syntax to merge:[code=php:0]$data1['maincat_id']=='$maincat' ? 'selected' : ''[/code]into:[code=php:0]echo '<option value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>'[/code][code=php:0]echo '<option $data1['maincat_id']=='$maincat' ? 'selected' : '' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';[/code]Have tried a few ways and keep gettin errors.Thanks Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/ Share on other sites More sharing options...
HuggieBear Posted September 19, 2006 Share Posted September 19, 2006 [quote]Whats the correct syntax to merge[/quote]I'm not sure I understand the question. What do you have and what do you want to achieve?You want to say if $data1['maincat_id'] is equal to $maincat then to make it the selected option?RegardsHuggie Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94486 Share on other sites More sharing options...
btherl Posted September 19, 2006 Share Posted September 19, 2006 I think he's after this:[code]echo '<option ' . ($data1['maincat_id']==$maincat ? 'selected' : '') . ' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';[/code]I prefer this style though.. less complexity in each line :)[code]$selected = $data1['maincat_id'] == $maincat ? 'selected' : '';echo '<option ' . $selected . ' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';[/code] Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94488 Share on other sites More sharing options...
HuggieBear Posted September 19, 2006 Share Posted September 19, 2006 That's what I thought, I'm not a fan of doing it inline either ;)Huggie Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94492 Share on other sites More sharing options...
bob_the _builder Posted September 19, 2006 Author Share Posted September 19, 2006 Hi,Nice never thought of doing it like that .. thanks!Trying to create a chained list/menu, not having much luck using the selected option on the 2nd list/menu[code]<scrip language=JavaScript> function reload(form) { var val=form.maincat.options[form.maincat.options.selectedIndex].value; self.location='chained.php?maincat=' + val ; }</scrip>[/code][code=php:0]require_once 'db.php';$maincat = $_GET['maincat'];$sql=mysql_query("SELECT * FROM gallery_maincat ORDER BY maincat_name ASC"); $sql2=mysql_query("SELECT * FROM gallery_subcat WHERE maincat_id = $maincat ORDER BY subcat_name ASC");echo '<form action="" name="subcat" method="post">';echo '<select name="maincat" onchange="reload(this.form)">';echo '<option value="">Selection</option>';while($row = mysql_fetch_array($sql)) { $selected = $row['maincat_id'] == $maincat ? 'selected' : '';echo '<option ' . $selected . ' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';}echo '</select>';echo '<select name="subcat" onchange="reload(this.form)">';echo '<option value="">Selection</option>';while($row2 = mysql_fetch_array($sql2)) { $selected = $row2['subcat_id'] == $row2['subcat_id'] ? 'selected' : '';echo '<option ' . $selected . ' value="'.$row2['subcat_id'].'">'.$row2['subcat_name'].'</option>';}echo '</select>';echo '</form>';[/code]Can it be writen better than that?Thanks Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94505 Share on other sites More sharing options...
AndyB Posted September 19, 2006 Share Posted September 19, 2006 minor observation ... the full/correct implementation of 'selected' is below:[code]<option value="whatever" selected="selected" ...[/code] Link to comment https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94532 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.