如果在一个属性内的else语句和一个If else语句中的一个属性

时间:2016-12-11 05:05:26

标签: php html css

我正在处理一个标签,我可以在其中创建一个具有数据映射高亮显示的链接图像,如何在属性中使用if else语句?并且在声明中将包含另一个属性。

这是我的代码:

<map id="ground" name="ground">
    <?php

    $sql = "SELECT * FROM stall s 
    LEFT JOIN tenant t 
    ON t.stall_id = s.stall_id
    LEFT JOIN rent r
    ON r.tenant_id = t.tenant_id 
    AND r.rent_status = 1
    WHERE s.stall_id = 1";          

    $query = $conn->prepare($sql);
    $query->execute();
    $fetch = $query->fetchAll();

    foreach ($fetch as $key => $value) { ?>
    <area id="9" shape="rect" coords="9,98,46,117" data-toggle="modal" class = "stallstyle1"  
    href="#stall_modal9" 
    value="<?php echo $value['rent_status']?>"
    <?php
    if ($value ['rent_status'] == 1) {
    data-maphilight='{"strokeColor":"008000","strokeWidth":3,"fillColor":"7CFC00","fillOpacity":0.6,"alwaysOn":true}'>
     } else {
    data-maphilight='{"strokeColor":"FF0000","strokeWidth":3,"fillColor":"FF3333","fillOpacity":0.6,"alwaysOn":true}'>
    }
    ?>

<?php } ?> 

1 个答案:

答案 0 :(得分:0)

使用三元运算符更容易理解,而不是使用if / else。因此,使用三元运算符来获取属性data-maphilight的值。然后在标记内使用它。希望如果从数据库获取值,以下代码将起作用。

    <map id="ground" name="ground">
    <?php

    $sql = "SELECT * FROM stall s 
    LEFT JOIN tenant t 
    ON t.stall_id = s.stall_id
    LEFT JOIN rent r
    ON r.tenant_id = t.tenant_id 
    AND r.rent_status = 1
    WHERE s.stall_id = 1";          

    $query = $conn->prepare($sql);
    $query->execute();
    $fetch = $query->fetchAll();

    foreach ($fetch as $key => $value){

    $maphilight= ($value['rent_status'] == 1)?'{"strokeColor":"008000","strokeWidth":3,"fillColor":"7CFC00","fillOpacity":0.6,"alwaysOn":true}':'{"strokeColor":"FF0000","strokeWidth":3,"fillColor":"FF3333","fillOpacity":0.6,"alwaysOn":true}';
     ?>
    <area id="9" shape="rect" coords="9,98,46,117" data-toggle="modal" class = "stallstyle1"  
    href="#stall_modal9" value="<?php echo $value['rent_status'];?>" data-maphilight='<?php echo $maphilight;?>'>
<?php } ?>