...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
April 3, 2012

jQuery-Galleriffic, get title of current image

Question by enne87

I use the wonderful jQuery-plugin Galleriffic to display image galeries. Now this works great, but what I want to achieve is to get the title of the image that is currently displayed. If I put the mouse cursor over the image, it shows the title. Unfortunately, I haven’t managed yet to get the title via jQuery.

Any help is very appreciated.

Thanks,

enne

Edit: Here’s the link to the plug-in: http://www.twospy.com/galleriffic/

Here is also some code. I’m not sure but I think the image is shown as background-image in the following container:

<div id="slideshow" class="slideshow">
</div>

I use the following javascript code:

 $('div.slideshow').hover(
         function () {
        //Get the title here    
          },
          function () {  
          }
  );

Ok, firebug shows me this code:

<div id="loading" class="loader" style="display: none;"></div>
    <div id="slideshow" class="slideshow">
      <div id="slide-container">
           <span class="image-wrapper current" style="opacity: 1;">
            <a class="advance-link" title="Chrysanthemum" href="#2" rel="history">
           <img alt="Chrysanthemum" src="http://ulc.local/sites/default/files/imagecache/preset_image_gallery_diashow/sites/default/files/Chrysanthemum_3.jpg">
          </a>
          </span>
         </div>

So here I need the “alt” attribute of the image.

Answer by Starx

You can select the current img using this selector

$('div.slideshow .current img')
Read more

Date between MySQL and PHP

Question by Chris Hardaker

I have a table with a field of type date within a MySQL database. My user places a date into a field (format dd-mm-yyyy) which I convert into yyyy-mm-dd for insertion into the database. This works fine. I can see the date in there as (for example) 2012-04-04.

My issue is that I then select this record, convert the date to the format I wish to display (dd-mm-yyyy) and get 03-04-2012. I understand why, in that my database is set to UTC, however the user is on Berlin time, therefore 04-04-2012 00:00 in Berlin is 03-04-2012 23:00 UTC.

The issue means that if I then save the displayed date (03-04-2012), the next time I see it, it displays as 02-04-2012 because I saved only the date and therefore the system is assuming a 00:00 time again.

I cannot see a way around this other than setting this as a datetime type rather than a date type, however I would rather not do that as time (for various reasons) is stored in a separate field. Any suggestions?

Answer by h4cky

When you inserting a record you add as datetime current UTC time, after that every user in their profile may want to/or set his timezone.

If you know the timezone of the user u can easy convert the datetime to user locale time. Because you know the differences in hours/minutes between the time.

P.S. You can store the datetime as varchar and save the unix timestamp in this field. Unix timestamp is based on current timezone I think.

UPDATE:
I think that might help

$date = time();
dump(date('d-m-Y H:i:s', $date)); // 03-04-2012 08:43:38

date_default_timezone_set('Europe/London');
dump('London: '. date('d-m-Y H:i:s', $date)); // London: 03-04-2012 11:43:38

date_default_timezone_set('Europe/Berlin');
dump('Berlin: '. date('d-m-Y H:i:s', $date)); // Berlin: 03-04-2012 12:43:38

date_default_timezone_set('Europe/Sofia');
dump('Sofia: '. date('d-m-Y H:i:s', $date)); // Sofia: 03-04-2012 13:43:38

dump function returns '<pre>'. $something .'</pre>';

Answer by Starx

First, make sure both time zones are same. Then, don’t store in datatime format, use integer. Convert the date to timestamps and then store. Like

$time = time(); //get the current time stamp
//Now insert $time

Now, both places are in common ground, You may do as you like. Changing date among different timezone is rather easy.

echo gmdate("M d Y H:i:s", $time);
Read more

url rewriting by .htaccess files in php

Question by Code Road

I am rewriting my problem .
Below I am giving all the details of what i am trying to do, and what is the result I am getting. I hope to be very clear this time. I have a samples page here www.broadcast2world.com/samples.php.
All the links and data which we see on the page is coming from a database. Now suppose we click on a link “More Info” for a particular video say social control. A specific href for that video is triggered as <a href="video.php?pid='.$id.'"> . It passes its id over url in the format http://www.broadcast2world.com/video.php?pid=66.

