Archive for the ‘php’ Category

Logical algorithm for previous and next items

Wednesday, July 9th, 2008

This can be used on pictures, news, shop items, etc. with JavaScript events for “previous” and “next”

    if (prev === null) {
      prev = current && next = current + 1
    } elseif (next === null) {
      next = current && prev = current -1
    } else {
      if (action == prev) {
        next = current
        prev = current - 1
      } elseif (action == next) {
        prev = current
        next = current + 1
      } else {
        prev = current - 1
        next = current + 1
      }
    }

Generate past years (with range) using PHP

Monday, June 23rd, 2008

Code:

    $cy = date('Y'); // current year
    $ld = substr($cy, 3); // last digit
    $ftd = substr($cy, 0, 3); // first three digits

    for ($i = $ld; $i > $ld - 3; $i--) {
      $j = $i;
      if ($i < 0) {
        if ($j == -1) {
          $ftd--;
        }
        $j = (9 + $i) + 1;
      }
      $years[] = $ftd . $j;
      $newld = $j;
    }

    $ld = --$newld;

    if ($ld > 0) {
      for ($i = $ld; $i > $ld - 5 || $i == 0; $i–) {
        if ($i > 0) {
          if ($i == $ld) {
            $ys = $ftd . $i;
          }

          if ($i-1 == $ld - 5) {
            if ($i == 9) {
              $ys2 = $ftd . ++$i;
            } else {
              $ys2 = $ftd . $i;
            }
            $newld = $i;
            break;
          }
        } else {
          $ys2 = $ftd . $i;
          $newld = 0;
          $ftd–;
          break;
        }
      }
      if ($ys2[3] == 1) {
        $ys2[3] = 0;
        $ftd–;
      }
      $years[] = $ys .’-’. $ys2;
    } else {
      if (!in_array($ftd . 0, $years)) {
        $years[] = $ftd . 0;
      }
      $newld = 0;
      $ftd–;
    }

    $ld = –$newld;

    if ($ld > 0) {
      for ($i = $ld; $i > 0; $i–) {
        if ($i == $ld) {
          $ys = $ftd . $ld .’-';
        }

        if ($i-1 == 0) {
          $ys .= $ftd . 0;
          $ftd–;
        }
      }
      $years[] = $ys;
    }

    $ftwod = substr($ftd, 0, 2); // first two digits
    $td = substr($ftd, 2); // third digit
    $j = 9;

    for ($i = $td; $i >= 0; $i–) {
      $fy = $ftd . $i . $j;
      $years[] = $ftwod . $i . ‘9-’. $ftwod . $i . ‘0′;
    }

Output:

  'years' =>
    array
      0 => string '2008' (length=4)
      1 => string '2007' (length=4)
      2 => string '2006' (length=4)
      3 => string '2005-2000' (length=9)
      4 => string '1999-1990' (length=9)
      5 => string '1989-1980' (length=9)
      6 => string '1979-1970' (length=9)
      7 => string '1969-1960' (length=9)
      8 => string '1959-1950' (length=9)
      9 => string '1949-1940' (length=9)
      10 => string '1939-1930' (length=9)
      11 => string '1929-1920' (length=9)
      12 => string '1919-1910' (length=9)
      13 => string '1909-1900' (length=9)

PHP function for reading DOM XML into an array

Wednesday, June 11th, 2008


$xml = read($dom);

  function read($node) {
    $array = array();
    if ($node->firstChild !== null) {
      $current = $node->firstChild;
    } else {
      return;
    }

    while (true) {
      switch ($current->nodeType) {
        case 1: // XML_ELEMENT_NODE
          $array[$current->nodeName][] = $this->read($current);
          break;
        case 3: // XML_TEXT_NODE
        case 4: // XML_CDATA_SECTION_NODE
          if (strlen(trim($current->nodeValue)) > 0) {
            return $current->nodeValue;
          }
          break;
        default:
          echo $current->nodeType;
          break;
      }

      if ($current->nextSibling !== null) {
        $current = $current->nextSibling;
      } else {
        return $array;
      }
    }

    return $array;
  }