Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

errors received when trying to set hyperlinks

Tags:

html

php

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

1 Answers

There are many errors!!!

  1. Where have you defined $imgitem?
  2. Why are you implodeing a string?
  3. Why are you closing the function prematurely, and using the variables there in the code out of the function?
  4. Where is the $arrImageFile defined?
  5. 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's 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's 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's 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 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 the 0th index.

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

And now, use it this way:

<a href="previewImage.php?imgId=[' . getImgLink($arrImageFile($key)) . ']" target="_blank">
like image 57
Praveen Kumar Purushothaman Avatar answered Jul 29 '26 06:07

Praveen Kumar Purushothaman