I am trying to set each image name as a hyperlink. But problem is I am receiving 3 errors, 2 are undefined erros and the other one is expecting 1 parameter.
Current code which is not working:
<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>
Errors received:
Warning: htmlspecialchars() expects parameter 1 to be string, array given in … on line 432
Notice: Undefined variable: imgitem in … on line 456
Notice: Undefined variable: imgitem in … on line 457
Formatted Code:
<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 results:
From the var_dump specified the results are below:
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)
UPDATE:
With Praveen’s code I am getting an error stating:
Fatal error: Cannot redeclare imageName() (previously declared in ...:451) in ... on line 451
Below is code:
<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>
There are many errors!!!
$imgitem?implodeing a string?$arrImageFiledefined?Other errors…
Update
Based on the output of
var_dump, I feel that you need to add another function to get the links of the image, as it is always present in the0th index.And now, use it this way: