Jump to content

PHP ternary operator howto?


Zerpex

Recommended Posts

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.

Link to comment
https://forums.phpfreaks.com/topic/258812-php-ternary-operator-howto/
Share on other sites

Archived

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

×
×
  • 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.