Monday, September 24, 2012

Creating XML file using DOMDocument() object in PHP



<?php
$employees = array();
$employees [] = array(
'name' => 'Albert',
'age' => '34',
'salary' => "$10000"
);
$employees [] = array(
'name' => 'Claud',
'age' => '20',
'salary' => "$2000"
);
$doc = new DOMDocument();
$doc->formatOutput = true;
$r = $doc->createElement( "employees" );
$doc->appendChild( $r );
foreach( $employees as $employee ) {
$b = $doc->createElement( "employee" );
$name = $doc->createElement( "name" );
$name->appendChild(
$doc->createTextNode( $employee['name'] )
);
$b->appendChild( $name );
$age = $doc->createElement( "age" );
$age->appendChild(
$doc->createTextNode( $employee['age'] )
);
$b->appendChild( $age );
$salary = $doc->createElement( "salary" );
$salary->appendChild(
$doc->createTextNode( $employee['salary'] )
);
$b->appendChild( $salary );
$r->appendChild( $b );
}
echo $doc->saveXML();
$doc->save("write.xml")
?>
Printing XML file

<?php
$doc = new DOMDocument();
$doc->load( 'write.xml' );
$employees = $doc->getElementsByTagName( "employee" );
foreach( $employees as $employee ) {
$names = $employee->getElementsByTagName( "name" );
$name = $names->item(0)->nodeValue;
$ages= $employee->getElementsByTagName( "age" );
$age= $ages->item(0)->nodeValue;
$salaries = $employee->getElementsByTagName( "salary" );
$salary = $salaries->item(0)->nodeValue;
echo "<b>$name - $age - $salary\n</b><br>";
}
?>

Thursday, September 20, 2012

Login form in fancy box: Close and Refresh the parent page after submit the login form


*****************************************************
HTML Page
*****************************************************
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Testing Page</title>
    <script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
    <link rel="stylesheet" href="fancybox/jquery.fancybox-1.3.4.css" type="text/css" media="screen" />
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
    <script type="text/javascript">
    $(document).ready(function() {
        $(".login-window").fancybox({
            'width'             : 300,
            'height'             : 180,
            'autoScale'       : false,
            'transitionIn'    : 'none',
            'transitionOut' : 'none',
            'type'               : 'iframe',
            'onClosed'        : function() {  
                            parent.location.reload(true);
                            ;}
        });
    });

</head>
<body>
</body>
<a href="fancy_login.php?" class ="login-window">Login</a>
</html>

*****************************************************
fancy_log.php
*****************************************************
<?php
if($_POST)  {
    $close_window = TRUE;
}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
   
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script type="text/javascript" src="scripts/jquery-1.6.2.min.js"></script>
// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
<script type="text/javascript" src="fancybox/jquery.fancybox-1.3.4.pack.js"></script>// stored in https://docs.google.com/open?id=0B3t3ir6gJardd21lOGFlMmZ3QVE
<?php
if($close_window)  {
echo '<script type="text/javascript">
            $(function () {
                parent.$.fancybox.close();
            });
        </script>';
}
?>
<title> Login</title>
</head>

<body>
<form name="loginform" action="" method="POST">
    <table align="center">
        <tr>
            <th colspan="2">Login</th>
        </tr>
        <tr>
            <td>Username</td>
            <td><input type="text" name="username" />
            </td>
        </tr>
        <tr>
            <td>Password</td>
            <td><input type="password" name="password" /></td>
        </tr>
        <tr>
            <td>&nbsp;</td>
            <td><input type="submit" name="login" value="Login" /></td>
        </tr>
    </table>
</form>
</body>
</html>

Wednesday, September 12, 2012

PHP class for simple pagination


<?php
class Pagination    {
    function __coustruct()  {
        parent::__construct();
    }

