...

Hi! I’m Starx

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

Hide anchor tag from url

Question by soft_developer

Possible Duplicate:
How to hide anchor tag from URL

I have a menu which contains submenus, I want that onclick if each submenu, that will redirect me on a div of the php page :

For example if I want to go from :

Menu Menu1 –> Submenu1 I want that onclick of my sbmenu1 it redirect my on a div on page1#div_name without dipslay on the URL : www.page1.php#div_name, I just want www.page1.php

    <ul>
        <li><a href="#">Menu1</a>
            <ul>
                <li><a href="page1.php#div_name">Submenu1</a></li>
                <li><a href="page2.php#div_name">Submenu2</a></li> 
            </ul>
        </li>
    </ul>

How can I fix this?

Answer by Starx

You can modify the URL after it completes loading

window.history.pushState(“object or string”, “Title”, “page.php”);

FOr more info, check this tutorial.

Read more

Interpolate constant (not variable) into heredoc?

Question by Michael

<?php
   define('my_const', 100);
   echo <<<MYECHO
      <p>The value of my_const is {my_const}.</p>
MYECHO;
?>

If i put a variable inside the braces it prints out. But not the constant. How can I do it?
Thank’s!

Answer by Starx

Use sprintf()

define('my_const', 100);
$string = <<< heredoc
      <p>The value of my_const is %s.</p>
heredoc;

$string = sprintf($string, my_const);
Read more

jquery animate height bouncing

Question by g0dl3ss

i made a script that dynamically loads content into a div and whenever the content makes the div’s height bigger or smaller it animates the height to the actual height.

The problem is that it doesn’t only slide up/down it always slides a bit up and then down to the desired height.

I’d like to make it slide directly at the desired height without the “bouncing”.

Here’s part of the script:
main_content.html(html);

main_content.css("height", "auto");
var newContentHeight = main_content.height();
loader_div.hide();
main_content.height(prevContentHeight);
main_content.fadeIn("fast", function() {
    main_content.animate({
        "height": newContentHeight
        }, 300);
    });

The script works, the only problem is that the animation doesn’t slide the div up or down to the desired height, it always slides it a bit up and then down to the desired height.

Is it some kind of wanted animation when using animate height or is there something wrong?
Thanks

edit: console logging newContentHeight(the final height of the div) it looks like it gathers 2 heights, then the animate first goes to one of them and then to the other one that’s why it looks like bouncing. Working on it.

edit2: yes the problem is definitely there, i cleaned up all the code and everything works except that i get 2 attributes from newContentHeight using console.log it looks like the content div that is hidden has 2 heights and those 2 heights are passed to the .animate that’s why it first goes up and then down…
Kinda strange

Answer by g0dl3ss

Couldn’t solve the problem, i modified the script below and now everything works like it should.
http://css-tricks.com/dynamic-page-replacing-content/
It has the exact animation i was looking for(except for a couple of steps easily implementable).

Answer by Starx

I think the problem is not the script, but the system running the script. Replace the time to a bit slower

main_content.animate({
"height": newContentHeight
}, 3000);
Read more

php mysql query – output value of variable in the query

Question by CLiown

I have the following mysql query in php:

$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%details%'");

However I want the query to be dynamic by changing the LIKE section of the query. Instead of:

LIKE '%details%'

I want to put a variable in there:

LIKE '% $format %'

where $format is a string.

Everything I have tried thus far has failed.

Whats the proper way to do this?

Answer by Starx

You already got the answer. I can do it exactly like you want.

$results = $wpdb->get_results("SELECT * FROM $wpdb->postmeta WHERE meta_key = '_wp_attachment_metadata' AND meta_value LIKE '%$details%'");

Since the query is wrapped in double quotes, you dont need to escape anything.

Read more

doesn't check for me?

Question by Fightpa Sirprite

i use split function

string = data.con_loop[i].split("\.");

string input is “0.9.L” when i check

if(string[2]=="L")

it doesn’t into statement in scope of if Why?

Answer by Starx

You don’t need double slashes, then use equals to check

string = data.con_loop[i].split(".");
if(string[2].equals("L")) {
    //.....
}
Read more

MySQL query for mutual friends

Question by george

Possible Duplicate:
MYSQL select mutual friends

I have a table for friendship, the friendship is stored only in one line. So there is no duplicate entries.

id  Person1    Person2  status
1         1          2  friend
2         1          3  friend
3         2          3  friend
4         3          4  friend

