Binary tree in MySQL for MLM

Recently for a student, I was asked to explain the design considerations of a Binary Tree which was to be used in an MLM solution. About 10 years back it was a nightmare, and in my career, I was lucky to get that privilage for more than a dozen times with varying schemes and structures. But now with MySQL having procedures, and functions, the tree design and related functions were a breze. Reproducing it here for future reference. This is in no way a complete solution, but just bits and pieces which may even be discarded as crap.

CREATE TABLE  `binTree` (
  `nodeid` int(10) unsigned NOT NULL auto_increment,
  `lnode` int(10) unsigned NOT NULL default '0',
  `rnode` int(10) unsigned NOT NULL default '0',
  `pnode` int(10) unsigned NOT NULL default '0',
  `pside` enum('l','r') NOT NULL default 'l',
  `tLevel` int(10) unsigned NOT NULL default '1',
  PRIMARY KEY  (`nodeid`),
  KEY `parent` (`pnode`),
  KEY `treelevel` (`tLevel`),
  KEY `lside` (`lnode`),
  KEY `rside` (`rnode`)
) ENGINE=MyISAM;

The above is the basic structure of the tree table, and some complementary functions and procedures are accompanied to make the usage simple, otherwise would be a herculian task for the developer to do the same.
Continue reading “Binary tree in MySQL for MLM”

Threading PHP scripts using OS facilities

Recently while developing systems which consumes webservices, after a lot of optimizations were put into our code, we resorted to do some sort of benchmarking. From the begenning itself, we had proper loggers which were logging the activities to database tables, and verbose text files. For benchmarking of the systems we use xdebug, and our Open PHP Myprofiler. The code coverage analysis by xdebug, as well as cache grind showed our bottleneck was our webservice provider. While checking out, we also found that we could reduce the waiting time for webservices by requesting smaller data set from the webservice.
Continue reading “Threading PHP scripts using OS facilities”

Prototype based date picker

We had started optimizing drive for the projects of Reserway Technologies, and the work being done on the demo site. For the initial prototype, we used a dual datepicker developed in flex, which interacts with the html using javascript calls. We used this because the component was already available, and just adding a couple of handlers could do our job, and we could concentrate more on the abstraction of GDS integration. Well we are in a state where the backend is almost stable and we need more performance drive on the frontend. The first choice was ofcouse linking the libraries to google cdn. Then started cutting the curves. The biggest bottleneck was the whopping 220Kb for the flash control. I was particular to use a simple datepicker, which could be styled using css, and would be better if used the prototype library.
Continue reading “Prototype based date picker”

Using TagTheNet to generate tags on WordPress

Recently on Kerala News by Asianet, though the wp-simple-tags was there, the posts were not being tagged automatically. And on a detailed check I found it was due to a misconfiguration, and once the same was done properly the tagging started smoothly. But already a set of 8K posts were there with no or unrelated tags mistakes and un awareness of the operators who were posting to the site. Now I wanted these to be tagged properly.
I had already used the word-twit plugin and done some mods to the same, so it was more eaiser for me. Just went through the original code of word-twit and salvaged a small script which is attached here with as download.
require_once( dirname(__FILE__) . '/wp-load.php' );
The code above loads the wordpress system and initializes the wp variables and connects to the database.
global $wpdb;
Makesure we have the database abstraction object from wordpress, to select the posts, and do all the required manipulations on the same.
require_once( ABSPATH 'wp-includes/class-snoopy.php' );
Include the inbuilt snoopy class, to mimic a web browser with the most simplicity.
Continue reading “Using TagTheNet to generate tags on WordPress”

WordPress Super Cache on lighttpd

While trying to speed up Select Articles, I got stuck, torn in between WP Super Cache and lighttpd. Already we at Saturn are using lighttpd on several dedicated servers, and know the potential and benefit of having this over Apache though the flexiblity of having a perdirectory configuration at the programmers choice was a big question. A quick search on the Google landed me to a technical blog, notepad and personal web page by Asterios Katsifodimos.

