article

Saturday, November 14, 2015

Custom 404 Error messages with codeigniter

Custom 404 Error messages with codeigniter

Create controller application\controllers\My404.php


<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class My404 extends CI_Controller
{
   public function __construct()
   {
       parent::__construct();
   }
   public function index()
   {
       $this-<output-<set_status_header('404');
       $this-<load-<view('err404');    
  }
}

Create View application\views\err404.php


<!DOCTYPE html>
<html lang="en">
<head>
   <meta charset="utf-8">
   <title>404 Page</title>
</head>
<body>
<div>
       <p>How to Create Custom Codeigniter 404 Error Pages </p>
       <p align="center" style="font-size:55px;">Sorry Page Not Found</p>
       <p align="center" style="font-size:55px;">Error 404</p>
       <div>
           <p align="center" ><a href="<?php echo base_url(); ?>">Go Back to Home</a></p>
       </div>
</div>
</body>
</html>

add 404 to application\config\route.php $route['404_override'] = 'my404';

Monday, November 9, 2015

Jquery ajax Selectbox


Jquery ajax Selectbox  


index.php

 
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3.0/jquery.min.js"></script>
<script type="text/javascript">
 jQuery(document).ready(function(){    
  jQuery("select[name='country']").change(function(){   
   var optionValue = jQuery("select[name='country']").val();  
   jQuery.ajax({
    type: "GET",
    url: "city.php",
    data: "country="+optionValue+"&status=1",
    beforeSend: function(){ jQuery("#ajaxLoader").show(); },
    complete: function(){ jQuery("#ajaxLoader").hide(); },
    success: function(response){
     jQuery("#cityAjax").html(response);
     jQuery("#cityAjax").show();
    }
   });    
  });
 });
</script>
Countries: 
<select name="country">
 <option value="">Please Select</option>
 <option value="1">Nepal</option>
 <option value="2">India</option>
 <option value="3">China</option>
 <option value="4">USA</option>
 <option value="5">UK</option>
</select>
<div id="ajaxLoader" style="display:none"><img src="../ajax-loader.gif" alt="loading..."></div>
<div id="cityAjax" style="display:none">
 <select name="city">
  <option value="">Please Select</option>
 </select>
</div>
city.php
<?php
$country = $_GET['country'];
if(!$country) {
 return false;
}
$cities = array(
   1 => array('Kathmandu','Bhaktapur','Patan','Pokhara','Lumbini'),
   2 => array('Delhi','Mumbai','Kolkata','Bangalore','Hyderabad','Pune','Chennai','Jaipur','Goa'),
   3 => array('Beijing','Chengdu','Lhasa','Macau','Shanghai'),
   4 => array('Los Angeles','New York','Dallas','Boston','Seattle','Washington','Las Vegas'),
   5 => array('Birmingham','Bradford','Cambridge','Derby','Lincoln','Liverpool','Manchester')
  );
$currentCities = $cities[$country];
?>
City: 
<select name="city">
 <option value="">Please Select</option>
 <?php
 foreach($currentCities as $city) {
  ?>
  <option value="<?php echo $city; ?>"><?php echo $city; ?></option>
  <?php 
 }
 ?>
</select>

Sunday, November 8, 2015

Jquery Multipele Delete


Jquery Multipele Delete





 
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Jquery Multipele Delete</title>
<script type="text/javascript" src="scripts/jquery.js"></script>
<script type="text/javascript">
$(function(){
 $("a.delete").click(function(){
  page=$(this).attr("href");
  ids=new Array()
  a=0;
  $("input.chk:checked").each(function(){
   ids[a]=$(this).val();
   a++;
  })
  if(confirm("Are you sure want to delete?")){
    el=$(this)
    $.ajax({
     url:page,
     data:"id="+ids,
     type:"GET",
     success:function(res)
     {
      if(res==1)
      {
       $("input.chk:checked").each(function(){
        $(this).parent().parent().remove();
       })
      }
     }
    })
  }
  return false;
 })
})
</script>
</head>
<body>
<table>
<caption><a href="delete.php" class="delete">delete</a></caption>
<tr>
<td><input type="checkbox" value="1" name="chk[]" class="chk" /></td><td>category a</td>
</tr>
<tr>
<td><input type="checkbox" value="2" name="chk[]" class="chk" /></td><td>category b</td>
</tr>
<tr>
<td><input type="checkbox" value="3" name="chk[]" class="chk" /></td><td>category c</td>
</tr>
<tr>
<td><input type="checkbox" value="4" name="chk[]" class="chk" /></td><td>category d</td>
</tr>
<tr>
<td><input type="checkbox" value="5" name="chk[]" class="chk" /></td><td>category e</td>
</tr>
</table>
</body>
</html>

