HOBALL 筆記簿

April 1, 2010

PHP: Run two foreach loops at the same time

Filed under: PHP — Tags: , , , — hoball @ 11:25 am

@reference: http://stackoverflow.com/questions/2556998/how-do-you-combine-two-foreach-loops-into-one

SPL MultipleIterator
@reference: http://docs.php.net/class.multipleiterator

// ArrayIterator is just an example, could be any Iterator.
$a1 = new ArrayIterator(array(1, 2, 3, 4, 5, 6));
$a2 = new ArrayIterator(array(11, 12, 13, 14, 15, 16));

$it = new MultipleIterator;
$it->attachIterator($a1);
$it->attachIterator($a2);

foreach($it as $e) {
  echo $e[0], ' | ', $e[1], "\n";
}

Prints

1 | 11
2 | 12
3 | 13
4 | 14
5 | 15
6 | 16

March 30, 2010

How MySQL executes ORDER BY

Filed under: SQL — Tags: , — hoball @ 3:30 pm

Reference: http://s.petrunia.net/blog/?p=24

March 12, 2010

ip2long vs INET_ATON

Filed under: PHP, SQL — Tags: , , , — hoball @ 9:15 pm

Source: http://mwillis.co.uk/mysql/ip2long-vs-inet_aton/

PHP ip2long() sometimes return negative integers,
while MySQL INET_ATON() function returns only positive numbers.

Ensure PHP ip2long() returns only positive intergers:

sprintf("%u", ip2long("254.254.254.254"));
$ip = ip2long($ip_address);
if ($ip < 0){ $ip += 4294967296; }

November 6, 2009

MySQL changing runtime variables without restart

Filed under: SQL — Tags: , , — hoball @ 5:18 pm

Setting Global variable, the followings are identical. (effective until service restart)

mysql> SET GLOBAL long_query_time = 3;
mysql> SET @@global. long_query_time = 3;

Setting Session variable, the followings are identical.  (effective until your session ends. Changes are not visible to other clients) 

mysql> SET SESSION long_query_time = 3;
mysql> SET @@session. long_query_time = 3;
mysql> SET @@long_query_time = 3;

More: MySQL.com Dynamic system variables

Reference: http://crazytoon.com/2007/07/23/mysql-changing-runtime-variables-with-out-restarting-mysql-server/

October 24, 2009

MySQL Memory Usage

Filed under: SQL — Tags: , , , — hoball @ 10:25 am

For tuning MySQL variables,

max_memory_needed = core_mysql + global_values + (thread_buffers * max_connections)

max_memory_needed = core_mysql
+ key_buffer_size
+ innodb_buffer_pool_size
+ innnodb_additional_memory_pool_size
+ innodb_log_buffer_size
+ max_tmp_tables * min(tmp_table_size,max_heap_table_size)
+ query_cache_size
+ 3 * myisam_sort_buffer_size
+ max_connections * ( read_buffer_size + join_buffer_size + read_rnd_buffer_size + thread_stack + (2 * max_packet_size) ))

Source:
http://www.pythian.com/news/1455/mysql-memory-consumption
http://www.mysqlperformanceblog.com/2006/05/17/mysql-server-memory-usage/

October 23, 2009

PHP, Connection Timeout for XMLReader->open()

Filed under: PHP — Tags: , , — hoball @ 3:23 pm

All XML extensions use the PHP streams.

To set a connection timeout, we can either edit the php.ini, or use ini_set() to configure it on runtime.

if ( (int) ini_get('default_socket_timeout') > 5)
{
    ini_set('default_socket_timeout',5);
}

http://bugs.php.net/bug.php?id=45785

September 29, 2009

HTML Tidy on linux under XAMPP

Filed under: PHP — Tags: , , , — hoball @ 11:53 pm

Installing tidy on PHP5 under linux is pain. The manual says  “compile with –with-tidy”, but it is not really happening.

Finally I made it work on my fedora (using XAMPP) by this method:

# Install libtidy
yum -y install tidy

# Download PHP 5 source
http://www.php.net/downloads.php

# Unpack the source
tar xvzf php-5.3.0.tar.gz

