Pages

Friday, October 28, 2011

Show Hide HTML Contents inside Nested Loops

Show hide contents with Javascript is not a big issue in php, HTML development. But when it comes to dynamically changing environment, contents inside a loop which the number of rows not known, it gets little difficult. Because Javascripts are running on client side and it will do the job through content IDs. Below code will show you how to show/hide contents inside a loop where the number of rows not predefined and also the number of rows inside a single row also not predefined. Basically a nested loop.

This is the Javascript code inside the <head>

<script type="text/javascript">
function toggle(div_id, count)
{
    for (i=0;i<=count;i++)
    {
        var a=div_id+"-"+i;
        var divval = document.getElementById(a);
        if (divval != null)
        {
            if ( divval.style.display == 'none' )
            {   
                divval.style.display = 'block';
            }
            else
            {
                divval.style.display = 'none';
            }
        }
    }

</script>

And here is the php,

$count = 0;
while(condition for loop1)
    {
            $count2 = 0;
            while(condition for loop2)
            {
                       ?>
                       
                        <tr  id='<? echo $count."-".$count2 ?>' style="display:none"><td></td></tr>                       
                        <?
                         $count2 = $count2+1;
              }
              $count = $count+1;
              ?>
             <tr id='<? echo $count."-more" ?>' style="display:block"; align="right"><td><img src="./images/more.png" align="right" onmouseover="this.src='./images/moremo.png';" onmouseout="this.src='./images/more.png';" onclick="toggle('<? echo $count ?>', '<? echo $count2 ?>')" height="17" width="40"  /></td><td>&nbsp;</td></tr>
             <?
     }

The basic idea of this code is a 'show more records' button inside the first loop. (one button per record) According to the button click event it will show/hide the result of the second loop.
What I have done here is sending the div_id (count of the first loop) and the number of records in the second loop to the JScript. The JScript will run another loop for the count of the second mentioned loop and do the necessary. See how I have created dynamic div ids.

Simple but powerful JavaScripts
Regards,
  

Tuesday, September 20, 2011

Server side (web) clock on your client's browser

There you can find many number or Javascript clocks available on the Internet, graphical as well as numerical. But all they do is showing the clock in the client's machine. What if the client machine's time is not correct? or the viewer is from another region or timezone but you need to display the clock according to your regional or server time. Here is the solution.

I'm calling this in my page onload event;
<BODY onLoad="startTimee(<? echo $a ?>, <? echo $b ?>, <? echo $c ?>)">

Here is my piece of PHP code, this should be located before the <body> tag;
<?
$a = date('H');
$b = date('i');
$c = date('s');
?>

Finally my simple Javascript, displays the server ticking clock;
<script type="text/javascript">
var a;
var b;
var c;
<! --This should be defined before all JS functions so it will work as global values for JS. -->


function startTimee(x, y, z)
{


var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();
 a=x-h;
 b=y-m;
 c=z-s;
startTime();
}


function startTime()
{
var today=new Date();
var h=today.getHours();
var m=today.getMinutes();
var s=today.getSeconds();


h=h+(a);
m=m+(b);
s=s+(c);


if (s>59)
{
s=(s)-60;
m=m+1;
}


if (s<0)
{
s=60+(s);
m=m-1;
}


if (m>59)
{
m=(m)-60;
h=h+1;
}


if (m<0)
{
m=60+(m);
h=h-1;
}


if (h>12)
{
h=h-12;
}


// add a zero in front of numbers<10
m=checkTime(m);
s=checkTime(s);
document.getElementById('txt').innerHTML=h+":"+m+":"+s;
t=setTimeout('startTime()',500);
}


function checkTime(i)
{
if (i<10)
  {
  i="0" + i;
  }
return i;
}
</script>

What I all do is taking the time difference with my server and clients machine on page load and assignees them to JS global values. Finally add (or subtract) it to the normal JS clock. Simple as that, (but powerful :) ) So you can embed this with any JS clock on the web as your requirement.

Note: the actual time may be differ + or - 5 to 10 seconds depending on the page load time.

Regards,

Monday, September 12, 2011

Remove and/or prevent inserting invalid characters on client side

This simple Javascript will prevent inserting ' < ' ' > ' and ' " ' characters from your HTML form,

<SCRIPT language=Javascript>
<!--
      function reminvalid(evt)
      {
         var charCode = (evt.which) ? evt.which : event.keyCode
         if ((charCode < 60 || charCode > 62) && charCode != 34)
            return true;

         return false;
      }

//-->
</SCRIPT>