Add Edit Delete Using PHP Class

Add Edit Delete Using PHP Class






 
<?php
$db['host']     = 'localhost';   //Your database host, I.E. localhost
$db['username'] = 'root';        //Your database username
$db['password'] = '';            //Your database password
$db['db']       = 'mycmsdb';   //Your database name
$db['prefix']   = '';            //Your table prefix, can be left blank

class MySQLDB
{
        var $dbhost;
        var $dbuser;
        var $dbpass;
        var $dbname;
        var $dblink;
        var $qrystr;
        var $result;
        var $dbprefix;
        
        function MySQLDB($dbhost, $dbuser, $dbpass, $dbname, $dbprefix)
        {
                $this->dbhost=$dbhost;
                $this->dbuser=$dbuser;
                $this->dbpass=$dbpass;
                $this->dbname=$dbname;
                $this->dbprefix=$dbprefix;
        }
        
        function connectdb()
        {
                $this->dblink=mysql_connect($this->dbhost,$this->dbuser,$this->dbpass) or die($this->show_error());
        }
        
        function selectdb()
        {
                mysql_select_db($this->dbname) or die($this->show_error());
        }
        
        function show_error()
        {
                print mysql_error($this->dblink);
        }
        
        function query($qry="")
        {
                if(!empty($qry)) $this->qrystr=$qry;
                if(empty($this->qrystr)) die("Error: Query string is empty.");
                else $this->result=mysql_query($this->qrystr,$this->dblink) or die($this->show_error());                
        }
        
        function setqrystr($qry)
        {
                $this->qrystr=$qry;
        }
        
        function get_insert_id()
        {
                return mysql_insert_id($this->dblink);
        }
        
        function getrow()
        {
                return mysql_fetch_row($this->result);
        }
        
        function getarr()
        {
                return mysql_fetch_array($this->result,MYSQL_ASSOC);
        }
        
        function getobj()
        {
                return mysql_fetch_object($this->result);
        }
        
        function getaffectedrows()
        {
                return mysql_affected_rows($this->dblink);
        }
        
        function getrownum()
        {
                return mysql_num_rows($this->result);
        }
        
        function freeresult()
        {
                mysql_free_result($this->result);
        }
        
        function closedb()
        {
                mysql_close($this->dblink);
        }
        
        function __destruct()
        {
                mysql_close($this->dblink);
        }
        
        function tb($tablename)
        {
                if(empty($this->dbprefix))        return $tablename;
                else return $this->dbprefix."_".$tablename;
        }
}
//Hostname,Username,Password,Database,table prefix
$db=new MySQLDB($db['host'], $db['username'], $db['password'], $db['db'], $db['prefix']);
$db->connectdb();
$db->selectdb();
?>

<?
//Examples
$qry="SELECT * FROM rme_bookings WHERE city='angeles'";
$db->query($qry);
$row=$db->getrow();
$maxtime=$row[0]; 
echo $maxtime;
//Udate
$qry="UPDATE ".$db->tb("admin")." SET uname='$uname', pwd='$pwd', email='$email' WHERE uid=$auid";
$db->query($qry);
 if($db->getaffectedrows()==0) $err[0]="Nothing altered! Try again.";
 else  $err[0]="Profile updated successfully."; 