I catch that id into a variable using

if(!isset($_GET['pid'])){
    $pageid='66';
}
else{
  $pageid=$_GET['pid'];  
} 

Using the $pageid variable, I run a query which ultimately prepare the page for that particular video.

What my SEO guy need is a url in the format www.broadcast2world.com/video/name-of-the-video.

Now the problem is that if I make a mod_rewrite amendments which is wonderfully explained by Ben below, I am not able to figure out, how to link that name-of-the-video to its id. I am sure there must be something I have to modify while creating specific url for videos as explained above. But I really don’t know what? Thanks again for your wonderful support

Answer by Starx

Instead of id, you have to search the database based on title then.

Here are what you are going to need.

.htaccess

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^videos/(.*)$ video.php?title=$1

Link generation

$title = "...";
$title = str_replace(" ", "-", $title); //add dashes on the link
echo "<a href="videos/$title">Click Me</a>";
Read more

How to pass click events from one div to another?

Question by Yuvals

I need to find a way to pass click events from a div on top to the div below it (and ignore the clicks on the higher div).
There is a known way of simulating the click event and passing it to the other div, but this is not a natural behavior and the least wanted option.

It seems that event bubbling will not help here since the div on the top is not contained in the one below..

Any idea?

here is a jsfiddle example:

http://jsfiddle.net/QKjUx/

If you remove the “overlay” div, you can click on either of the elements. While it’s there, you cannot. It is possible to pass the event to the div below, however, the position is important – there can be a link which I want the user to be able to click on.

Answer by viebel

add this css property to the overlay div:

   pointer-events: none;

You can read more about it at: http://robertnyman.com/2010/03/22/css-pointer-events-to-allow-clicks-on-underlying-elements/

Answer by Starx

You can trigger click event on one div, by clicking on another div.

You can create an event a click event like

//A function to fire an event
function eventFire(el, etype){
    if (el.fireEvent) {
      el.fireEvent('on' + etype);
    } else {
      var evObj = document.createEvent('cClick');
      evObj.initEvent(etype, true, false);
      el.dispatchEvent(evObj);
    }
}

var div1 = document.getElementByID("firstDiv"); //find the first div
div1.onClick = function() { //attach a onclick event
   //Now select the element and fire the event
   var div2 = document.getElementById("seconddiv"); //select the second div
   eventFire(div2, 'click'); //fire the click event
};
Read more

Trouble Getting z-index Working

Question by Michael Davis

Here is a fiddle I made for an example. I can’t for the life of me figure out what I’m missing. My intention is for the class of .tss-content to sit between #wrap-a and #tss on the z axis.

In other words, trying to get the white layer positioned between the grey and the blue layers.

I’m sure it’s something dumb, but any help would be greatly appreciated. Thanks in advance!

Answer by Starx

You can float the white boxes and give a left value to push it to the right side

.tss-social {
    float:left;
    left: 50px;
} 

Demo

Read more

How to solve HTML5 Validation error on fb-like box?

Question by Dharmik Bhandari

How to solve this HTML 5 Validation error related to fb like button.

Here is the text from Validation engine:

Line 3170, column 261: Element name fb:like-box cannot be represented as XML 1.0.
…rue” border_color=”” stream=”false” header=”false” height=”260″ >

Here is the code on Line 3170..

<div id="fb-root"></div><script src="http://connect.facebook.net/en_US/all.js#xfbml=1"></script><fb:like-box href="http://www.facebook.com/fburlhere" width="250" show_faces="true" border_color="" stream="false" header="false" height="260" ></fb:like-box>

I have tried several tricks given on several sites as well as stackexchange, but none of them appear to work.

Just after this error, there is also another error which reads… Element fb:like-box not allowed as child of element div in this context. (Suppressing further errors from this subtree.) referring to the same line number.

Thanks in advance.

Answer by Starx

