我正在尝试将每个图像名称设置为超链接。但问题是我收到 3 个错误,2 个是未定义的错误,另一个需要 1 个参数。
当前代码不起作用:
<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 行
格式化代码:
<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>
var_dump
结果:
从 var_dump 指定的结果如下:
array(2) {
[72]=> array(2) {
[0]=> string(16) "Lighthouse_4.jpg"
[1]=> string(12) "Tulips_3.jpg"
}
[73]=> array(1) {
[0]=> string(16) "Hydrangeas_3.jpg"
}
}
int(73)
更新:
使用 Praveen 的代码,我收到一条错误消息:
Fatal error: Cannot redeclare imageName() (previously declared in ...:451) in ... on line 451
下面是代码:
<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>
//line 451 <?php function imageName( $imgitem ) { return htmlspecialchars($imgitem);
} foreach ($arrQuestionId as $key=>$question) { echo '
<tr class="tableqandarow">'.PHP_EOL; echo '
<td width="11%" class="imagetd">'; function imageName( $imgitem ) { return htmlspecialchars($imgitem);
function getImgLink ( $imgKey ) { return $imgKey[0]; if (empty($arrImageFile[$key]))
{ echo ' '; } else { echo '
<ul class="qandaul">
<li>
<a href="previewImage.php?imgId=[' . getImgLink($arrImageFile($key)) . ']"
target="_blank">'; echo implode('</li>\n
<li>
</a>
<a href="previewImage.php?imgId=[' . getImgLink($arrImageFile($key)) . ']"
target="_blank">', imageName($arrImageFile[$key]) ); echo '</li>
</ul>
</a>'; } echo '</td>'; } } echo '</tr>'.PHP_EOL; } ?></tbody>
</table>
</div>