May 5, 2012

Jquery contains selector, regex

Question by Jernej Pangeršič

This is my code:
Let’s say that name is set to john

$("#first_table").append($('#second_table').find("tr:contains(" + name + ")"));

Basically I have 2 tables on my website. I would like to move elements from first to second, but only if they match completely. How can i do that?

Now if I have (for example) john john #1, john #2 in my table, they would all be moved. I would only like to move john and leave john #1 and john #2
Thanks!
EDIT:
I would like the code to work to match the variable name EXACTLY.
So if name is JOHN it must only get JOHN. If it’s JOHN #1, it must only get JOHN #1 and so on. Is this possible?

Answer by Starx

Since every name case is valid to :contains(" + name +") statement, it matches all of them.

Try this, with a :not to omit the one with “#”

$("#first_table").append($('#second_table').find("tr:contains(" + name + "):not(:contains('#'))"));

Update:

If you want to be precise about the values inside. Then use something like this

$("#second_table tr td").each(function() {
   if($(this).text() == 'John') { 
     $(this).closest("tr").appendto($("#first_table")); //find the cotaining tr and append to another table
   }
});
April 13, 2012

Wildcard for WHERE?

Question by Sam Clark

I want to setup code that does this: (* is wildcard)

SELECT * FROM * WHERE * CONTAINS '$search-query';

What MYSQL code can I use to acheive this.

Answer by Starx

There is a project called anywhereindb which can do what you want.


I am not going to create a full solution, its going to take a long time, but, I am going to create an example of what you would called

SELECT * From `tablename` WHERE * CONTAINS `$search_query`

First, lets extract the fields

$fields = array();
$query = "SELECT * FROM `yourtable` LIMIT 1;";
$result = mysql_query($query);
while ($i < mysql_num_fields($result)) {
    $info = mysql_fetch_field($result, $i);
    $fields[] = $info -> name;
}

Now prepare your query

$query = "SELECT * FROM `table` WHERE";
foreach($fields as $index => $field) {
    $fields[$index] = $field." CONTAINS '$search_query'"
}
$query .= implode(" and ", $fields);
//Finally query
$result = mysql_query($query);
...

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