Tuesday, April 28, 2009

how to convert month from string to integer or opposite


smoetimes i need to print out the month in integer value like (1,2,3,4,5,6,7,8,9,10,11,12) or print out it in string value like April , March ,.... etc . i made class convert_month to convert any month in integer to string value or any month string value to integer see my example below :
make month_convert.php page and copy the code below inside it then save the page


class convert_month {

/*author engineer sherif sakr ,
//email :xxsherif82@yahoo.com


*/

//data members
private $msg ;
private $month;
private $type ;
private $montharray=array(1=>'January',2=>'February',3=>'March',
4=>'April',5=>'May',6=>'June',
7=>'July',8=>'August',9=>'September',
10=>'October',11=>'November',12=>'December');

//constructor function
public function __construct($month_from_user) {
//intiate month value
$this->month=$month_from_user;

// check month value type if integer or sting
$type= $this->get_variable_type() ;

// *******convert the value type to its oppsite ************//

if($type=="integer") $this->month=$this->convert_to_string() ;
else if($type=="string") $this->month=$this->convert_to_integer() ;

//if value type not supported get the error message
else return $this->msg ;
}

//********************* get method *************//
public function get_month() {
if($this->month==''){
return $this->error_msg();
}
else return $this->month ;
}

//************get variable type method ***********//
private function get_variable_type() {
$this->type=gettype($this->month) ;
return $this->type ;
}
//********convert variable type to integer method *********//

private function convert_to_integer() {
foreach ($this->montharray as $key=> $value ) {
if( $this->month==$value )
{
return $this->month=$key ;
break ;
}
}
}

//******convert variable type to string method **************//

private function convert_to_string() {
foreach ($this->montharray as $key=> $value ) {
if( $this->month==$key )
{
return $this->month=$value ;
break ;
}
}
}
//**************error method *****************//

public function error_msg() {

return $this->msg="sorry data type not supported !!! ";

}


}
?>

make test.php page and put the code below and save the page then run test.php

require "month_convert.php" ;

$month =4 ; // or put $month = 'april ' as string
$month= new convert_month($month);

$monthx=$month->get_month() ;

echo $monthx ; // will print April

?>

that was just example of using OOP but we can do the same using small function
put the code below in the page test2.php and run it

//get_month function
function get_month($month) {

//check case sensitive letter if type is string
if(gettype($month=='string')){
//convert to lower string
$month=strtolower($month);
//UPERCASE FIRST LETER ONLY
$month=ucfirst($month);
}


$montharray=array(1=>'January',2=>'February',3=>'March',
4=>'April',5=>'May',6=>'June',
7=>'July',8=>'August',9=>'September',
10=>'October',11=>'November',12=>'December');

/*check month
//in_arry($month,array) function searches an array for a specific value,
// This function returns TRUE if the value is found in the array, or FALSE otherwise.
//The array_key_exists($month,array) function checks an array for a specified key,
// and returns true if the key exists and false is the key does not exist. */

if(in_array($month,$montharray)==true || array_key_exists($month,$montharray)){
foreach ($montharray as $key=> $value ) {
if( $month==$value )
{
// return month back in integer value
return $month=$key ;
break ;

}

else if ( $month==$key )

{
// return month back in string value
return $month=$value ;
break ;

}
} // end forech

}
// return month back as messaage error with out
else return $month="not supported data" ;


} // end function

//test month
$month=3 ;

$month=get_month($month);

echo $month ; // print out March

?>

Saturday, April 25, 2009

Make a Dierctory Items with PHP


learn how to make directory items using object oriented programming (OOP)

first make directory" images" than make page "DirectoryItems.php" and copy the class below then save it but all in the same folder
class DirectoryItems {

/*author engineer sherif sakr ,
//email :xxsherif82@yahoo.com
//http://sherif-sakr.blogspot.com/
*/

//data members
private $dicrectory ;
private $replacechar ;
private $filearray=array();

//////////////////////////////////////////////////////////////////
//constructor
/////////////////////////////////////////////////////////////////
public function __construct($directory, $replacechar = "_"){
$this->directory = $directory;
$this->replacechar = $replacechar;
$d = "";

if(is_dir($directory)){
$d = opendir($directory) or die("Failed to open directory.");
while(false !== ($f = readdir($d))){
if(is_file("$directory/$f")){
$title = $this->createTitle($f);
$this->filearray[$f] = $title;
}
}
closedir($d);
}else{ //error die("Must pass in a directory.");
}
}


//////////////// public function

getCount(){
return count($this->filearray);
}
/////// public function

getFileArray(){
return $this->filearray;
}

///////////////////////////////

//private functions///////////////////////////////////////////////////////////////// private function createTitle($title){ //strip extension $title = substr($title, 0, strrpos($title, ".")); //replace word separator $title = str_replace($this->replacechar, " ", $title); return $title; }



/////////////////eliminate all elements from array except images ///////////////////

public function imagesOnly(){
$extension = "";
$types = array("jpg", "jpeg", "gif", "png");
foreach ($this->filearray as $key => $value){
$extension = substr($key,(strpos($key, ".")+1));
$extension = strtolower($extension);
if(!in_array($extension, $types)){
unset($this->filearray[$key]);
}
}
}

}//end class



make page test.php and put the code below then save and run it
imagesOnly();//get portion of array
$filearray = $di->getFileArray();
$count=$di->getCount();
echo 'images count ' .$count ;
$imag_counter=0 ; //instant count per line
$imag_perline=5; // max image number per line

foreach ($filearray as $key => $value){
$imag_counter=$imag_counter+1;
$imag_path='images/'.$key;
echo 'ima tag with path';
if ($imag_counter==5) {$imag_counter=0 ; echo ' break tage here';}

}

MS in Computer Science with paid training in USA company