The markup are dynamically placed/generated from Facebook itself. You cannot control them, the way you want. But there is a HTML5 version of the Like Box, you can use instead.

When you are generating the code for the like-box. You will get an option likeFacebook HTML5 like-box
[Source]

Read more

How to Speed up Comparison of two tables in MySQL

Question by Deepak

I have two large tables in MySQL, one containing about 6,00,000 records and another containing about 90,000. I have one common field in these two tables on the basis of which I want to compare the records. I indexed both the tables on this common field but still query execution takes a very long time. Following is the query I used:

SELECT a.url,a.title,a.description,a.jobreferenceno,a.location,a.state,
    a.country,a.created_datetime,a.postalcode,a.company
FROM TABLE1 as a
WHERE EXISTS (
    select b.checkfield
    from TABLE2 as b where a.checkfield=b.checkfield
);

checkfield is the common column in both the tables.

Any suggestions on how to speed it up?

Answer by Roland Bouman

subqueries, like the EXISTS subquery you used here, are notoriously slow in MySQL. You should convert them to a JOIN if you have the chance. A JOIN query for your example would look like this:

SELECT a.url
,a.title
,a.description
,a.jobreferenceno
,a.location
,a.state
,a.country
,a.created_datetime
,a.postalcode
,a.company 
FROM TABLE1 as a 
INNER JOIN TABLE2 as b 
ON         a.checkfield = b.checkfield

(Note that this is not entirely the same as your original: here, any row from TABLE1 for that matches multiple rows in TABLE2 will be returned multiple times. If checkfield is unique in both tables, the result will be the same.)

That said, it is unclear how this really helps – you’re not really comparing rows here, simply selecting those rows from TABLE for which there is at least one row in TABLE2 that happens to have the same value in checkfield

(at any rate, checkfield should be indexed in both tables to help the efficiency of these queries )

Answer by Starx

Since there is one field in common, you can use INNER JOIN

SELECT a.url, a.title, a.description, a.jobreferenceno, a.location, a.state, a.country, a.created_datetime, a.postalcode, a.company FROM table1 a 
INNER JOIN table2 b USING (checkfield)
Read more

can't add two decimal numbers using jQuery

Question by sammy

I am trying to add two decimal values but the returned sum is pure integer. What is wrong? I can’t find it. any help will be welcome.

jQuery(".delivery-method #ship_select").change(function(){
    var cost = jQuery(this).val(); 
    jQuery("#delivery_cost").val(cost); //returns 20.00
    var tot = parseInt(cost) + parseInt(total); //total returns 71.96
});

With the code i am getting only 91 and not 91.96

Answer by kadaj

Use parseFloat() instead of parseInt().

jQuery(".delivery-method #ship_select").change(function(){
    var cost = jQuery(this).val(); 
    jQuery("#delivery_cost").val(cost); //returns 20.00
    var tot = parseFloat(cost) + parseFloat(total); //total returns 71.96
});

Answer by Starx

Use parseFloat() instead of parseInt()

var tot = parseFloat(cost) + parseFloat(total);

But, since you want to restrict to two decimal places strictly

function roundNumber(num, dec) {
   var result = Math.round(num*Math.pow(10,dec))/Math.pow(10,dec);
   return result;
}

var tot = roundNumber((cost+total), 2);
Read more

how to apply this to all of my pages in jquerymobile

Question by Wondering Coder

Im using jquerymobile together with codeigniter framework and I’m having a problem

I have this script below and I want it to be trigger in all of my pages.