Trying to get WP Super Cache & WordPress working on my fast lighttpd server, I came into problems, mainly because of lighttpd’s lack of (Apache’s version of) the mod_rewrite module. The static files that were created from the cache were not statically served from wordpress. The problem is that in order to use them, the PHP fcgi was called for each request. So, why would we have to call PHP every time that a file can be completely statically provided by the web server?

Read the rest of his version Installing WP Super Cache with lighttpd

function array_flatten

This is not much to brag about, still we had a very large dataset as variable dimensional and variable depth, which we wanted to be sent to a custom function using prototype.js. For the data exchange we use JSON, but the varying depth and related tags, made us to do like hell in the javascript templating that we had to find a method to flatten the array in such a way that each of the associative keys would not be mangled.

The final out come is function array_flatten


function array_flatten($p, $ki = ''){
    if($ki !== '') $ki .= '-';
    $rv = array();
    foreach($p as $k => $v){
        if(is_array($v))
            $rv = array_merge($rv, array_flatten($v, $ki.$k));
        else
            $rv[$ki . $k] = $v;
    }
    return $rv;
}

The flattened dataset, helped us to do rendering in less than half the iterations, and the application response was more better after using this.

Annoying php error related to headers

Cannot modify header information – headers already sent by (output started at /path/to/the/php/file.php:line) in /path/to/the/php/file.php on line newline

Okay the normal case would be to check the file thouroghly and make sure there is nothing that is outputted at the specified line number. But what if the line is pointing to the first line in the current file, and there is nothing visible, well the culprit may be a windows based editing software which converted the php file into UTF-8.

So the file encoding may destroy the php script. Changin the encoding back to ANSI would clear the problem.

WordTwit – just finished integrating to a couple of my wordpress sites

I have just finished integrating wordtwit to asianetindia.com as well as this blog. There is nothing much to hoot and howl about it, other than I just made some tweaks here and there. One being the addition of auto hash tagging for twitter with the help of tag the net. And another one to replace the url compression with a similar service which I started just on a fancy, also I replaced those since I was not interested in retaining the included small url options.

While working for the auto hash tagging, I found that the function twit_hit_server, near about line 215, was not updating the $output variable, with the results in case the method was post, so I just added that $output = $snoopy->results; to the conditions inside if ($post) / if ($result).

Then changed

$message = str_replace( '[title]', get_the_title(), $message );

to

$message = str_replace( '[title]', wordtwit_tagify_title(get_the_title()), $message );

inside the function post_now_published definition.

The code listing for wordtwit_tagify_title is

function wordtwit_tagify_title($text){
  
$output '';
  if(
strlen($text) > 110$text substr($text0100) . ' ...';
  
twit_hit_server'https://tagthe.net/api/'''''$outputtrue, array( 'text' => $text'view' => 'json' ) );
  
$res = @json_decode($output);
  if(!isset(
$res->memes[0]->dimensions->topic[0]))
    return 
$text;
  
$topTag $res->memes[0]->dimensions->topic[0];
 
  
$g strpos($text$topTag);
 
  return 
substr($text0$g) . '#' substr($text$g);
  
}

WordTwit is a plugin that utilizes the Twitter API to automatically push a published post to your Twitter account as a tweet. It allows all your Twitter contacts to keep up to date with your blog postings.

First taste of bbpress – was sweet but getting sour

Hey hey.. when we had to implement a bulletin board for an institution, the first thought was for phpbb, but the time limitation as well as lack of resources to hack through a completely new code took the turn and prompted us to take a new course. The bb press way. Atleast it was the same language written and used in the same way we were handling for more than a year. Well _ck_ should be thanked for all the goodies. And when we find bugs or errors, we all should help each other to make the system more better.

When I was using the bbPress Attachments, trying to delete a post, even by admin or moderator, would confront with an SQL error. The same error was reported by Joseph, for which _ck_ responded with downloading a new version from the trunk. For me even after the new version came in, I was getting the error. Continue reading “First taste of bbpress – was sweet but getting sour”