$qry="SELECT conf_value FROM ".$db->tb("configuration")." WHERE conf_name='AUTO_FILE_DELETE'";
$db->query($qry);
$row=$db->getrow();
if($row[0]=="Yes")
{
 $now=time();
 $qry="SELECT dir, file_name FROM ".$db->tb("fileinfo")." WHERE expire_time<$now";
 $db->query($qry);
 while($row=$db->getrow())
 {
  @unlink("uploads/".$row[1]."/".$row[2]);
  @rmdir("uploads/".$row[1]);
 }
 $qry="DELETE FROM ".$db->tb("fileinfo")." WHERE expire_time<$now";
 $db->query($qry);
}

 $qry="SELECT uname, pwd, email FROM ".$db->tb("admin")." WHERE uid=$auid";
$db->query($qry);
 $row=$db->getrow();
 $uname=$row[0];
$pwd=$row[1];
 $email=$row[2];
  
$qry="SELECT * FROM ".$db->tb("fileinfo")." ORDER BY upload_time DESC";
$db->query($qry);
while($row=$db->getarr())
{
 $status="Ok";
 $idkey = $row["idkey"];
 if($row["no_of_dwnld"]>=$row["max_dwnld"]) $status="Count Exceeded";
 if($row["expire_time"]<time()) $status="Expired";
 if($row["link_status"]==0) $status="Suspended";
}   

$qry="DELETE FROM ".$db->tb("adminlog")." WHERE uid=".$row[0]." and timein=".$row1[0];
$db->query($qry);  

$qry="INSERT INTO ".$db->tb("adminlog")."(uid,timein,ip) VALUES(".$row[0].",".time().",'".$_SERVER['REMOTE_ADDR']."')";
$db->query($qry);

$db->query("UPDATE ".$db->tb("admin")." SET `pwd`='".$md5."'");
$row=$db->getrow();
?>

like Rating with Jquery and Ajax

like Rating with Jquery and Ajax
<!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=iso-8859-1" />
<title>like Rating with Jquery and Ajax</title>
<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script type="text/javascript">
$(document).ready(function()
{
$(".like").click(function()
{
var id=$(this).attr("id");
var name=$(this).attr("name");
var dataString = 'id='+ id + '&name='+ name;
$("#votebox").slideDown("slow");
$("#flash").fadeIn("slow");
$.ajax
({
type: "POST",
url: "rating.php",
data: dataString,
cache: false,
success: function(html)
{
$("#flash").fadeOut("slow");
$("#content").html(html);
} 
});
});
$(".close").click(function()
{
$("#votebox").slideUp("slow");
});

});
</script>
<style>

body
{
font-family:Arial, Helvetica, sans-serif;
font-size:13px;

}
a
{
text-decoration:none
}
a:hover
{
text-decoration:none

}
#votebox
{
border:solid 1px #dedede; padding:3px;
display:none;
padding:15px;
width:700px;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
}
.close
{
color:#333
}
#greebar
{
float:left;
background-color:#aada37;
border:solid 1px #698a14;
width:0px;
height:12px;
}
#redbar
{
float:left;
background-color:#cf362f;
border:solid 1px #881811;
width:0px;
height:12px;
}

