...

Hi! I’m Starx

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

mysql auto increment without inserting

Question by chaft

I have a mysql table with an auto increment field. I am using the auto increment counter as an id generator across the database.

I need a way to increment the counter, get the new number but without inserting any data.

Is there an easy to do it?

Answer by Eric Petroelje

If you are using it as an ID generator, then (aside from altering the table) you’ll have to insert something to get the next value.

The easiest way to do this without inserting any data would be to do an INSERT followed by calling LAST_INSERT_ID followed by issuing a ROLLBACK. That will rollback the insert, but the next ID will still be incremented.

Answer by Starx

Use this query, to get the status of the table

SHOW TABLE STATUS LIKE 'table_name'

There will be a field Auto_increment with the value you want

Using PHP to extract the value

$result = mysql_query("SHOW TABLE STATUS LIKE 'table_name'");
$row = mysql_fetch_assoc($result);
$nextAutoIncrement = $row['Auto_increment'];
Read more

jquery ul li selector

Question by lucky13

I have this list

<ul style="display:none">
  <li id="key1" title="Look, a tool tip!">item1 with key and tooltip
  <li id="key2" class="selected">item2: selected on init
  <li id="key3" class="folder">Folder with some children
    <ul>
      <li id="key3.1">Sub-item 3.1
      <li id="key3.2">Sub-item 3.2
    </ul>

  <li id="key4" class="expanded">Document with some children (expanded on init)
    <ul>
      <li id="key4.1">Sub-item 4.1
      <li id="key4.2">Sub-item 4.2
    </ul>
</ul>

Is there any way to select each < li > by its “id” with query?

Ive tryed it like this but its now working

$("ul li:#key1").qtip({
         content: '<img src="icons/Icon.png" />'
    });

Thanx for the answers but none of the above was working.. Ive tryed this

$("ul li:first").qtip({
 content: '<img src="icons/Icon.png" />'
});

and im able to see the qtip.But only for the first on the list (key1).
Trying

$("ul li#key1").qtip({
 content: '<img src="icons/Icon.png" />'
});

or

$("#key1").qtip({
 content: '<img src="icons/Icon.png" />'
});

its not working for me.. =/

Answer by Starx

Why the extra colon :? You dont need it.

$("ul li#key1").qtip({
     content: '<img src="icons/Icon.png" />'
});
Read more

Single page or multiple pages?

Question by Ferenc Kamraš

I want to make a web service where users can make their own pages. Which is the better solution:

a. Execute everything on one page with get variables, using clean urls

or…

b. Make seperate .php files for every user

Is it a problem to have all the traffic of 500-1000 users on one page?

Answer by Your Common Sense

Second is not an option at all. It just makes no sense in the context of the dynamic web pages

Go for the first.
this is how every web site in the world works

Answer by Starx

A different way to look up what Your Common Sense has said.

Imagine you DID applied the second options, then suddenly your website booms like facebook. What do you think you will do, create over millions of pages for each user.

That is one of the worst concept NO OFFENCE, create a common page for every user to land on, control the content and elements as per them.

Read more

how to change variable when filter is on

Question by bonny

