28 February, 2013

OBJECT OVERLOADING

< ? php
class Data
{
private $data = array();
public function __set($name, $value)
{
$this->data[$name] = $value;
}
public function __get($name)
{
if (isset($this->data[$name])) { return $this->data[$name]; }
}
public function __isset($name)
{
return isset($this->data[$name]);
}
public function __unset($name)
{
unset($this->data[$name]);
}
}
$data = new Data();
$data->name = ‘F’;
echo “The data value of ‘name’ is {$data->name}”;
unset($data->name);
echo ‘The value is ‘, isset($data->name) ? ” : ‘not ‘, ‘set.’;
? >

SHELL SORT

< ? php
function shell_sort(&$a)
{
$count = count($a);
$columns = 1;
while ($columns < $count)
{
$columns = $columns * 2 + 1;
}
$columns = ($columns – 1) / 2;
while ($columns > 0)
{
for ($c = 0; $c < $columns; $c++)
{
for ($i = $columns; $i < $count; $i += $columns)
{
$value = $a[$i];
for ($x = $i – $columns;( ($x >= 0) && ($a[$x] > $value) );$x -= $columns)
{
$a[$x + $columns] = $a[$x];
}
$a[$x + $columns] = $value;
}
}
$columns = ($columns – 1) / 2;
}
}
$values = array(7, 3, 4, 6, 1);
shell_sort($values);
foreach ($values as $v) { echo “{$v} “; }
?>

27 February, 2013

A CLASS TO PUT GET AND POST VARIABLES IN HIDDEN FORM

< ? php
class c_HiddenVars
{
function display($a)
{
$c = Count($a);
for ($i = 0, Reset($a); $i < $c; $i++, Next($a))
{
$k = Key($a); $v = $a[$k];
if (is_array($v))
{
$vc = Count($v);
for (Reset($v), $vi = 0; $vi < $vc; $vi++, Next($v))
{
$vk = Key($v);
echo “< input type=hidden name=”$k[$vk]” value=”".htmlspecialchars($v [$vk]). “”>n”;
}
}
else
{
echo “< input type=hidden name=”$k” value=”".htmlspecialchars($v). “”>n”;
}
}
}
function get()
{
global $HTTP_GET_VARS;
if (is_array($HTTP_GET_VARS))
{
$this->display
($HTTP_GET_VARS);
}
}
function post()
{
global $HTTP_POST_VARS;
if (is_array($HTTP_POST_VARS))
{
$this->display
($HTTP_POST_VARS);
}
}
function all()
{
$this->get();
$this->post();
}
};
?>

SUBCLASS AND PARENT CLASS

< ? php
class Employee
{
private $name;
function setName($name)
{
if ($name == “”)
echo “Name cannot be blank!”;
else
$this->name = $name;
}
function getName()
{
return “My name is “.$this->name.;
}
}
class Executive extends Employee
{
function pillageCompany()
{
echo “hi!”;
}
}
$exec = new Executive();
$exec->setName(“Joe”);
echo $exec->getName();
$exec->pillageCompany();
? >

26 February, 2013

RETURN AN ARRAY OF FILES WITHIN A DIRECTORY

< ?
function listFilesInDir($start_dir)
{
/*
returns an array of files in $start_dir (not recursive)
*/
$files = array();
$dir = opendir($start_dir);
while(($myfile = readdir($dir)) !== false)
{
if($myfile != ‘.’ && $myfile != ‘..’ && !is_file($myfile) && $myfile ! = ‘resource.frk’ && !eregi(‘^Icon’,$myfile) )
{
$files[] = $myfile;
}
}
closedir($dir);
return $files;
}
?>
Example:
< ?
$ dir = ‘downloads/wordDocs/public/topic_1′;
$wordDocs = listFilesInDir($dir);
foreach($wordDocs as $key => $fileName)
{
echo “{$fileName}”;
}
?>

WRITE TO FILE

< ? php
$filename = ‘test.txt’;
$somecontent = “Add this to the filen”;
// Let’s make sure the file exists and is writable first.
if (is_writable($filename))
{
// In our example we’re opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that’s where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, ‘a’))
{
print “Cannot open file ($filename)”;
exit;
}
// Write $somecontent to our opened file.
if (!fwrite($handle, $somecontent))
{
print “Cannot write to file ($filename)”;
exit;
}
print “Success, wrote ($somecontent) to file ($filename)”;
fclose($handle);
}
else
{
print “The file $filename is not writable”;
}
? >