#flash
{
display:none;
font-size:10px;
color:#666666;
}
#close
{
float:right; font-weight:bold; padding:3px 5px 3px 5px; border:solid 1px #333;
-moz-border-radius: 6px;
-webkit-border-radius: 6px;
}
h1
{
font-family:'Georgia', Times New Roman, Times, serif;
font-size:36px;
color:#333333;
}
</style>
</head>
<body>
<div style="margin:50px">
<a href="#" class="like" id="1" name="up">Like</a> -- <a href="#" class="like" id="1" name="down">Dislike</a>
<div id="votebox">
<span id='close'><a href="#" class="close" title="Close This">X</a></span>
<div style="height:13px">
<div id="flash">Loading........</div>
</div>
<div id="content"></div>
</div>
</div>
</body>
</html>
rating.php
<?php
$mysql_hostname = "localhost";
$mysql_user = "username";
$mysql_password = "password";
$mysql_database = "database";
$bd = mysql_connect($mysql_hostname, $mysql_user, $mysql_password) 
or die("Opps some thing went wrong");
mysql_select_db($mysql_database, $bd) or die("Opps some thing went wrong");
if($_POST['id'])
{
$id=mysql_escape_String($_POST['id']);
$name=mysql_escape_String($_POST['name']);
mysql_query("update messages set $name=$name+1 where id='$id'");
$result=mysql_query("select up,down from messages where id='$id'");
$row=mysql_fetch_array($result);
$up_value=$row['up'];
$down_value=$row['down'];
$total=$up_value+$down_value;
$up_per=($up_value*100)/$total;
$down_per=($down_value*100)/$total;
?>
<div style="margin-bottom:10px">
<b>Ratings for this blog</b> ( <?php echo $total; ?> total)
</div>
<table width="700px">
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $up_value; ?></td>
<td width="600px"><div id="greebar" style="width:<?php echo $up_per; ?>%"></div></td>
</tr>
<tr>
<td width="30px"></td>
<td width="60px"><?php echo $down_value; ?></td>
<td width="600px"><div id="redbar" style="width:<?php echo $down_per; ?>%"></div></td>
</tr>
</table>
<?php
}
?>

Friday, November 6, 2015

Using phpMailer to Send Mail through PHP

Using phpMailer to Send Mail through PHP

Download the PHPMailer script  

index.html
<form method="post" action="email.php">
  Email: <input name="email" id="email" type="text" /><br />
  Message:<br />
  <textarea name="message" id="message" rows="15" cols="40"></textarea><br />
  <input type="submit" value="Submit" />
</form>
mail.php
<?php
$email = $_REQUEST['email'] ;
$message = $_REQUEST['message'] ;
require("lib/PHPMailer/PHPMailerAutoload.php");
$mail = new PHPMailer();
// set mailer to use SMTP
$mail->IsSMTP();
// As this email.php script lives on the same server as our email server
// we are setting the HOST to localhost
$mail->Host = "localhost";  // specify main and backup server
$mail->SMTPAuth = true;     // turn on SMTP authentication
// When sending email using PHPMailer, you need to send from a valid email address
// In this case, we setup a test email account with the following credentials:
// email: send_from_PHPMailer@tutorial101.blogspot.com
// pass: password
$mail->Username = "send_from_PHPMailer@tutorial101.blogspot.com";  // SMTP username
$mail->Password = "123456789"; // SMTP password
// $email is the user's email address the specified
// on our contact us page. We set this variable at
// the top of this page with:
// $email = $_REQUEST['email'] ;
$mail->From = $email;
// below we want to set the email address we will be sending our email to.
$mail->AddAddress("ednalan@tutorial101.blogspot.com", "Ednalan");
// set word wrap to 50 characters
$mail->WordWrap = 50;
// set email format to HTML
$mail->IsHTML(true);
$mail->Subject = "You have received feedback from your website!";
// $message is the user's message they typed in
// on our contact us page. We set this variable at
// the top of this page with:
// $message = $_REQUEST['message'] ;
$mail->Body    = $message;
$mail->AltBody = $message;
if(!$mail->Send())
{
   echo "Message could not be sent. <p>";
   echo "Mailer Error: " . $mail->ErrorInfo;
   exit;
}
echo "Message has been sent";
?>

Thursday, November 5, 2015

jQuery Get Selected Radio Button Value

jQuery Get Selected Radio Button Value
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>jQuery Get Selected Radio Button Value</title>
<script type="text/javascript" src="http://code.jquery.com/jquery.min.js"></script>
<script type="text/javascript">
    $(document).ready(function(){
        $("input[type='button']").click(function(){
         var radioValue = $("input[name='gender']:checked").val();
            if(radioValue){
                alert("Your are a - " + radioValue);
            }
        });
        
    });
</script>
</head> 
<body>
    <h4>Please select your gender.</h4>
    <p> 
        <label><input type="radio" name="gender" value="male">Male</label> 
        <label><input type="radio" name="gender" value="female">Female</label>
    </p>
    <p><input type="button" value="Get Value"></p>
</body>
</html>      

Related Post