如何将href纳入变量并将其赋予href =“ $ vrable so”

时间:2019-01-02 06:37:05

标签: php html

<?php if (!empty($this->highlitsrightside)) {   foreach ($this->highlitsrightside as $rightsidehlights){ if (!empty($rightsidehlights['highimage1']) || !empty($rightsidehlights['highimage2']) || !empty($rightsidehlights['highimage3']) || !empty($rightsidehlights['highimage4']) || !empty($rightsidehlights['highimage5'])) { ?>
           <?php $hrf1 = "<?php echo SERVICES . 'righthighlights/?highrid=' . $rightsidehlights['highlightid'];?>"; ?>

              <?php } else { $hrf1 = '#';} ?>
          <?php if (!empty($rightsidehlights['icontext'])) {?>

            <a class="off_clr_text link_chn_lo" href="$hrf1" data-options="" tabindex="0"><i class="<?php echo $rightsidehlights['icontext']; ?>" ></i> <?php echo $rightsidehlights['highlightname']; ?></a>
            <?php  } else {?> 

<a class="off_clr_text link_chn_lo" href="$hrf" data-options="" tabindex="0"> <img style=" display: inline-block;" src="<?php echo UPLOADS . 'images/highlights_icon/' . $rightsidehlights['highlighticon'] ?>"width="30" height="16"  ></span></i> <?php echo $rightsidehlights['highlightname']; ?></a>

            <?php } }?> 

enter image description here

3 个答案:

答案 0 :(得分:0)

您需要像在代码中那样在HTML内使用PHP标记。 例子

<a href="<?=$hrf1?>" > </a>

下面是您的代码

<?php 
    if (!empty($this->highlitsrightside)) {   
        foreach ($this->highlitsrightside as $rightsidehlights)
        { 

        if (!empty($rightsidehlights['highimage1']) || !empty($rightsidehlights['highimage2']) || !empty($rightsidehlights['highimage3']) || !empty($rightsidehlights['highimage4']) || !empty($rightsidehlights['highimage5'])) { ?>
               <?php $hrf1 = "www.google.com"; ?>

                  <?php } else { $hrf1 = '#';} ?>
              <?php if (!empty($rightsidehlights['icontext'])) {?>

                <a class="off_clr_text link_chn_lo" href="<?=$hrf1?>" data-options="" tabindex="0"><i class="<?php echo $rightsidehlights['icontext']; ?>" ></i> <?php echo $rightsidehlights['highlightname']; ?></a>
                <?php  } else {?> 

                <a class="off_clr_text link_chn_lo" href="<?=$hrf1?>" data-options="" tabindex="0"> <img style=" display: inline-block;" src="<?php echo UPLOADS . 'images/highlights_icon/' . $rightsidehlights['highlighticon'] ?>"width="30" height="16"  ></span></i> <?php echo $rightsidehlights['highlightname']; ?></a>

                <?php
                 }
         }


    } ?>  

答案 1 :(得分:0)

您没有正确使用open和close PHP标记。

    <?php 
    if (!empty($this->highlitsrightside)) {   
        foreach ($this->highlitsrightside as $rightsidehlights)
        { 

        if (!empty($rightsidehlights['highimage1']) || !empty($rightsidehlights['highimage2']) || !empty($rightsidehlights['highimage3']) || !empty($rightsidehlights['highimage4']) || !empty($rightsidehlights['highimage5'])) {
            $hrf1 = "www.google.com";
        } else { 
            $hrf1 = '#';
        }
        if (!empty($rightsidehlights['icontext'])) {
            echo '';
    ?>
            <a class="off_clr_text link_chn_lo" href="<?php echo $hrf1; ?>" data-options="" tabindex="0"><i class="<?php echo $rightsidehlights['icontext']; ?>" ></i> <?php echo $rightsidehlights['highlightname']; ?></a>
    <?php  
        } else {?> 
            <a class="off_clr_text link_chn_lo" href="<?php echo $hrf1; ?>" data-options="" tabindex="0"> <img style=" display: inline-block;" src="<?php echo UPLOADS . 'images/highlights_icon/' . $rightsidehlights['highlighticon'] ?>"width="30" height="16"  ></span></i> <?php echo $rightsidehlights['highlightname']; ?></a>
    <?php
                 }
         }

    } ?> 

要消除混乱,此脚本可以很简单:

<?php 
if (!empty($this->highlitsrightside)) {   
    foreach ($this->highlitsrightside as $rightsidehlights){ 
        if (!empty($rightsidehlights['highimage1']) || !empty($rightsidehlights['highimage2']) || !empty($rightsidehlights['highimage3']) || !empty($rightsidehlights['highimage4']) || !empty($rightsidehlights['highimage5'])) {
            $hrf1 = "www.google.com";
        } else { 
            $hrf1 = '#';
        }
        if (!empty($rightsidehlights['icontext'])) {
            echo '<a class="off_clr_text link_chn_lo" href="'.$hrf1.'" data-options="" tabindex="0"><i class="'.$rightsidehlights['icontext'].'" ></i>'.$rightsidehlights['highlightname'].'</a>';
        }else{
            echo '<a class="off_clr_text link_chn_lo" href="'.$hrf1.'" data-options="" tabindex="0"> <img style=" display: inline-block;" src="'.UPLOADS . 'images/highlights_icon/' . $rightsidehlights['highlighticon'].'"width="30" height="16"  ></span></i> '.$rightsidehlights['highlightname'].'</a>'; 
        }
    }
} 

?> 

答案 2 :(得分:0)

在下面尝试

<?php if (!empty($this->highlitsrightside)) {   foreach ($this->highlitsrightside as $rightsidehlights){ if (!empty($rightsidehlights['highimage1']) || !empty($rightsidehlights['highimage2']) || !empty($rightsidehlights['highimage3']) || !empty($rightsidehlights['highimage4']) || !empty($rightsidehlights['highimage5'])) { ?>

        <?php  $hrf1 = SERVICES.'righthighlights/?highrid='.$rightsidehlights['highlightid']; ?>

              <?php } else { $hrf1 = '#';} ?>
          <?php if (!empty($rightsidehlights['icontext'])) {?>

            <a class="off_clr_text link_chn_lo" href="<?php echo $hrf1; ?>" data-options="" tabindex="0"><i class="<?php echo $rightsidehlights['icontext']; ?>" ></i> <?php echo $rightsidehlights['highlightname']; ?></a>
            <?php  } else {?> 

<a class="off_clr_text link_chn_lo" href="<?php echo $hrf; ?>" data-options="" tabindex="0"> <img style=" display: inline-block;" src="<?php echo UPLOADS . 'images/highlights_icon/' . $rightsidehlights['highlighticon'] ?>"width="30" height="16"  ></span></i> <?php echo $rightsidehlights['highlightname']; ?></a>

            <?php } }  ?>