Zerpex Posted March 13, 2012 Share Posted March 13, 2012 Hi, I'm a little confused about the ternary operator, and how I should use it in following example. I have a list of, form radio's, where I want to check, if the $_GET['type'] etc is set to 'typeAll' the radio with the value 'typeAll' should be checked, else it shouldn't be checked. This is what I have currently: <ul> <li><strong>Vælg type</strong></li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="type" value="typeAll" />Alle Typer</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="type" value="typePerson" />Personbiler</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="type" value="typeVare" />Varebiler</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="type" value="typeMotor" />Motorcykler</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="type" value="typeTrailer" />Trailer</li> </ul> <ul> <li><strong>Vælg mærke</strong></li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> checked type="radio" name="brand" value="brandAll" />Alle mærker</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandSuzuki" />Suzuki</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandYamaha" />Yamaha</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandVW" />VW</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandBMW" />BMW</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandPeugoet" />Peugoet</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="brand" value="brandMercedes" />Mercedes</li> </ul> <ul> <li><strong>Vælg pris</strong></li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> checked type="radio" name="price" value="priceAll" />Alle priser</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="price" value="price0-25" />0-25.000 DKK</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="price" value="price25-50" />25.000-50.000 DKK</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="price" value="price50-100" />50.000-100.000 DKK</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="price" value="price100-200" />100.000-200.000 DKK</li> <li><input <?=($_GET['type'] == 'value' ? 'checked' : '');?> type="radio" name="price" value="price200p" />over 200.000 DKK</li> </ul> <ul> I can manually write in the value in the ternary thingy, because I'm going to connect the inputs to a DB very soon! Best regards, Lucas R. Quote Link to comment https://forums.phpfreaks.com/topic/258812-php-ternary-operator-howto/ Share on other sites More sharing options...
trq Posted March 13, 2012 Share Posted March 13, 2012 Your checking for the string 'value' in all of your comparisons. Quote Link to comment https://forums.phpfreaks.com/topic/258812-php-ternary-operator-howto/#findComment-1326744 Share on other sites More sharing options...
RussellReal Posted March 13, 2012 Share Posted March 13, 2012 <input type="text" name="type" value="typeAll"<? echo ($_GET['type'] == 'typeAll') ? ' checked="checked"':''; ?> /> Quote Link to comment https://forums.phpfreaks.com/topic/258812-php-ternary-operator-howto/#findComment-1326745 Share on other sites More sharing options...
Zerpex Posted March 13, 2012 Author Share Posted March 13, 2012 Your checking for the string 'value' in all of your comparisons. Yep thorpe, It was just my copy/pasting, with code, I know that I had value for all my comparsions! Thank you anyway guys! Quote Link to comment https://forums.phpfreaks.com/topic/258812-php-ternary-operator-howto/#findComment-1326762 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.