hello i would like to code a filter for an search engine on my site. now i thought it would be the best way to use a simple filter. so my code looks like:

}else if ($filter_a === 1){
        $filter = "WHERE `a` LIKE '%$a%'";
}else if ($filter_b === 1){
        $filter = "WHERE `b` LIKE '%$b%'";
}else if ($filter_c === 1){
        $filter = "WHERE `c` LIKE '%$c%'";
...
}else if ( ($filter_a === 1) && ($filter_b === 1) ){
        $filter = "WHERE `a` LIKE '%$a%' AND `b` LIKE '%$b%'";
}else if ( ($filter_a === 1) && ($filter_c === 1) ){
        $filter = "WHERE `a` LIKE '%$a%' AND `c` LIKE '%$c%'";
... and so on

the problem is that i have 5x N so the permutation of this would be 120 possibilites for $filter

is there a way how i can solve this? if there is someone who could help me out i really would appreciate. thanks a lot.

Answer by Starx

How about this way?

if ($filter_a === 1) {
        $filter[] = "`a` LIKE '%$a%'";
}
if ($filter_b === 1) {
        $filter[] = "`b` LIKE '%$b%'";
}
if ($filter_c === 1) {
        $filter[] = "`c` LIKE '%$c%'";
}
$filter = "WHERE ".implode(" AND ", $filter);
Read more

finding all iframe srcs with jquery

Question by Martin

is there a way, with jquery, to for exapmle on click, find all iframe elements, remove their src= tag, and give it back? sort of, refresh 🙂

Was looking for foreach function or something like that, but I’M rather hopeless at this 🙁

Thanks for your time,
Mart

Answer by ubercooluk

  $(document).ready(function(){ 
$('#somebuttonid').click(function(){
$("iframe").each(function() { 
        var src= $(this).attr('src');
        $(this).attr('src',src);  
});

    });
});

Answer by Starx

You can refresh all the iframe like this

$("iframe").each(function() { 
   $(this).attr('src', $(this).attr('src')); 
});
Read more

Can a jQuery selector find elements based on their data?

Question by Codemonkey

I have an application with a considerable amount of checkboxes. Each of them has a jQuery data parameter indicating which group they belong to, e.g. <input type="checkbox" class="show" data-group="zones" />

In some cases I would like to select a subset of these checkboxes based on the data they contain. Can a jQuery selector pull this of? If not, are there any other ways of doing this short of manually filtering?

Answer by Starx

After researching a bit, There is also another way, using filter()

var inputs = $('input').filter(function() { 
  return $(this).data("groups") == true 
});

Next manipulate as a whole

inputs.each(function() {
    $(this).data('groups', 'new zone');
});

Or you modify the single element

inputs[0].data('groups', 'new zone');
Read more

How to deal with multi-tab forms( with or without jquery)

Question by aoi

I have a large page/form divided as tabs, now the requirement is such that, each tab should have a save button, which will submit the fields within that tab. And there should also be a global save button which saves the data from all tabs.

My idea of doing this was, making separate forms for each tab, and use jquery serialize to post them, but some of the forms have image upload and ckeditor fields which do not play well with jquery.

Any idea,about what would be the best way to solve this?

Answer by Starx

With Using jQuery, You can submit each form one by one

$("#globalsubmit").submit(function(e){

  //simply submit all the forms one by one
  $("#form1").submit();
  $("#form2").submit();

  return false;
});

Or Find all the form and submit at once

$("#globalsubmit").submit(function(e){

  $("form").each(function() { $(this).submit(); });

  return false;
});

Without jQuery. Here are the few steps you might want to follow.

  1. Submit the form, do not process the data, but store in the session.
  2. Collect all the data from all the form in the session and at the end Submit the data as a bulk.
Read more

Call function when adding a div to my site

Question by ifsession

I have a script which adds a div to my site when I upload a photo. Now I wan’t to call a function when this div gets added. I’ve tried this, but it doesn’t work.

var divImg = document.createElement("div");
divImg.id = "divimg";
newImg.setAttribute('onload','urediBtnx(this)');
divImg.innerHTML = '<a href=""><img src="/imago/images/btnx.gif" class="btnx"></a>';

It should call this function:

function urediBtnx(dv){
    alert("blabla");
}

The thing is, if I do it this way, then the function gets called, but I need to get the div, not the img.

divImg.innerHTML = '<a href=""><img onload="urediBtnx(this)" src="/imago/images/btnx.gif" class="btnx"></a>';

Answer by Christophe

You can attach onload event handlers to element like body, img, script or iframe, but not to a div.

In your scenario, the div is the grand-parent of your image. You can keep the onload on the image, then use this.parentNode.parentNode to reach the div.

Answer by Starx

Div does not have an onload event. They are only availabe for <body>, <frame>, <frameset>, <iframe>, <img>, <link> or <script>.

Subsequently calling the function after its added to the DOM is a way.

parent.appendChild(divImg);
urediBtnx();

The possibility of a callback can be gained if you are jQuery library

$("#parent").append($(divImg), function() {
    urediBtnx();
});
Read more

MySQL query where a pair of values does not exist in another table

Question by Evan Johnson

I’m trying to preform a SELECT query that grabs data from two tables only if certain data does not exist in a third table. Here are the three tables:

Blogs

id   |   blog_name

Featured Feeds

id   |   blog_id

Subscriptions

id   |   member_id   |   blog_id

I want to return the blog_id and blog_name of those listed in the Featured Feeds table, but exclude Featured Feeds that the user (member_id) is already subscribed to in the Subscriptions table. Here is the query I have thus far (using 3 for the member_id – please note the table names are slightly different than above):

SELECT featured_feeds.blog_id, featured_feeds.category_id, blogs.blog_name FROM featured_feeds LEFT JOIN blogs ON featured_feeds.blog_id = blogs.blog_id LEFT JOIN subscriptions ON subscriptions.blog_id = featured_feeds.blog_id WHERE subscriptions.blog_id != '3' ORDER BY blogs.blog_name ASC

This query doesn’t exclude the Featured Feeds that the user is subscribed to if another user is subscribed to the same feeds. For example, if Subscriptions table had the following rows:

1   |   3   |   4
2   |   2   |   4

If blog_id 4 is listed in the featured feeds, my query will still return that blog_id due to the second row of data above. Do I need to use a GROUP?

Thanks in advance!

Answer by hkf

SELECT b.id, b.blog_name
FROM blogs b
INNER JOIN featured f on f.blog_id = b.id
WHERE b.id NOT IN (SELECT id FROM subscriptions WHERE member_id = 3)

Answer by Starx

Do a outer a left join, omitting the matching results.

SELECT * FROM `featured` f
LEFT OUTER JOIN `subscription` s
ON f.blog_id = s.blog_id
WHERE s.id IS null
Read more
...

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