Jump to content

Recommended Posts

Something that's been bothering me for a while with some new code, I have a function ClickImage (Shown below)

function ClickImage($Image, $URL, $LinkID) { ?><img src=<?echo $Image;?> onclick="window.open('<?echo $URL.sha1($LinkID);?>','','width=250,height=200')" class=parchment><?}

 

Now, this code all works fine, but I often want to display this item in a table, like so:

echo "<table><tr><td>".ClickImage($ItemArray['Image'], "ItemDescription.php?ID=", $ItemArray['ItemID'])."</td><td><b>[some Other Text]</b></td></tr></table>"

 

Running the code like this will produce the table, but the image will be displayed before the table is called. Output given below (Pulled from Firebug):

<td width=25%><img class="parchment" onclick="window.open('ItemDescription.php?ID=fb644351560d8296fe6da332236b1f8d61b2828a','','width=250,height=200')" src="/EnnuiImages/Items/M11.png">
<table><tr><td>[Where the image should be]</td><td>[some other Text]</td></tr></table></td>

 

However, I've found that if I call it separately, like so:

echo "<table><tr><td>"; 
echo ClickImage($ItemArray['Image'], "ItemDescription.php?ID=", $ItemArray['ItemID']);
echo "</td><td><b>[some Other Text]</b></td></tr></table>"

Then it displays as it should, I'm wondering if there's a specific problem I'm not noticing, or is this problem just inherent to using a function to display an image in this way?

 

Thanks in advance :)

 

Link to comment
https://forums.phpfreaks.com/topic/207627-php-function-breaking-table/
Share on other sites

Yes, there is a specific problem and it is rather subtle.  The way you wrote your echo line:

echo "<table><tr><td>".ClickImage($ItemArray['Image'], "ItemDescription. ...

would work if ClickImage() returned a string. But ClickImage() does not return a string, it does not return anything, so there is nothing in the cell. However, since ClickImage() has some HTML outside of the PHP tags, that HTML text is "sent to the browser".

 

Now consider the sequence of events in this line of code.  In order for the ECHO statement to send the (table) HTML it has to FIRST evaluate ClickImage(). So ClickImage() is executed (which causes the IMG tag to be sent to the browser). It returns nothing, so (basically) an empty string "replaces" it in the echo statement, THEN the TABLE tag is sent to the browser (remember, the IMG tag was already sent).

 

To fix this, you should have ClickImage() return the string (if you have ClickImage() echo the string you will get the same result). This is why some of the PHP builtin functions (such as print_r) have a $return parameter that let's you control whether the function sends the output to the browser or return it as a string:

 

function ClickImage($Image, $URL, $LinkID) { ?>
<img src=<?echo $Image;?>
onclick="window.open('<?echo $URL.sha1($LinkID);?>
','','width=250,height=200')" class=parchment><?
}

 

I would replace it with something like

function ClickImage($Image, $URL, $LinkID, $return = false) { 
    $out = '<img src="' . $Image . '"' .
        ' onclick="window.open(\'' . $URL.sha1($LinkID) . '\',\'\',\'width=250,height=200\')" class=parchment>';

    if ($return) return $out
    else echo $out;
}

It is always a pain to get both single and double quotes inside a string. I'm sure that could be cleaned up a bit.  It is presented here as an example of how to handle the output functionality.

 

Oh, and don't use short-tags ( <? ) these will lead to problems if you move to a site that does not support them.  Always use full tags ( <?php )

 

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.