<script type="text/javascript">
        var i = 0;
    $(function() {
        $("#h1").hide();
        $("#h2").hide();
        $("#h3").hide();
        <? if ($account_type != 'prepaid' && $account_type != 'PREPAID') : ?>
            $("#h4").hide();
        <? endif ?>
        head();
        setInterval('head()',2000);
    });

        function head()
        {
            i++;
            if (i==1) h1();
            if (i==2) h2();
            <? if ($account_type == 'prepaid' || $account_type == 'PREPAID') : ?>
                if (i==3){ h3(); i=0; }
            <? else : ?>
                if (i==3) h3();
                if (i==4){ h4(); i=0; }
            <? endif ?>
        }

        function h1()
        {
            <? if ($account_type == 'prepaid' || $account_type == 'PREPAID') : ?>
                $("#h3").hide();
            <? else : ?>
                $("#h4").hide();
            <? endif ?>
            $("#h1").fadeIn().delay(1000);
            //h2();
        }

        function h2()
        {
            $("#h1").hide();
            $("#h2").fadeIn().delay(1000);
        }

        function h3()
        {
            $("#h2").hide();
            $("#h3").fadeIn().delay(1000);
        }

        function h4()
        {
            $("#h3").hide();
            $("#h4").fadeIn().delay(1000);
        }

    </script>

tried replacing the $(function() { to $(document).bind(‘pageinit’, function () { but still doesn’t work. The function only fires in my index.php not in the other pages. Please help.

Answer by regeint

Try this one.
Prepare an index file with following scripts

<script type="text/javascript">
var i = 0;
$(function() {
    start();        
});

function start(){

    $("#h1").hide();
    $("#h2").hide();
    $("#h3").hide();
    <? if ($account_type != 'prepaid' && $account_type != 'PREPAID') : ?>
        $("#h4").hide();
    <? endif ?>
    head();
    setInterval('head()',2000);
}

    function head()
    {
        i++;
        if (i==1) h1();
        if (i==2) h2();
        <? if ($account_type == 'prepaid' || $account_type == 'PREPAID') : ?>
            if (i==3){ h3(); i=0; }
        <? else : ?>
            if (i==3) h3();
            if (i==4){ h4(); i=0; }
        <? endif ?>
    }

    function h1()
    {
        <? if ($account_type == 'prepaid' || $account_type == 'PREPAID') : ?>
            $("#h3").hide();
        <? else : ?>
            $("#h4").hide();
        <? endif ?>
        $("#h1").fadeIn().delay(1000);
        //h2();
    }

    function h2()
    {
        $("#h1").hide();
        $("#h2").fadeIn().delay(1000);
    }

    function h3()
    {
        $("#h2").hide();
        $("#h3").fadeIn().delay(1000);
    }

    function h4()
    {
        $("#h3").hide();
        $("#h4").fadeIn().delay(1000);
    }

</script>

now, call this function when you initpage like the following

$('#my_page').live('pageinit',function(event){
start();

});

Answer by Starx

If you want to fire in other pages too. Include the script in all pages. Using

<script src="common.js"></script>
Read more

Printing HTML page

Question by Java

I have a very long table in my webpage. When I print it, it looks that the last row of the table is in the one page ( only in the top of the page ). The rest of the page is blank. On the next page I have nest table. I do not know, why next table is not in the bottom of the last row of first page.

In HTML looks in following way:

<table align="left" cellspacing="0" cellpadding="0" border="1">
  <thead>
    <tr>....</tr>
  </thead>
  <tbody>
    <tr>...</tr>
  </tbody>
</table>

<table align="center" width="800" cellspacing="0" cellpadding="0" border="1" style="margin: 0 auto 0 auto; page-break-inside: avoid;">
  <tbody>
    <tr>
      <td align="left" width="200">xxx</td>
      <td align="right" width="200">xxx</td>
      <td align="right" width="200">xxx</td>
      <td align="right" width="200">x</td>
    </tr>
    <tr>
      <td align="left" width="200">
      <td align="right" width="200">
      <td align="right" width="200">
      <td align="right" width="200">
    </tr>
    <tr></tr>
</table>

CSS:

background-color: white;
border: 1px solid #000000;
border-collapse: collapse;
border-spacing: 0;
font-size: 10px;
vertical-align: middle;

Answer by Starx

You can control the style-sheet used for print separately using

@media print {
   ....
}

Since you table is breaking inside, you can disable that using

@media print {
   table {
       page-break-inside: avoid;
   }
}
Read more
...

Please fill the form - I will response as fast as I can!