# Configure tidy for installed php5 API
cd php-5.3.0/ext/tidy
/opt/lampp/bin/phpize

# Stay at the same folder, Configure & Compile the source
./configure --with-php-config=/opt/lampp/bin/php-config --with-tidy
make clean    <-- Without this the compile builds a bad module for some reason
make
make install

# Install module into php.ini - Add:: extension="tidy.so"
vi /opt/lampp/etc/php.ini

References:

ubuntuforums.org: LAMP: php5-tidy doesn’t exist!

newsn.net 给AndLinux下面的xampp装tidy模块的经验 (Chinese)

/opt/lampp/etc/php.i

July 24, 2009

For-loop with two counting variables

Filed under: PHP — Tags: , — hoball @ 2:38 pm

Perma Link: http://stackoverflow.com/questions/1172553/php-for-loop-with-2-variables/1172565#1172565

for ($i=0, $k=10; $i<=10 ; $i++, $k--) {
    echo "Var " . $i . " is " . $k . "<br>";
}

Gumbo, Author of this code snippet:
The two variables $i and $k are initialized with 0 and 10 respectively. 
At the end of each each loop $i will be incremented by one ($i++) and $k decremented by one ($k—).
So $i will have the values 0, 1, …, 10 and $k the values 10, 9, …, 0.

July 23, 2009

Javascript – catpuring mouse click

Filed under: Javascript — Tags: , , — hoball @ 1:00 pm

While developing my app, I wrote the protype using jQuery, but I only need one method from jQuery, which is click(). I wanted to build it with pure javascript, but have no idea how…(what a newbie!)

So I went to StackOverflow to ask my question, here is the permalink: www.stackoverflow.com

Summary of answers:

var p = document.getElementsByTagName("p");
for(var i=0; i<p.length; i++){
    p[i].onclick = function(){
        alert("p is clicked and the id is " + this.id);
    }
}

Breton also posted a fully featured suggestion of the jQuery click() method:

it looks a little bit like you miss more than just the click function of jQuery. You also miss jquery’s selector engine, chaining, and automatic iteration across collections of objects. With a bit more effort you can minimally reproduce some of those things as well.

var myClickCapture = function (selector) {
    var method, name,iterator;
    if(selector.substr(0,1) === "#") {
       method = "getElementById";
       name = selector.substr(1);
       iterator = function(fn) { fn(document[method](name));  };
    } else {
       method = "getElementsByTagName";
       name = selector;
       iterator = function(fn) {
          var i,c = document[method](name);
          for(i=0;i<c.length;i++){
             fn(c[i]);
       };
    };
    myClickCapture.click = function (fn){
         iterator(function(e){
            e.onclick=fn;
         })
    } 

    return myClickCapture;

}

I haven’t tested the code, but in theory, it gets you something like this:

myClickCapture("x").click(function(e){ alert("element clicked") });

Hopefully this gives you a sense of the sorts of things jquery is doing under the covers.

July 22, 2009

Javascript fix for the failure of IE to pass HTTP_REFERER in setting new URL on location.href

Filed under: Javascript — Tags: , , , , — hoball @ 12:49 pm

Setting a new URL using location.href will change the HTTP_REFERER on Firefox/Opera/Safari, but NOT on IE

Here is the hack to fix this (quoted from here):

/* location.href FIX for MSIE */
//http://blogs.msdn.com/ie/archive/2008/10/30/hot-off-the-press-codefocus-on-ie8.aspx#9028128
function setHref(url) {
    var isIE = (navigator.appName.indexOf("Microsoft")!=-1) ? true : false;

    if (!isIE) {
        //Standards based browsers
        parent.location.href = url; // Set target: self. / parent. / top.
    } else {
        var lha = document.getElementById(‘_lha’);
            if(!lha){
                lha = document.createElement(‘a’);
                lha.id = ‘_lha’;
                lha.target = ‘_parent’;  // Set target: for IE
                document.body.appendChild(lha);
            }
            lha.href = url;
            lha.click();
    }
}

Older Posts »

Create a free website or blog at WordPress.com.