尝试为每个图像名称设置超链接时,我在超链接中收到未定义的imgitem
通知。我是否将<a>
标签放在不正确的位置?
echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
echo ' ';
} else {
echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[$imgitem]" target="_blank">';
echo implode("</li>'n<li></a><a href='previewImage.php?imgId=[$imgitem]' target='_blank'>", array_map(function($imgitem){
return htmlspecialchars($imgitem);
}, $arrImageFile[$key]));
echo '</li></ul></a>';
}
echo '</td>';
更新:
当前代码不起作用:
<table id="tableqanda" cellpadding="0" cellspacing="0">
<thead>
<tr>
<th width="11%" class="image">Image</th>
</tr>
</thead>
</table>
<div id="tableqanda_onthefly_container">
<table id="tableqanda_onthefly" cellpadding="0" cellspacing="0">
<tbody>
<?php
function imageName( $imgitem ) {
return htmlspecialchars($imgitem); //432
}
foreach ($arrQuestionId as $key=>$question) {
echo '<tr class="tableqandarow">'.PHP_EOL;
echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
echo ' ';
} else {
echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">'; //line 456
echo implode('</li>'n<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) ); //line 457
echo '</li></ul></a>';
}
echo '</td>';
echo '</tr>'.PHP_EOL;
}
?>
</tbody>
</table>
</div>
收到的错误:
警告:htmlspecialchars() 期望参数 1 是字符串,数组在 ...在第 432 行
注意:未定义的变量:imgitem in ...在第 456 行
注意:未定义的变量:imgitem in ...在第 457 行
我会将您的结构更改为这样的东西:
function CreateLink($filename, $type){
if($type == 'image'){
return '<a href="previewimage.php?filename='.$filename.'" title="Click to view in New window" target="_blank">'.htmlspecialchars($filename).'</a>';
}
}
......................
$img_result = '';
if(empty($arrImageFile[$key])){
$img_result = ' ';
}else{
$img_result .= '<ul class="qandaul">';
if(is_array( $arrImageFile[$key] )){
foreach($arrImageFile[$key] as $filename){
$img_result.= '<li>' . CreateLink($filename, "image") . '</li>';
}
}else{
$img_result.= CreateLink($arrImageFile[$key], "image");
}
$img_result.= '</ul>';
}
//end:procedure image
echo '<td width="11%" class="imagetd">'.$img_result.'</td>' . PHP_EOL;
使用单引号将返回文本值。
echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[$imgitem]" target="_blank">';
更改为:
echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">';
更新
function imageName( $imgitem ) {
return htmlspecialchars($imgitem);
}
echo '<td width="11%" class="imagetd">';
if( empty($arrImageFile[$key]) ) {
echo ' ';
} else {
echo '<ul class="qandaul"><li><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">'; //line 443
echo implode('</li>'n<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) );
echo '</li></ul></a>';
}
echo '</td>';