Php 为图像使用其他标题

Php 为图像使用其他标题,php,html,Php,Html,在下面的代码中,我打开了php标记,并根据ifelse()条件决定了不同的图像。我想为所选图像添加不同的标题,但无法找到解决方案。代码如下: <img title = "Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago." src="<?php

在下面的代码中,我打开了php标记,并根据ifelse()条件决定了不同的图像。我想为所选图像添加不同的标题,但无法找到解决方案。代码如下:

<img title = "Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago." 
            src="<?php 
                            if ($record->$period == 0) { echo base_url()."img/warning.png";}
                            else{

                                date("M d, Y", strtotime($record->$period));
                                $vtime = new DateTime($record->$period);             ///////////////////////
                                $today = new DateTime(); // for testing purposes            ///Calculate  Time period//
                                $diff = $today->diff($vtime);                                   ///
                                $time = $diff -> m;
                                if($time <= 4)
                                {echo base_url()."img/green.png";}
                                elseif( $time > 4 && $time <= 6)
                                {echo base_url()."img/yellow.png";}
                                elseif($time >= 6)
                                {echo base_url()."img/red.png";}
                            }
                " 
    />
$period==0){echo base_url()“img/warning.png”;}
否则{
日期(“M d,Y”,标准时间($record->$period));
$vtime=新日期时间($record->$period)///////////////////////
$today=new DateTime();//用于测试///计算时间段//
$diff=$today->diff($vtime)///
$time=$diff->m;
如果($time 4&&$time=6)
{echo base_url()“img/red.png”;}
}
" 
/>
我希望第一个条件有不同的标题。例如,如果第一个条件为真,并且显示的图像为“warning.png”。则图像标题应为“Check record”,而不是标题“last assessment submitted was…”


非常感谢您的帮助。

您可以简单地使用以下命令。只需将当前的if…else条件嵌套在title标签上的另一个if…else条件中即可

<img title = "
    <?php if($record->period==0) 
        echo "Check record"; 
    else { ?>
        Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago.
    <?php } ?>" 
            src="<?php 
                            if ($record->$period == 0) { echo base_url()."img/warning.png";}
                            else{

                                date("M d, Y", strtotime($record->$period));
                                $vtime = new DateTime($record->$period);             ///////////////////////
                                $today = new DateTime(); // for testing purposes            ///Calculate  Time period//
                                $diff = $today->diff($vtime);                                   ///
                                $time = $diff -> m;
                                if($time <= 4)
                                {echo base_url()."img/green.png";}
                                elseif( $time > 4 && $time <= 6)
                                {echo base_url()."img/yellow.png";}
                                elseif($time >= 6)
                                {echo base_url()."img/red.png";}
                            }
                " 
    />
$period==0){echo base_url()“img/warning.png”;}
否则{
日期(“M d,Y”,标准时间($record->$period));
$vtime=新日期时间($record->$period)///////////////////////
$today=new DateTime();//用于测试///计算时间段//
$diff=$today->diff($vtime)///
$time=$diff->m;
如果($time 4&&$time=6)
{echo base_url()“img/red.png”;}
}
" 
/>

您只需使用以下命令即可。只需将当前的if…else条件嵌套在title标记上的另一个if…else条件中

<img title = "
    <?php if($record->period==0) 
        echo "Check record"; 
    else { ?>
        Last assessment for this child was submitted <?php if ($time == 0){echo $time;}else{echo $time - 1;}?> Month(s) ago.
    <?php } ?>" 
            src="<?php 
                            if ($record->$period == 0) { echo base_url()."img/warning.png";}
                            else{

                                date("M d, Y", strtotime($record->$period));
                                $vtime = new DateTime($record->$period);             ///////////////////////
                                $today = new DateTime(); // for testing purposes            ///Calculate  Time period//
                                $diff = $today->diff($vtime);                                   ///
                                $time = $diff -> m;
                                if($time <= 4)
                                {echo base_url()."img/green.png";}
                                elseif( $time > 4 && $time <= 6)
                                {echo base_url()."img/yellow.png";}
                                elseif($time >= 6)
                                {echo base_url()."img/red.png";}
                            }
                " 
    />
$period==0){echo base_url()“img/warning.png”;}
否则{
日期(“M d,Y”,标准时间($record->$period));
$vtime=新日期时间($record->$period)///////////////////////
$today=new DateTime();//用于测试///计算时间段//
$diff=$today->diff($vtime)///
$time=$diff->m;
如果($time 4&&$time=6)
{echo base_url()“img/red.png”;}
}
" 
/>

您应该避免在HTML中内联太多PHP代码,以保持代码的可读性