What MySQL query (join, inner join) will help me to find common (mutual) friends between person #1 and person #3? The input in this example is {1,3} and the output should be {2} since Person #2 is friend with bot #1 and #3.

Answer by Starx

I think this is rather simply achieved by this

SELECT * FROM friends

WHERE
     (Person1 = '1' or Person2 = '1') && 
     (Person1 = '2' or Person2 = '2') &&
     status = 'friend'

Given that the you are trying to find mutual between person 1 and 2

Read more

border-radius bug with <a> tag in Firefox on Mac

Question by Blowski

In Firefox, when I put border-radius on an <a> tag, I’m getting little white lines. appearing between the text and the edge of the element. If I zoom in, they disappear.

I tried putting it on JSFiddle, but it’s not replicating the issue.

Does anyone recognise the issue visually?

The whole page is here: http://www.danblows.com/.

The bit adding the border radius is, but just putting the same code onto JSFiddle doesn’t replicate the issue.

.cta, .cta:visited  {
    background-color:#000000;
    border-radius:5px;
    border:10px solid #000000;
    color:#FFFFFF;
}

Screengrab of issue

Answer by Starx

Link cannot display block properties properly. Give your anchor a display: inline-block;

.cta { display: inline-block; }

TESTED 🙂

Read more

Insert into table with multiple select statements in MY SQL

Question by Ashara Shrestha

I have the result from a query like:

+------------------+------------+
| meta_key         | meta_value |
+------------------+------------+
| Destination Name | Shivapuri  |
| Destination Date | 26/03/2012 |
+------------------+------------+

I am trying to write a select statement with the Column name as Destination Name and Destination Date whose respective values are Shivapuri and ’26/03/2012′. How is this possible to do with a query in MY SQL?

Answer by Starx

SELECT 
    (CASE WHEN meta_key = 'Destination Name' THEN meta_value END) as name,
    (CASE WHEN meta_key = 'Destination Date' THEN meta_value END) as date
FROM `yourtable`
Read more

dynamically change function name in javascript

Question by zugrina

$('a').live('click',function(e){
    e.preventDefault();
    var id = $(this).attr('id');
    infowindow2.open(map, marker2); // I need instead of 2 to print the value of variable id
});

How can I dynamically change the number 2 to variable ID ?

Thanks for any help

Answer by gdoron

$('a').live('click',function(e){
    e.preventDefault();
    var id = $(this).attr('id');
    infowindow2.open(map, eval('marker' + id)); 
});

LIVE DEMO

Notes:

  • eval is deprecated, You should look for a better design.
  • so as live… You should use on instead.

Answer by Starx

EVAL should always be the last option

In order use dynamic name in a function names you can windows object.

Here is an Example:

var id = '2';
function map2() {
    alert('me called');
}
window["map"+id]();

Demo

Your Usage would be something like this

$('a').on('click',function(e){
    e.preventDefault();
    var id = $(this).attr('id');
    infowindow2.open(map, window['map'+id]()); 
});
Read more

Input texts go back to default value after click

Question by nunos

The form below is working as I want except for one thing. When I click the ‘Add’ link I the texts of all the textboxes go back to its default value like seen in the images. How can I fix it? Thanks.

before click:

first image

after click on ‘Add’:

second image

Code:

function addSection() {
    var str = '<div>' + 
                '<input type="text" name="composer" value="Compositor" />' + 
                '<input type="text" name="peca" value="Peca" size="40" />' + 
                '<input type="button" value="x" style="width: 26px" onclick="delSection(this)" /><br />' + 
            '</div>';

    document.getElementById("programa").innerHTML += str;
}

function delSection(field) {
    field.parentNode.outerHTML = "";
}

window.onload = addSection;

</script>
<fieldset>
    <legend>Programa</legend>
    <div id="programa">
    </div>
    <a href="#" onclick="addSection()" >Add</a><br />
</fieldset>
<input type="submit" value="Inscrever Aluno" name="submit" />

Answer by Starx

You can overcome this, using appendChild()

JS

function newSet() {
    var div = document.createElement("div");   

    var composer = document.createElement("input");
    composer.type="text";
    composer.value = "Compositer";

    var peca = document.createElement("input");
    peca.type = "text";
    peca.value = "Peca";

    var button = document.createElement("input");
    button.type = "submit"

    div.appendChild(composer);
    div.appendChild(peca);
    div.appendChild(button);

    return div
}

function addSection() {    
    document.getElementById("programa").appendChild(newSet());
}

Demo

Read more
...

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