尝试创建超链接时未定义的通知

时间:2013-01-24 22:18:42

标签: php html

尝试为每个图像名称设置超链接时,我在超链接中收到未定义的imgitem通知。我是否将<a>标签放在了错误的位置?

echo '<td width="11%" class="imagetd">';
if (empty($arrImageFile[$key])) {
    echo '&nbsp;';
} 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 '&nbsp;';
        } 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行的...中给出数组

注意:未定义的变量:第456行的imgitem

注意:未定义的变量:imgitem in ... on line 457

2 个答案:

答案 0 :(得分:1)

使用单引号将返回文字值。

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 '&nbsp;';
} 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>';

答案 1 :(得分:1)

我会把你的结构改成这样的东西:

     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 = '&nbsp;';
        }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;