...

Hi! I’m Starx

experienced Software Developer. And this is my blog
Start Reading About me
Blog Page
June 8, 2011

How to do a Join in MySql based on partial matches?

Question by Shyam Natraj Kanagasabapathy

I have two tables.

Table A

Column 1     Column 2
CT 3C        10.5 -23.12
OT 5A        11.2 -24.5

Table B

Column 1     Column 2
PRIM         12.3 -24.51, 10.5 -23.12, 61.24 -78.23
SEC          8.7345 -46.1934, 10.49 -49.1834
TERT         18.98 -28.12, 11.23 -24.78, 1.45 -24.11, 11.2 -24.5

Resulting Table after the join:

Column 1      Column 2      Column 3     Column 4
CT 3C         10.5 -23.12   PRIM         12.3 -24.51, 10.5 -23.12, 61.24 -78.23
OT 5A         11.2 -24.5    TERT         18.98 -28.12, 11.23 -24.78, 1.45 -24.11, 11.2 -24.5

Is there a way to this match without splitting the contents of ‘column 2 in Table B’ into separate columns? The contents in ‘Column 2 of Table B’ are delimited by ‘,’.

10.5 -23.12 should match with 12.3 -24.51, 10.5 -23.12, 61.24 -78.23 as it is contained in the list of values present in it.

I can’t split them into separate columns because in some cases there are as many as 300 distinct values in it separated by a ‘,’

Answer by OMG Ponies

Short Term Solution

Use the FIND_IN_SET function:

SELECT a.column1, a.column2,
       b.column1, b.column2
  FROM TABLE_A a
  JOIN TABLE_B b ON FIND_IN_SET(a.column2, b.column2) > 0

Long Term Solution

Denormalized data should only be stored as a performance improvement, after it’s been determined necessary — not before.

You need a many-to-many table between TABLE_A and TABLE_B to hold the relationships represented in the comma separated lists.

Answer by Starx

300 distinct values This is clearly not a field. A nested table, might be a better name it.

You are doing it wrong. If a single field contains about 300 records, you should keep it as a different table.

Read more
June 7, 2011

How to save pictures in MySQL

Question by JKAUSHALYA

I want to save pictures(mostly JPEG) to MySQL database. I saw most people say save pictures elsewhere and add link to table. It is the most efficient way. But i need to encrypt my pictures and want to set user privileges. So how can i do this. Please can anyone help me.

I’m using a C client program to connect to the MySQL.

Answer by genobis

Not a good idea, but if you really have to do it this way, use BLOBs (a data type).

http://dev.mysql.com/doc/refman/5.0/en/blob.html

Answer by Starx

Yes, do not insert pictures to the database table. It reduces manageability heavility. Also, when the database increases, the performance is reduced significantly. Let DBMS manage data and let File Management System manage files.
Better, save the pictures on a table and then later only retrieve the images from a folder, once a valid user is found.

Read more
June 1, 2011

<textarea> box to increase height (wrap) when the current text overflows the given width?

Question by nubela

Is there a css (non-js) solution to have my box to increase height (wrap) when the current text overflows the given width?

Thanks!

Answer by Starx

Given that you want to increase text area in its height. It is possible at CSS3 supporting browsers like FF4. You can also control the direction of which it can be resized.

Another of the CSS3 solution can be using contenteditable element, instead of textarea, which will make how you want possible without using any javascript?

I might show you some examples if this is what you want.

Read more

how to Disable print screen in web page using JavaScript

Question by Neon

i am developing a website, i want disable print screen so i searched in the net and i used a JavaScript to disable print screen. it worked fine but during loading of a page it asks for the permission to access the clipboard.

the pop-up message that it shows,

“do u want to allow this webpage to access your clipboard?
If you allow this, the webpage can access the clipboard and read information that you’ve cut or copied recently.. “