    function showPagination($query, $page = '1', $per_page = '10')   {
        $total = 0;
        $rst = $this->selectData($query);
        $total = mysql_num_rows($rst);
        $page = ($page == 0 ? 1 : $page);
        $start = ($page - 1) * $per_page;
        $prev = $page - 1;
        $next = $page + 1;
        $lastpage = ceil($total/$per_page);
        $lpm1 = $lastpage - 1;
        $pagination = '';
        $linktitle = 'Goto ';
        $page_url = $_SERVER['PHP_SELF'];
        if($lastpage > 1)   {
            $first  = '<<';
            $prev   = '<';
            $next   = '>';
            $last   = '>>';
            $pagination .= '<ul class="pagination">';
                $link = ($page != '1')?$page_url.'?p=1': '#';
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'First Page">'.$first.'</a></li>';
                if($link != '#')$link = $page_url.'?p='.((int)$page-1);
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'Previous Page">'.$prev.'</a></li>';
                for ($counter = 1; $counter <= $lastpage; $counter++)   {
                    if ($counter == $page)
                        $pagination.= '<li><a class="current">'.$counter.'</a></li>';
                    else
                        $pagination.= '<li><a href="'.$page_url.'?p='.$counter.'">'.$counter.'</a></li>';
                }
                $linklast = ($page != $lastpage)?$page_url.'?p='.$lastpage: '#';
                $link = ($linklast != '#')? $page_url.'?p='.((int)$page+1):'#';
                $pagination.= '<li><a href="'.$link.'" title="'.$linktitle.'Next Page">'.$next.'</a></li>';
                $pagination.= '<li><a href="'.$linklast.'" title="'.$linktitle.'Last Page">'.$last.'</a></li>';
                $pagination .= '<li class="details">Page <span>'.$page.'</span> of <span>'.$lastpage.'</span></li>';
                $pagination .= '<div class="clear"></div></ul>';
         
        }
        return $pagination;
    }
    function selectData($query)   {
        mysql_connect('localhost', 'root', 'password') or mysql_error();
        mysql_select_db('database') or mysql_error();
        $result = mysql_query($query) ;
        return ($result and mysql_num_rows($result))? $result : false;
    }
}
?>

Monday, September 10, 2012

PHP Class: Mail sending with attachment


<?php
ini_set('error_reporting', E_ALL);
if($_POST['send_mail']) {
if($_POST['email']) {
function sendmsg($email_to, $email_subject, $msgtext, $email_from, $email_replyto, $file, $type){
require_once("htmlMimeMail.php");
$filename = basename($file);
$mail  = new htmlMimeMail();
$mail->setFrom("$email_from");
$mail->setReturnPath("$email_replyto");
$mail->setSubject("$email_subject");
$mail->setHTML("$msgtext");
$mail->addAttachment($mail->getFile("$file"), "$filename", 'application/pdf');
$result = $mail->send(array("$email_to"));
return $result ? 'Mail sent successfully!' :  'Sorry! Cannot send the mail. Try later!';
}
$fileatt = "test.pdf";
$subject = 'Sending Mail using HTMLMimeMail';
$to = $_POST['email'];
$email_from = 'satheesh@iv.bh';
$email_replyto = 'satheesh@iv.bh';
$type = 'application/pdf';
$ok = sendmsg($to, $subject, $msg,$email_from,$email_replyto, $fileatt, $type);
              echo $ok;
}
else echo 'Please enter Email';
}else echo 'Please enter Email';
?>
<form action="" method="POST">
<input type="text" name = "email"><br>
<input type="submit" value="Send Mail" name="send_mail">
</form>

Tuesday, September 4, 2012

Function for shorten long text without partial words


 function shortenText($text, $length = '')    {
// $normal_text = strip_tags(html_entity_decode($text, ENT_QUOTES));
$max = $length;
$normal_text = strip_tags($text, ENT_QUOTES);
$tot_len = strlen($normal_text);
if(!$length)$length = $tot_len;
if($length < $tot_len)   {
while(substr(strip_tags($text, ENT_QUOTES), $max, 1) != ' ' && $max > 0 )  {
$max--;
}
if($max == 0)$max = $length;
}
$output = substr($normal_text, 0, $max);
if($length < $tot_len)$output .= '<span dir="ltr">...<span>';
return $output;
}