Jump to content


Photo

Correct syntax


  • Please log in to reply
5 replies to this topic

#1 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 19 September 2006 - 06:02 AM

Hi,

Whats the correct syntax to merge:

$data1['maincat_id']=='$maincat' ? 'selected' : ''


into:

echo '<option value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>'



echo '<option $data1['maincat_id']=='$maincat' ? 'selected' : '' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';


Have tried a few ways and keep gettin errors.


Thanks



#2 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 September 2006 - 07:13 AM

Whats the correct syntax to merge


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
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#3 btherl

btherl
  • Staff Alumni
  • Advanced Member
  • 3,893 posts
  • LocationAustralia

Posted 19 September 2006 - 07:18 AM

I think he's after this:

echo '<option ' . ($data1['maincat_id']==$maincat ? 'selected' : '') . ' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';

I prefer this style though.. less complexity in each line :)

$selected = $data1['maincat_id'] == $maincat ? 'selected' : '';
echo '<option ' . $selected . ' value="'.$row['maincat_id'].'">'.$row['maincat_name'].'</option>';


#4 HuggieBear

HuggieBear
  • Members
  • PipPipPip
  • Advanced Member
  • 1,899 posts
  • LocationEngland, UK

Posted 19 September 2006 - 07:24 AM

That's what I thought, I'm not a fan of doing it inline either  ;)

Huggie
Advice to MySQL users: Get phpMyAdmin and test your queries work there first, take half the hassle out of diagnosis, also check the reserved words list.

Links: PHP Docs :: RegEx's :: MySQL :: DevGuru :: w3schools

#5 bob_the _builder

bob_the _builder
  • Members
  • PipPipPip
  • Advanced Member
  • 207 posts

Posted 19 September 2006 - 08:37 AM

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

<scrip language=JavaScript> 
function reload(form) 
{ 
var val=form.maincat.options[form.maincat.options.selectedIndex].value; 
self.location='chained.php?maincat=' + val ; 
}
</scrip>

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>';

Can it be writen better than that?


Thanks

#6 AndyB

AndyB
  • Staff Alumni
  • Advanced Member
  • 5,465 posts
  • LocationToronto

Posted 19 September 2006 - 10:35 AM

minor observation ... the full/correct implementation of 'selected' is below:

<option value="whatever" selected="selected" ...

Legend has it that reading the manual never killed anyone.
My site




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users