Jump to content

How To Add Tooltip For Image Displayed From Database


sildona

Recommended Posts

Set alt,title attribute for image tag

 

Like:

echo "<img src='".$showimage."' height='200px' width='133px'  alt='SAMPLE TEXT'/>";
OR
echo "<img src='".$showimage."' height='200px' width='133px'  title='SAMPLE TEXT'/>";

 

or you can download some javascript tooltip script and apply it to image tag.

Link to comment
Share on other sites

Here's two examples to do it.

Example Page: http://xaotique.no-ip.org/tmp/13/

 

index.php

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
   "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">

<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
   <head>
       <title>Xaotique</title>
       <meta http-equiv="content-type" content="text/html;charset=utf-8" />
       <link rel="stylesheet" href="stylesheet.css" type="text/css" />
       <script type="text/javascript" src="http://code.jquery.com/jquery-latest.min.js"></script>
       <script type="text/javascript" src="script.js"></script>
   </head>

   <body>
       <?php $image = "http://www.glamquotes.com/wp-content/uploads/2011/11/smile.jpg"; ?>

       <img height="250" src="<?php echo $image; ?>" title="Example 1 using a title attribute." />
       <img height="250" src="<?php echo $image; ?>" class="js-title" title="Example 2 using Javascript or title attribute backup." />
   </body>
</html>

 

script.js

$(document).ready(function()
{
   var hover = false, item;

   $(window).mousemove(function(e)
   {
       if (hover)
       {
           item.data("tooltip").css(
           {
               left: e.pageX + 20,
               top: e.pageY + 20
           }).show();
       }
   });

   $(".js-title").hover(
       function(e)
       {
           if ($(this).attr("title") != undefined)
           {
               $(this).data("title", $(this).attr("title"));
               $(this).data("tooltip", $(document.createElement("div"))
                   .html($(this).data("title"))
                   .attr("className", "js-tooltip")
                   .css(
                   {
                       position: "absolute",
                       left: 0,
                       top: 0,
                       "background-color": "#252525",
                       color: "#EFEFEF",
                       padding: 5,
                       border: "4px double #EFEFEF",
                       "white-space": "nowrap"
                   })
                   .hide()
               );

               $("body").append($(this).data("tooltip"));
               $(this).removeAttr("title");
           }

           if ($(this).data("title") != undefined)
           {
               hover = true;
               item = $(this);
           }
       },
       function()
       {
           hover = false;
           item.data("tooltip").hide();
       }
   );
});

Edited by Xaotique
Link to comment
Share on other sites

  • 2 weeks later...

If it were my site, I would add the tooltip text in a new field to the database. Then when you are fetching from the DB, retrieve that and bind it as an additional result.

 

while($stmt->fetch()){
   $format = "<img src='%s' title='%s' />" // where %s is a placeholder for a variable string
   printf($format, $imgSrc, $imgTooltip) // $format is the intended format, the following two parameters are the strings that you will place into the image placeholders during each iteration of results.
}

Link to comment
Share on other sites

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.