2010/08/24

Insert code

<?php
//post without border to see what happen
$tool = 'Source Code Formatter';
$auther = 'Amer Gerzic';
//all syntax highlight is using inline css
?>
I did set up border, and wanted to see if I can apply css to it, then after the template change, I only need to update css file.

2010/08/17

ie hack

As an web developer, I wish there was only one browser, then we should not worry about the crossing browser compatible issue. But on the other hand, if there was only one browser, then it would be a very bad thing for the end user. So web developers are talking about ie-hack or firefox-hack, or opera-hack. But most of developers probably like firefox more than IE, who start from Firefox, and then test, tweak and fix css problem on another browser. So do I.

If don’t want to write a separate css file for IE using conditional comments, then you might use a simple character hack.

2010/07/09

Small php script in Smarty templates

Sometimes, we wanted to run a small PHP scripts inside Smarty templates. That’s good that we don’t need to touch PHP source file to add smarty variables. Here is an example,

In the PHP source code, I have smarty variable named phpvar,

<?php
$phpvar = "Hello World";
$smarty->assign('phpvar', $phpvar);
?>

And in the smarty tempate file, I wanted to split phpvar into tow variable. Sure, you can update source code, to include 2 variables, but sometime, you may do it in the template file. Here is how,

{if preg_match("/(.+)\s+(.+)/", $phpvar, $matches)}
<li>{$matches.1}</li>
<li>{$matches.2}</li>
{/if}

 

2010/06/18

http://ajax.googleapis.com is down

Many sites are hosting their javascript libraries on googleapis.com, such as jquery, which is supposed to be faster because googleapis is CDN (Content Distribution Network), however, what happen if googleapis.com is down.

Today, I see this problem, I went to nettus, slickdeals, they are extremely slow, I see this at the Firefox process bar,

connecting to ajax.googleapis.com...

Initially, I thought it is my ISP problem, I could not open “ajax.googleapis.com” at all. Then I tried http://downforeveryoneorjustme.com/, which told me, the site looks down for everyone not just for me.

I also went to Twitter to see if people are talking about this, unfortunately, their site also uses googleapis, and the search seems broken.

Anyone have the same problem?

By the time (1:55pmEST, 06-18-2010) I am writing this, the googleapis.com looks down for me for about 30 mins.

Updated: 2:00pm EST 06-18-2010, looks like the server is back. Not sure it is just my ISP problem, because ajax.googleapis.com’s home page is a not found page (BTW, I got timeout error when there was a problem), downforeveryoneorjustme.com may always say that it looks down.

2010/05/26

mysql get month from UNIX TIME

Try to count total registers in a certain month from members table, but the regdate is in UNIX TIME STAMP. Here is a solution using MYSQL's function FROM_UNIXTIME

SELECT count( * ) FROM `members` WHERE month( FROM_UNIXTIME( `regdate` ) ) =12

2010/04/09

PHP get ip function

A pretty good way to get IP, especially, your site has different servers, i.e. load balancing.

 function get_ip(){
    if (isset($_GET['ip'])) {
       $ip = stripslashes($_GET['ip']);
    } elseif (isset($_SERVER) and !empty($_SERVER)) {
        if (isset($_SERVER['HTTP_X_FORWARDED_FOR'])) {
            $ip = $_SERVER['HTTP_X_FORWARDED_FOR'];
        } else if (isset($_SERVER['HTTP_CLIENT_IP'])) {
            $ip = $_SERVER['HTTP_CLIENT_IP'];
        } else {
            $ip = $_SERVER['REMOTE_ADDR'];
        }
    } else {
        if (getenv('HTTP_X_FORWARDED_FOR')) {
            $ip = getenv('HTTP_X_FORWARDED_FOR');
        } else if (getenv('HTTP_CLIENT_IP')) {
            $ip = getenv('HTTP_CLIENT_IP');
        } else {
            $ip = getenv('REMOTE_ADDR');
        }
    }
    return $ip;
}

2010/03/19

smarty dollar to cent

In a project we have a smarty variable presenting a dollar value like ‘1.23’, or ‘.06’. And we wanted to show cent value when the value less than one dollar, for exmaple,

  1. “.78” will be “78”
  2. “.06” will be “6”

Instead of crating a new smarty variable in php, here is a workaround I made in the template.

{*here $dollar_per is in dollar value*}
{if $dollar_per && $dollar_per < 1}
   {if $dollar_per < 0.1} {*ex. 0.06*}
   {assign var="cent_per" value=$dollar_per|substr:2} {*get 6*}
   {assign var="cent_per" value="&nbsp;"|cat:$cent_per} {*add space before 6 in my case*}
   {else} {*ex. 0.78*}
   {assign var="cent_per" value=$dollar_per|substr:1} {*get 78*}
   {/if}
{/if}

Note: substr and cat were used!

2010/03/12

name attribute of <a> tag

There are several attributes of <a> tag, and ‘name’ is one of them. I just found today, that you cannot have character, ‘%’, as a value of this attribute.

I used to use name attribute to hold some data, then pass it as variable in jQuery code as the following,

$(document).ready(function() {
  $(".s-img>a").hover(function(){
    var largePath = $(this).attr("name"); 
    // note: here I used 'name' attr 
    $("#limg").attr({ src: largePath});
    return false;
    },
    function() {
      ;
  });
});

It failed when the name has ‘%’ character in there. Fortunately, the “alt” attribute is happy with ‘%’as its value. So then, changed it ‘alt’, it works.

P.S. Actually, I see most of ‘alt’ other than ‘name’ to pass variable. Maybe, this is a reason.

2010/03/11

ajax setcookie failed with page reload

I have ajax to set cookie and then reload the page, the original code is some thing like the following:

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set"); //set cookie
  location.reload(true); //reload the page
});

But it failed. Looked around and figured out that the .get ajax called might not be successful before the page reload. ajax is asynchronous, so the reload function could be executing during the ajax called.

The solution is using ajax callback to make sure reload function runs after ajax call ended, see the revise code as the following,

$('#btn').click(function(){
  $('#btn').html('Please wait ...');
  $.get("/ajax/update.php?action=set", function(){
    location.reload(true);
  });
});

2010/01/26

informix database using PHP

In a project, I need to write a php code to talk to informix database, which I have not done before.

First, you need PHP Informix Module, here is an guide, http://www.ibm.com/developerworks/data/library/techarticle/dm-0606bombardier/

Fortunately, the server I am working on has already installed this module, so I just wrote a simple test script as the following,

<?php
$conn_id = ifx_connect ("mydb@ol_srv1", "user", "pass");
?>

This code snippet is from PHP Manual, which is actually not a good for test. because if there is some error in ifx_connect, you will not see any error message. I found a better one from PhpDig.net, http://www.phpdig.net/ref/rn30re552.html

if (!$conn = @ifx_connect( "stores7@demo_se", "testuser", "password" )) {
   die( sprintf( "Connection error: %s %s", ifx_error(), ifx_errormsg() ) );
}

I experienced the following error at the beginning,

Connection error: E [SQLSTATE=IX 001  SQLCODE=-1829] Unable to load locale categories.

The guy from IBM had an explanation http://www.iiug.org/forums/ids/index.cgi/noframes/read/3333

OK, some environment parameters missing, $INFORMIXDIR, after fixing this one, I got another one missing:

Connection error: E [SQLSTATE=IX 000  SQLCODE=-25560] Environment variable INFORMIXSERVER must be set.

Finally, fixed!