Acerca de Linux, BSD y notas personales

Archives for Programación category

MySQL search and replace

update tablename set field = replace(field,'search_for_this','replace_with_this');

Getting the recent one month or year records from MySQL table
Some time we have to collect last 7 or 15 days or X days (or month, year or week) data from MySQL table. For example let us find out who are the new members joined in our forum in last week. One shop may be interested in knowing new products added in last one month. What are the books arrived in last one year. Here irrespective of the date values we want the records of last X days from today, or we can say that the records between today and last X days ( month , year or week) are required.

We will use the MySQL function CURDATE() to get the today’s date.

To get the difference in today date and previous day or month we have to use the MySQL function DATE_SUB

DATE_SUB is a MySQL function which takes date expression, the interval and the constant to return the date value for further calculation.

Here are some sample queries on how to get the records as per requirements . 

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 15 DAY)

The above query will return last 15 days records. Note that this query will return all future dates also. To exclude future dates we have to modify the above command a little by using between query to get records. Here is the modified one.

SELECT * FROM dt_tb WHERE `dt` BETWEEN DATE_SUB( CURDATE( ) ,INTERVAL 15 DAY ) AND CURDATE( )

Let us try to get records added in last one month

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 MONTH)

Here also future records will be returned so we can take care of that by using BETWEEN commands if required.

select * from dt_tb where `dt` >= DATE_SUB(CURDATE(), INTERVAL 1 YEAR)

You can easily make out what the above query will return.

Now let us try a different requirement. How to get the records of the working days of the week so far ? If today is Thursday then records from Monday to Thursday should be returned. We will discuss this in our next section >>.

Here is the sql code to create and fill the table with records

CREATE TABLE `dt_tb` ( `id` int(2) NOT NULL auto_increment, `dt` datetime NOT NULL default ‘0000-00-00 00:00:00′, `dt2` date NOT NULL default ‘0000-00-00′, UNIQUE KEY `id` (`id`) ) ENGINE=MyISAM AUTO_INCREMENT=11 DEFAULT CHARSET=latin1 AUTO_INCREMENT=11 ;

INSERT INTO `dt_tb` VALUES (1, ‘2007-02-15 00:00:00′, ‘2005-01-25′);
INSERT INTO `dt_tb` VALUES (2, ‘2007-02-12 23:56:54′, ‘2005-06-12′);
INSERT INTO `dt_tb` VALUES (3, ‘2005-12-08 13:20:10′, ‘2005-06-06′);
INSERT INTO `dt_tb` VALUES (5, ‘2005-02-10 00:00:00′, ‘2006-01-02′);
INSERT INTO `dt_tb` VALUES (6, ‘2006-11-26 00:00:00′, ‘2006-12-25′);
INSERT INTO `dt_tb` VALUES (7, ‘2006-11-26 00:00:00′, ‘2007-02-25′);
INSERT INTO `dt_tb` VALUES (8, ‘2007-10-20 00:00:00′, ‘2007-10-25′);
INSERT INTO `dt_tb` VALUES (9, ‘2007-02-11 00:00:00′, ‘2007-01-25′);
INSERT INTO `dt_tb` VALUES (10, ‘2007-01-22 00:00:00′, ‘2007-01-15′);

http://www-128.ibm.com/developerworks/systems/library/es-nweb.html?ca=drs-

Strings

Concatenar strings

$str1 = “hola “;
$str2 = “que tal “;
$str3= “como estas?”;

$str4 = $str1.$str2.$str3;

Strings Chars
Se pueden reemplazar los caracteres:
$str1 = “0123456789″;
$str1[2]=”x“;

Cambiar a mayusculas y minúsculas:
strtoupper();
strtolower();

Capitaliza la primera letra:
ucfirst();

Capitaliza todas las letras:
ucwords();

ASCII

chr(32) = space
chr(65) = A

ord(“A”) = 65;

 

Tags:

unset()

unset()
Syntax:
    void unset(mixed var [, mixed var, ...])
var
    Variable to unset.
Unsets a variable.
unset() destroys one or more variables and deallocates the memory associated with them.
Code:

<?php

$a = “Some data”;

unset($a);
if (!isset($a)) {
   print “\$a is no longer available”;
}

?>

Output:

$a is no longer available

Introducción a PHP

¿ Que es PHP ?

  • Scripting / Programming Language (“C – Like”)
  • FREE and Open Source
  • Fast, Open, Stable, Cross Platform!
  • No compilation!
  • PHP is all about “Community”

 

 

Navigator Object

  • Determine details of the user browser
  • appName – browser application name
  • appVersion – string containing version details
  • cookieEnabled – boolean value
  • mimeTypes – array of known types of files
  • plugins – array of plug-in application
  • platform – operating system

http://www.devguru.com/technologies/javascript/11226.asp

 

Creating and extending objects

Why develop objects?

  • Data hiding
  • Code elegance
  • Code reuse
  • Script stability

Steps for developing custom objects

  • Design properties and methods
  • Create constructor functions
  • Develop methods
  • Bind methods to objects

 

Anatomy of an object

  • information that describes the object
  • functions that change the state of the object

 

Duality of the object

variable -> function
property (information about the object)-> method (what the object can do)
attribute->behaviour
description->action
adjetive->verb

Encapsulation

  • Binding data and functions
  • Provides robust and secure set of elements
  • simple interface hide details
  • class an object prototype
  • Object instantaion with instructions

Inheritance

  • enables the development of a new object
  • … by extending and existing one (base)
  • Eases development while facilitating flixibility

Polymorphism

  • Associating different but related classes
  • using commond funcionts

 

String Method

 document.write(”<h1>String Methods</h1>”);

   var sample_string = “AaBbCcDdEeFfGg”;

   document.write(”<br>Orignal String: ” + sample_string);

   document.write(”<h2>Formatting</h2>”);

   document.write(”<br>toLowerCase: ” + sample_string.toLowerCase());

document.write(”<br>toUpperCase: ” + sample_string.toUpperCase());

document.write(”<br>bold: ” + sample_string.bold());

document.write(”<br>italics: ” + sample_string.italics());

document.write(”<br>strike: ” + sample_string.strike());

document.write(”<br>big: ” + sample_string.big());

document.write(”<br>small: ” + sample_string.small());

document.write(”<br>sup: ” + sample_string.sup());

document.write(”<br>sub: ” + sample_string.sub());

document.write(”<br>fixed: ” + sample_string.fixed());

document.write(”<br>fontcolor(red): ” + sample_string.fontcolor(”#FF0000″));

document.write(”<br>fontsize(-3):” + sample_string.fontsize(”-3″));

document.write(”<br>fixed: ” + sample_string.fontsize(”-3″));

 

   document.write(”<h2>Processing</h2>”);

 

document.write(”<br>substr(2, 2): ” + sample_string.substr(2, 2));

document.write(”<br>substring(2, 4): ” + sample_string.substring(2, 4));

document.write(”<br>concat(\”XxYxZz\”): ” + sample_string.concat(”XxYxZz”));

document.write(”<br>charAt(0): ” + sample_string.charAt(0));

document.write(”<br>charCodeAt(0): ” + sample_string.charCodeAt(0));

document.write(”<br>indexOf: ” + sample_string.indexOf(”A”));

document.write(”<br>search(\”Cc\”): ” + sample_string.search(”Cc”));

document.write(”<br>replace(\”Cc\”, \”Zz\”): ” + sample_string.replace(”Cc”, “Zz”));