if ($record->period === 0) {
    echo '<img src="img/warning.png" title="Warning title" />';
} else {
    // Are you sure this does what you want?
    // You probably need $record->period. (no $)
    $vtime = new DateTime($record->$period);
    $today = new DateTime();
    $diff = $today->diff($vtime);
    $time = $diff->m;
    $title = 'Last assessment for this child was submitted ' . 
        ($time === 0 ? 0 : $time-1) . 
        ' month(s) ago.';

    if ($time <= 4) {
        echo '<img src="img/green.png" title="'. $title . '" />';
    } else if ($time <= 6) {    
    // Don't need to check if it's bigger than 4, you've already checked this 
    // in the initial "if" and if that was succesful, we wouldn't be here.
        echo '<img src="img/yellow.png" title="'. $title . '" />';
    } else {
        echo '<img src="img/red.png" title="' . $title . '" />';
    }
}
if($record->period==0){
回声';
}否则{
//你确定这是你想要的吗?
//您可能需要$record->period.(无$)
$vtime=新日期时间($record->$period);
$today=新日期时间();
$diff=$today->diff($vtime);
$time=$diff->m;
$title='此孩子的上次评估已提交'。
($time==0?0:$time-1)。
“一个月前。”;

如果($time您应该避免在HTML中内联太多PHP代码,以保持代码的可读性

if ($record->period === 0) {
    echo '<img src="img/warning.png" title="Warning title" />';
} else {
    // Are you sure this does what you want?
    // You probably need $record->period. (no $)
    $vtime = new DateTime($record->$period);
    $today = new DateTime();
    $diff = $today->diff($vtime);
    $time = $diff->m;
    $title = 'Last assessment for this child was submitted ' . 
        ($time === 0 ? 0 : $time-1) . 
        ' month(s) ago.';

    if ($time <= 4) {
        echo '<img src="img/green.png" title="'. $title . '" />';
    } else if ($time <= 6) {    
    // Don't need to check if it's bigger than 4, you've already checked this 
    // in the initial "if" and if that was succesful, we wouldn't be here.
        echo '<img src="img/yellow.png" title="'. $title . '" />';
    } else {
        echo '<img src="img/red.png" title="' . $title . '" />';
    }
}
if($record->period==0){
回声';
}否则{
//你确定这是你想要的吗?
//您可能需要$record->period.(无$)
$vtime=新日期时间($record->$period);
$today=新日期时间();
$diff=$today->diff($vtime);
$time=$diff->m;
$title='此孩子的上次评估已提交'。
($time==0?0:$time-1)。
“一个月前。”;

如果($time就像我在评论中所说的那样,你可以将逻辑分开,在这里和那里进行计算。完成后,设置变量,然后在演示文稿中重复:

<?php
// initialization
$title = '';
$src = '';

// logic
$time = ($time == 0) ? $time : $time - 1;
$title = "Last assessment for this child was submitted %s Month(s) ago."; // initial

if ($record->$period == 0) { 
    $src = base_url() . "img/warning.png";
    // override $title
    $title = 'Check record';
} else {
    $vtime = new DateTime($record->$period);            
    $today = new DateTime();
    $diff = $today->diff($vtime);
    $time = $diff->m;
    if($time <= 4) {echo ;
        $src = base_url()."img/green.png";
    } elseif( $time > 4 && $time <= 6) {
        $src = base_url()."img/yellow.png";
    } elseif($time >= 6) {
        $src = base_url()."img/red.png";
    } else {
        // whatever you need to do
    }
}

$title = sprintf($title, $time);

?>
<!-- HTML MARKUP -->
<img title="<?php echo $title; ?>" src="<?php echo $src; ?>" />

" />

正如我在评论中所说的,您可以分离逻辑,在这里和那里进行计算。完成后,设置变量,然后在演示文稿中重复:

<?php
// initialization
$title = '';
$src = '';

// logic
$time = ($time == 0) ? $time : $time - 1;
$title = "Last assessment for this child was submitted %s Month(s) ago."; // initial

if ($record->$period == 0) { 
    $src = base_url() . "img/warning.png";
    // override $title
    $title = 'Check record';
} else {
    $vtime = new DateTime($record->$period);            
    $today = new DateTime();
    $diff = $today->diff($vtime);
    $time = $diff->m;
    if($time <= 4) {echo ;
        $src = base_url()."img/green.png";
    } elseif( $time > 4 && $time <= 6) {
        $src = base_url()."img/yellow.png";
    } elseif($time >= 6) {
        $src = base_url()."img/red.png";
    } else {
        // whatever you need to do
    }
}

$title = sprintf($title, $time);

?>
<!-- HTML MARKUP -->
<img title="<?php echo $title; ?>" src="<?php echo $src; ?>" />

" />
PHP很容易嵌入HTML,所以请使用它

$imageURL=”“;
如果($record->$period==0){
$imageURL=base_url()“img/warning.png”;
}否则{
//日期(“md,Y”,strotime($record->$period));//这不可用删除此语句
$vtime=新日期时间($record->$period)///////////////////////
$today=new DateTime();//用于测试///计算时间段//
$diff=$today->diff($vtime)///
$month=$diff->m;
如果($第4个月和&$个月)
PHP很容易嵌入HTML,所以请使用它

$imageURL=”“;
如果($record->$period==0){
$imageURL=base_url()“img/warning.png”;
}否则{
//日期(“md,Y”,strotime($record->$period));//这不可用删除此语句
$vtime=新日期时间($record->$period)///////////////////////
$today=new DateTime();//用于测试///计算时间段//
$diff=$today->diff($vtime)///
$month=$diff->m;

如果($month 4&&$month)只是将逻辑放在上面,那么在完成所有逻辑之后,为其设置一些变量,然后将其打印到HTML中markup@Ghost你能给出一个html的示例代码吗?$img=4;{echo image_path}”>只要把逻辑放在上面,为它设置一些变量,在所有的逻辑完成后,然后,将它打印到HTML中markup@Ghost你能给出一个html的示例代码吗