Ehsanul Haque

Welcome to my personal site & blog

05 October
3Comments

PHP Function I frquently use

While developing the web applications with PHP I have made my own small function library. In this library I have few functions that I use very frequently. One of the functions that is used more often is echo_d(). The purpose of this function is to check a variable to see if it is empty or not set; then based on the check result it either prints a default value or the value of the variable.

This is a simple function that saves me a lot of time and keeps my main codes cleaner. So here it is.

function echo_d($var, $def) {
  $var = trim($var);
  echo ((isset($var)) && (!empty($var))) ? $var : $def;
}

When to use this function? One of the most common places I use this function is in the reports. When I am generating reports for anything I always have to check whether certain value is in the database; it prints a default value if it cannot find one. The usage of this function in the scenario is something like:

$lastLoginDate = $rowsFromDB['last_login_date'];
echo_d($lastLoginDate, "---");

This function can be expanded to make it more versatile but I have found this tiny little function very useful.


Thank you.

20 August
14Comments

Patch for Grab Yahoo class released

Last week suddenly my Grab Yahoo class stopped working for Address Book section. After Mr Mandy Singh reported the bug on phpclasses.org discussion board I started digging to resolve the issue.

When requesting the address book CSV from Yahoo a value for the key “crumb” is sent in the query string. Earlier Yahoo never checked for the validity of the variable but they have changed the way it is used. Now they check if the value for “crumb” is valid or not. As my script was using a predefined value and it has expired, Yahoo server could not validate. Yahoo was showing an error stating “Invalid or missing crumb”.

I modified my class so that it first loads the page from where request for the CSV is made. From the source of the page, it locates the latest value for crumb assigned by Yahoo and uses that value to send request for the address book CSV file. It is now working as expected. If you find any bug please report it on phpclasses.org discussion board.

Please download the latest version of Grab Yahoo class (v. 1.2.1) here.


Thank you.

20 December
14Comments

Javascript: Select Multiple Dropdown Items

If you would like to have a multiple select dropdown and want to use the selection in a specified format, then here is the solution. Sometimes you may need to create a query string value or fill in a textbox or textarea with the selected values. Say for example you have a dropdown list where you can select multiple items. Upon selecting each option you want to fill in a textarea with the selected value.

Example:


HTML Part:

<form method=POST name='testing'>
  <select name='testsel' multiple onchange='showselection()'>
    <option value="one">one</option>
    <option value="two">two</option>
    <option value="three">three</option>
  </select>
  <textarea id="txtEditions"></textarea>
</form>

Javascript Part:

<script language='javascript'>
  function showselection()
  {
    var frm = document.testing
    var opt = frm.testsel

    var numofoptions = opt.length
    var selValue = new Array

    var j = 0
    for (i=0; i<numofoptions; i++)
    {
      if (opt[i].selected === true)
      {
        selValue[j] = opt[i].value
        j++
      }
    }

    selValue = selValue.join("+")

    document.getElementById("txtEditions").innerHTML = selValue
  }
</script>