The primary requirement of this code is to prevent cross site scripting on the web form. You can modify the char code(s) as the requirement. 

Unfortunately this won't prevent copy and paste insertion. So I had to embed this code too..

function delinvalid(string)
    {   
        output =  string.replace(/[<>"]+/g,' ');
        return output;
    }

This will remove the unwanted characters and replace by a space (or any other character you specify after the comma, in this case ' ')

You can use it this way too,


string.replace(/[^a-zA-Z 0-9]+/g,' ');

Then it will remove everything other than a-z A-Z 0-9 and space.

Remember to use the below notation if you want to replace the '^' sign.

string.replace(/\^/g, '\r\n');

Here is my input element,

<input name="***" type="text" size="**" id="**" onkeypress="return reminvalid(event)" onChange="this.value=delinvalid(this.value)">

Simple and powerful Javascripts....

Regards.

Tuesday, June 28, 2011

Install Squid on your home directory

Squid is a open source proxy server mostly running on linux environment  (also there is a version for windows too). The default path that the squid will install is /usr/local/squid/. You can change the path by using ./configure --prefix=/some/other/directory/squid at the compilation of squid. (Please refer http://wiki.squid-cache.org/SquidFaq/CompilingSquid)

Occasionally some may want to install squid on the home directory. As usual you need to set the permission squid/var/logs  & cache_dir directories to the cache_effective_user you have configured at squid.conf. the default is squid:squid. The problem arise here is it still gives an error saying permission denied.

The solution is easy. Change your cache_effective_user to your home directory name. And set permission of above mentioned directories to the same. Ya.. It works....

Tuesday, April 19, 2011

Enable Sri Lanka GPS navigation on your Nokia device

There are so many GPS enabled mobile devices coming to Sri Lanka but unfortunately Nokia doesn't provide the map of SL at there OVI store. Last week I got some time to play with my Nokia 5800 and thought to find someway to install an offline map works in SL. First I installed Google maps but unfortunately it doesn't work offline on Symbian OSes. (It says it works on Android). After few more tryouts I could find some posts regarding Garmin XT on mobile devices and finally it worked.
So below this post actually not something I invented but provide A to Z guidance to install SL map on your Nokia device without browsing so many posts, articles and wasting time. 

Download file contains

* GarminMobileXTforSymbianS603rdEdition_50040.exe       Download
* GarminMobileXTFreeBasemap_4xxxx.exe       Download
* GarminMobileXTSupportFiles_4xxxx.exe         Download
* garmin_kgen.exe            Download
* Download the Garmin Sri Lankan Map From Here.
             For devices with limited memory, use this map instead. This contains only A-Grade Roads, and contains most of the important locations in Colombo. Not required to use, if you are on a Nokia.
* 5800_manual.pdf
* GarminMobile20_GarminMobileXTOwnersManual.pdf

Note: You can skip part 1

1. Setting up Apparatus, The Nokia 5800

1. Back up all data using PC Suite (go to Nokia website).
2. Soft reset by pressing *#7370#. Should take about 2-3 mins and you lose all your data (except on memory card). But don’t worry, you have back up remember?
3. On your Nokia 5800, go to Application > Location > Positioning > Positioning Methods. Enable “Assisted GPS” and “Integrated GPS” ONLY. This will ensure you don’t get charge using WAP or 3G network for Internet.
4. Go to Application > Location > GPS Data > Position > Options > Satellite status. This will show the satellite signals you’re receiving. Be sure you are outdoor in an open area. Balcony doesn’t work well, believe me I’ve tried. Once you’ve got AT LEAST 4 satellite signal, go back indoor, you don’t want people to think you’re crazy, waving your phone around.
5. Now you’re ready for GarminXT.


2. Installing GarminXT on Nokia 5800.

1. Once you’ve downloaded, unzip the files to someplace accessible preferably on your desktop. If you haven’t, please download it.
2. Connect your phone via USB and select ‘Mass storage mode’.
3. From you PC, double click “GarminMobileXTforSymbianS603rdEdition_50040.exe” to install and wait.
4. Double click “GarminMobileXTFreeBasemap_4xxxx.exe” to install. Wait..
5. Double click “GarminMobileXTSupportFiles_4xxxx.exe” to install.
6. Remove USB connection between PC and phone. When prompted ‘Launch GarminXT’ do not click Yes, just Cancel.
7. From your phone, go to Menu > Settings > Application Manager > Installation files. There should be GarminXT.sis file. Install it.


3. Cracking GarminXT on Nokia 5800.

1. Run GarminXT from your phone at Menu > Application > Garmin. It will prompt you with some settings. Choose your preferable.
2. Select “Use other device” when it prompts you to buy or trial. You will get a screen, “Where To?” and “View map”
3. In GarminXT, go to Tools > Settings > About. Jot down “Card ID”, you will need it. Exit GarminXT
4. On your PC, run “garmin_kgen.exe” and enter the “Card ID” from previous steps.
5. Generate it and you will get a string of codes. Copy the codes to Notepad and save it as “sw.unl”. *Be sure you don’t save it as sw.unl.txt”*
6. Connect your phone to PC and select “Mass storage mode”.
7. Copy “sw.unl” to Garmin folder. Usually located at E:/Garmin.
9. Copy “gmapsupp.img” from downloaded file to Garmin folder. “gmapsupp.img” is the Sri Lankan map.
8. Disconnect your phone from PC.



4. Restore your Nokia 5800

1. Reconnect your phone via PC Suite.
2. Restore your settings through back up.


5. Setting up GarminXT on Nokia 5800

1. Run GarminXT on your phone.
2. Go to Tools > Settings > System > Remote GPS. Change to “Use Attached GPS” by disabling the “Bluetooth GPS”.
3. Go to Tools > Manage My Data > Map Sets > MapSource. Make sure all maps are enabled.
4. Toying GarminXT on Nokia 5800.



Run GarminXT,
Select “View Map” and wait for a while. The satellite will find you. It took me 5 mins or so. Just have patience and faith.
Now you’ll see where you are.
Go back and select “Where To?”. If you’re at home and wants GarminXT to store you current location as home, then select “Home”
Pat yourself and say “Well done”. The rest is up to you.


To add the Detailed overlay map follow the below guidelines.


1. Download the Garmin Overlay Map from Here.
2. Copy and Paste it to the Root of the Garmin Folder in your MMC.
3. Launch Garmin Mobile XT.
4. Go to Tools (Spanner Icon) -> Settings -> Map and Scroll all the way down till you see Map Features option.
5. Inside Map Features locate Small Town (Off by Default) – Open it and Set it to Small Label.
6. Exit and View Map to see updated Details.
7. Details will only be visible at and below Zoom Level 3.

If you want to install some locked maps here is the way.. This example is how to install the India map. (Have not try personally)
How to install Indian map in Garmin Mobile XT

You can download the Indian Map from this torrent: http://thepiratebay.org/torrent/5272640/Garmin_MapSource_City_Navigator_India_2009.30_img_version
Steps to follow
  1. Connect mobile to PC in Mass Storage mode. Go to: Computer\Nokia 5800 XpressMusic\Memory card\Garmin
  2. There will be two .img files GMAPBMAP.IMG-Base map and GMAPSUPP.IMG-Malaysian map (which comes with the installation)
  3. Download the indian map image from the torrent.
  4. Rename it to: GMAPSUP2.IMG
  5. Copy to Computer\Nokia 5800 XpressMusic\Memory card\Garmin
  6. Safely disconnect mobile from PC.
  7. Run the Garmin KeyGen.exe in PC. Type your UnitID (already know where is this. See the first post)
  8. First, click generate for software unlock code.
  9. Select map product as custom mapset.
  10. It will show a dialog box. Type the FID as 281
  11. Generate map unlock code.
  12. Copy the newly generated code and save it in GMAPSUP2.unl
  13. Copy to Computer\Nokia 5800 XpressMusic\Memory card\Garmin
  14. Enter the two codes in Garmin XT when asked (better save these in a text file.)
  15. Good to go. You are done!!

For more details please visit http://gpssrilanka.blogspot.com/. Finally we must be really thankful them for their clear guidance.

Friday, March 4, 2011

How to receive and save SMS through Kannel

This is the relevant configuration part of the kannel.conf file;

group = sms-service
keyword = default
#keyword-regex = .*
catch-all = yes
max-messages = 0
get-url = "http://localhost:23456/sms.php?ORIGINATOR=%p&RECEIVER=%P&SMS=%a&SOURCE_PRV=%o"



At local host (same as where i run my kannel server) i have this code in the file sms.php;

$query = "insert into sms set sender='".$_GET["ORIGINATOR"]."', receiver='".$_GET["RECEIVER"]."', msg='".$_GET["SMS"]."', source_prv='".$_GET["SOURCE_PRV"]."'";
$result = mysql_query($query);
if(!$result) die(sql_error());

Make sure that you have defined your database connectivity parameters before this code. What kannel does is just he receive a SMS it calls the URL defined as the get-url in the .conf file. And passing the parameters in GET method. Now you are able to receive and save a SMS through kannel.