我有以下代码,在图像标签中,我打开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";}
}
"
/>
我想要第一个条件的不同标题,即如果第一个条件为真并且显示的图像是"警告.png"。然后,图像标题应为"检查记录",而不是标题"上次提交的评估是......"
任何帮助都非常感谢。
您可以简单地使用以下内容。只是嵌套电流,如果...否则条件在另一个如果...标题标签上的其他条件也。
<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";}
}
"
/>
就像我在评论中所说的那样,你可以分离逻辑,在这里和那里进行计算。完成后,设置变量,然后将其回显到演示文稿中:
<?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; ?>" />
你应该避免在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 . '" />';
}
}
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 :
这将返回月份数字,以便更具体,而不是使用$month
$time
作为变量名称来存储月份值。
关于if
条件
- 第一个
If
:检查$month is <= 4
:正确 - 第二
If
:检查$month > 4 or <=6
- 第三
elseif
:在你的旧代码中。为什么我们需要这个,因为如果不是满足以上两个条件,则意味着它是强制性的>= 6
然后直接将其放入其他部分。