Notes by JDeveloper for JDeveloper
Once you master it, you're gonna like it

Small snippet to track load time in PHP

Written by Viktor Iwan

There are times when you need to debug on what time does a process required. This small snippet perhaps can show you how:

 $time = microtime();
 $time = explode(' ', $time);
 $time = $time[1] + $time[0];
 $start = $time;


//SOME PROCESS GOES HERE


  $time = microtime();
  $time = explode(' ', $time);
  $time = $time[1] + $time[0];
  $finish = $time;
  $total_time = round(($finish - $start), 4);

echo 'Process finished in '.$total_time.' seconds.';
die();

This snippet helps me a ton when i have to debug a complaint from one of my client... and hope it can helps you too!


Becareful with Subquery Select with more than 1 level deep

Written by Viktor Iwan

I recently found a heavy loading of a page in mysite which using iJoomla Component named Guru. After profiling with Zend Studio, i found out the real problem cause by 2 level subquery

i'm talking about queries like this:

$sql ="SELECT id FROM #__guru_task WHERE id IN (SELECT media_id FROM #__guru_mediarel WHERE type = 'dtask' AND type_id in (SELECT id FROM #__guru_days WHERE pid =".intval($id).") ) ";

It's 2 level of subquery with 'IN' statement...

To solve this kind of problem, you only have to change the IN statement into multiple queries, like this:

$sql ="SELECT id FROM #__guru_days WHERE pid =".$id;
$db->setQuery($sql);
$db->query();
$temp = $db->loadColumn();
		
$sql ="SELECT media_id FROM #__guru_mediarel WHERE type = 'dtask' AND type_id in (".implode(',',$temp).")";
$db->setQuery($sql);
$db->query();
$temp = $db->loadColumn();
		
$sql ="SELECT id FROM #__guru_task WHERE id IN (".implode(',',$temp).")";
$db->setQuery($sql);
$db->query();
$all_lessons = $db->loadColumn();

 

 After some optimization with this approach, i'm able to change this:

 

Into this:

 

Look how mysql perform for the same result... 25 seconds load into 0.8 seconds load... That Fast !!!

Moral of the story: Don't use subquery with 'IN' statement more than 1 level. You can search on google with keyword "MySQL Subquery Slow Performance" and you'll see more people suggest the same.


Create Intro Text / Split Text Automatically

Written by Viktor Iwan

So if you build a news content type, you probably think you need two field to store intro and fulltext.

Well, with this script you don't have to build two field, just use introtext, and we can create the introtext automatically.

<?php 
$intro=strip_tags($item->renderField($field[4]));
if (strlen($intro) > 200) {

// truncate string
$stringCut = substr($intro, 0, 200);

// make sure it ends in a word so "assassinate" doesn't become "ass..."
$intro = substr($stringCut, 0, strrpos($stringCut, ' ')).'[..]';
}
echo $intro;
?>

Some Notes for you:

  • strip_tags is use to strip-out (erase) all the html tag, so you'll get pure text
  • you need to cut/split the intro text on the white-space, not in the word