Jump to content

Archived

This topic is now archived and is closed to further replies.

bob_the _builder

Correct syntax

Recommended Posts

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

Share this post


Link to post
Share on other sites
[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?

Regards
Huggie

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
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

Share this post


Link to post
Share on other sites
minor observation ... the full/correct implementation of 'selected' is below:

[code]<option value="whatever" selected="selected" ...[/code]

Share this post


Link to post
Share on other sites

×

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.