i want to avoid the above pop-up message and also disable print screen.
Below is my JavaScript code.:

     function AccessClipboardData() {
        try {
            window.clipboardData.setData('text', "Print Disabled");
        } catch (err) {               
        }


    <body>
    <script language="JavaScript" type="text/javascript">
        setInterval("AccessClipboardData()", 300);
        var ClipBoardText = "";

        if (window.clipboardData) {
            ClipBoardText = window.clipboardData.getData('text');
            ClipBoardText = window.clipboardData.clearData('text');
            if (ClipBoardText == "") {
                alert('Sorry you have to allow the page to access clipboard');
                document.all("divmaster").style.display = "none"
            }
        }       
    </script>
</body>

can please help to solve that print screen and clip board problem.

thanks in advance..

Answer by Starx

You must be trying to protect your page from theft or copy.

But I have only one to say to you. If the website loads on the clients computers, it means all the contents including images to markup all of them are stored on the client’s PC and then displayed on the browser window. So, not matter what you do, there is never a final solution for this.

So, I suggest you to do not go down this road.

Read more
May 29, 2011

jQuery $.post doesn't pass what button were pressed

Question by Cheburek

Recently I’ve stuck with a problem where $.post does not send information about button which were pressed.
I.e. we have a form with two buttons.

<form method="post"  action="/home/index">

  <input type='hidden' name='data1' value='value1' />

  <button name="button1" type="submit">First</button>
  <button name="button2" type="submit">Second</button>

</form>

$(document).ready(function() {
    $('#formId').submit(function () {
        var f = $(this);
        var action = f.attr("action");
        var serf = f.serialize();
        $.post(action, serf,
        //onreturn
          function (data) {
                //do something here on success
              },
              'json'
          );
        return false;
      });
});

Without ajax form is posted as following if user pressed First button: data1=value1&button1

But when I use $.post posted form does not contain any information about button: data1=value1

jQuery 1.6.1

The behavior depends on that which button were pressed.
Any help or workaround would be appriciate!

Answer by T.J. Crowder

Since you’re stopping the browser’s default behavior, which is what supplies the button value, you’ll have to capture what button was pressed, and add that information to the data you’re posting (your serf data). Probably the most reliable way would be to hook the click event on the buttons as well as the submit event of the form, and route them all to a central function that does the ajax submission with (or without) a button value (without in the case of a form submitted another way, rather than by pressing one of those buttons).

Something along these lines:

$(document).ready(function() {
    function submitTheForm(f, button) {
        var action = f.attr("action");
        var serf = f.serialize();
        if (button && button.name) {
            serf += "&" + encodeURIComponent(button.name);
            if (button.value) {
                serf += "=" + encodeURIComponent(button.value);
            }
        }
        $.post(action, serf,
        //onreturn
          function (data) {
                //do something here on success
          },
          'json'
        );
    }
    $('#formId').submit(function () {
        submitTheForm($(this));
        return false;
    });
    $("#formId :submit").click(function() {
        submitTheForm($(this).closest('form'), this);
        return false;
    });
});

Answer by Starx

I don’t think that is suppose to be happening. But anyways, you could something like this too.

$("#formid").children("input[type="submit"]").click(function() {
    var name = $(this).attr("name");
    post(name);
    return false;
});
function post(buttnname) {
    var action = $("#formid").attr("action");
    var serf = $("#formid").serialize()+"&button="+buttname;
    $.post(action, serf,
    //onreturn
        function (data) {
            //do something here on success
          },
          'json'
        );
}
Read more
May 26, 2011

Absolute positioned image in border radius wrapper

Question by Johan Wallberg

I have a wrapper with border radius. Inside the wrapper I have a absolute positioned image in the top right corner. My problem is that the image doesn’t crop/hide under the wrapper with border radius. I’ve tried overflow:hidden on the wrapper but it doesn’t work. See image below.

enter image description here

Answer by Starx

Image tag is not affected by border-radius.

Your best bet is to use the picture as a background like:

<div id="someimage" style="background:url('image.jpg');border-radius: 5px; height: 200px; width: 500px;"></div>

The element(in above example a div) should contain the size of the actual image), and unless you use CSS3, the image cannot be resized like <img> tag

Read more
May 24, 2011

mouseover() – using addClass() with this

Question by clarkk

how can I add a class when mouse is over an element?

var row = $('<tr></tr>')
    .append('<td class="list_row">'+js2txt(string)+'</td>')
    .mouseover(function(){
        this.addClass('list_row_mover');
    });

js error:

this.addClass is not a function

Answer by KARASZI István

In your function the scope (this) is the HTML element, not the jQuery object.

Here is a possible fix:

var row = $('<tr></tr>')
  .append('<td class="list_row">'+js2txt(string)+'</td>')
  .mouseover(function(){
    $(this).addClass('list_row_mover');
  });

Answer by Starx

use

$(this).addClass("list_row_mover");
Read more

Using nl2br with html tags

Question by Alex Emilov

I use nl2br when displaying some information that is saved somewhere, but when HTML tags are used I want not to add <br> tags for them.

For example if I use

<table>
<th></th>
</table>

it will be transformed to

<table><br />
<th></th><br />
</table><br />

and that makes a lot of spaces for this table.

Ho can break line tags be added only for other non-HTML content?

Thanks.

Answer by Michiel Pater

You could replace the closing tags and newlines by only closing tags:

$str = str_replace('>
', '>', $str);

Answer by Starx

I think your question is wrong. If you are typing

<table>
<th></th>
</table>

into a text area then no matter what you do It will include <br /> in between them. Because it is what nl2br is supposed to do.

Read more
May 13, 2011

How to use jquery rewrite redirect url

Question by user610983

I created a drop down list(with javascript onchange features), I would like to rewrite the redirect url from this: http://localhost/en/abc/kk.php?CT=1 to http://localhost/abc/kk.php?lang=en&CT=1 by using jquery.

Possible to do it?

Answer by Starx

You cannot rewrite the redirect url through clientside script such as javascript itself. You need .htaccess file to do so.

However, if the urls http://localhost/en/abc/kk.php?CT=1 is already present in your markup like in anchor tags

<a href="http://localhost/en/abc/kk.php?CT=1">Some Link Text</a>

Then you can use jQuery to change the value

$(document).ready(function() {
    $("a").attr("href","http://localhost/abc/kk.php?lang=en&CT=1");
    //It is better to replace the values using pattern mathcing
});
Read more
May 11, 2011

Can't put IP-address in database

Question by Bjorn Seigers

I’m trying to put an ip-address in my database.
If I do an echo like this:

echo $_SERVER['REMOTE_ADDR'];

But if I’m trying to put it in a variable or in the database it gives nothing, so in my database it says: NULL
The commands I used for that:

$ip = $_SERVER['REMOTE_ADDR'];
mysql_query("UPDATE users SET last_ip='".$ip."' WHERE id=".$row['id']) or die(mysql_error());

I don’t know what I’m doing wrong.
Can someone help me with this please?

Thanks!

Answer by Starx

Your code is correct and should work. Unless as commented by @wallyk, the data type of the ip field is unsupported one.

However, just to make sure wrap the WHERE condition in ' (Single Quote) and try.

Read more
...

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