我有以下代码,其中内部图像标签我打开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";}
}
"
/>
我想要第一个条件的不同标题。即如果第一个条件为真且所示图像为“warning.png”。然后图像标题应该是“检查记录”而不是标题“提交的最后一次评估是......”
非常感谢任何帮助。
答案 0 :(得分:1)
您可以简单地使用以下内容。只需将当前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";}
}
"
/>
答案 1 :(得分:1)
就像我在评论中所说的那样,你可以将逻辑分开,在这里和那里进行计算。完成后,设置变量,然后在演示文稿中将其回显:
<?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; ?>" />
答案 2 :(得分:0)
你应该避免在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 . '" />';
}
}
答案 3 :(得分:0)
PHP很容易嵌入HTML。所以使用它。
$imageURL = "";
if ($record->$period == 0) {
$imageURL = base_url() . "img/warning.png";
} else {
// date("M d, Y", strtotime($record->$period)); // This is not usable remove this statement
$vtime = new DateTime($record->$period); ///////////////////////
$today = new DateTime(); // for testing purposes ///Calculate Time period//
$diff = $today->diff($vtime); ///
$month = $diff->m;
if ($month <= 4) {
$imageURL = base_url() . "img/green.png";
} elseif ($month > 4 && $month <= 6) {
$imageURL = base_url() . "img/yellow.png";
} else {
$imageURL = base_url() . "img/red.png";
}
}
重写您的图片代码,如下所示:
<img title = "Last assessment for this child was submitted <?php echo ($time)? $time - 1: $time; ?> Month(s) ago."
src="<?php echo $imageURL; ?>" />
您的代码存在一些错误。请仔细检查以下几点以获取进一步的发展。
date("M d, Y", strtotime($record->$period))
:为什么这个陈述存在,因为你没有保存日期函数保存的结果。
$vtime :
什么是vtime
?变量应该是简短而有意义的名称。
$diff->m :
这将返回月份编号,以便更具体,而不是$time
使用$month
作为变量名来存储月份值。
关于if
条件
If
:检查$month is <= 4
:正确If
:检查$month > 4 or <=6
elseif
:在旧代码中。为什么我们需要这个,因为如果不是
满足以上两个条件则意味着它是强制性的>= 6
然后将其直接放入其他部分。