Errors received when trying to set up hyperlinks

I am trying to set each image name as a hyperlink. But the problem is I am getting 3 errors, 2 are undefined erros and the other is 1 parameter.

Current code that doesn't work:

    <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>

      

Errors received:

Warning: htmlspecialchars () expects parameter 1 to be a string, the array is specified in ... on line 432

Note: variable undefined: imgitem in ... on line 456

Note: variable undefined: 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 '&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>

      

var_dump

results:

From the specified var_dump parameter, the results are shown 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 Pravei's code, I get the error:

Fatal error: Cannot redeclare imageName() (previously declared in ...:451) in ... on line 451

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

      

+3


source to share


2 answers


There are many mistakes !!!

  • Where did you identify $imgitem

    ?
  • Why are you implode

    entering a string?
  • Why are you closing the function prematurely and using variables in your code outside the function?
  • Where is it indicated $arrImageFile

    ?
  • Finally, what are you trying to do?

Other errors ...

function imageName( $imgitem ) {
    return htmlspecialchars($imgitem); //432
}
///////////////////////////////////////////////////////////////////
// You are closing the function here, hence the $imgitem scope //
///////////////////////////////////////////////////////////////////


          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
////////////////////////////////////////////////////
// The $imgitem variable is not accessible here //
////////////////////////////////////////////////////

    echo implode('</li>\n<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) ); //line 457
////////////////////////////////////////////////////////////////////////////
// Why are you trying to implode something like this? Not a right syntax! //
////////////////////////////////////////////////////////////////////////////
    echo '</li></ul></a>';
        }
        echo '</td>';
        echo '</tr>'.PHP_EOL;
        }

      


Update



<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
                }
----------------^
///////////////////////////////////////////////////////////////////
// You are closing the function here, hence the $imgitem scope //
///////////////////////////////////////////////////////////////////

                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
-------------------------------------------------------------------------------------------^
/////////////////////////////////////
// Where have you defined $imgitem //
/////////////////////////////////////

                        echo implode('</li>\n<li></a><a href="previewImage.php?imgId=[' . $imgitem . ']" target="_blank">', imageName($arrImageFile[$key]) ); //line 457
------------------------------------------------------------------------------------------^
/////////////////////////////////////
// Where have you defined $imgitem //
/////////////////////////////////////

                        echo '</li></ul></a>';
                    }
                    echo '</td>';
                    echo '</tr>'.PHP_EOL;
                }
            ?>
        </tbody>
    </table>
</div>

      


Based on the output var_dump

, I feel like you need to add one more function to get the image links as it is always present in the 0

-th index.

function getImgLink ( $imgKey )
{
    return $imgKey[0];
}

      

And now use it like this:

<a href="previewImage.php?imgId=[' . getImgLink($arrImageFile($key)) . ']" target="_blank">

      

+3


source


try to remove the square brackets enclosed in [' . $imgitem . ']

. Square brackets do not match query string char



Another thing is why use it implode

?

0


source







All Articles