Jump to content

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

Link to comment
https://forums.phpfreaks.com/topic/21241-correct-syntax/
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
Link to comment
https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94486
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]
Link to comment
https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94488
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
Link to comment
https://forums.phpfreaks.com/topic/21241-correct-syntax/#findComment-94505
Share on other sites

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.