Showing posts with label CodeIgniter. Show all posts
Showing posts with label CodeIgniter. Show all posts

Tuesday, November 13, 2018

PHP programs to CRUD in php

<?php
include("./connect.php");
if(isset($_POST['btnReg'])){
$name=$_POST['txtnm'];
$gen=$_POST['rdbgen'];
$cno=$_POST['txtcno'];
$addr=$_POST['txtaddr'];
$h=$_POST['chk'];
$desg=$_POST['selDesg'];
$hobby=implode(",",$h);
$sql="insert into tblPerson(pname,gender,contactNo,hobbies,address,designation) values('".$name."','".$gen."','".$cno."','".$hobby."','".$addr."','".$desg."')";
$res=mysqli_query($con,$sql);
}
if(isset($_POST['btnDisp'])){
header("Location:./disp.php");
}
if(isset($_GET['act'])){

$sql="select * from tblPerson where pid =".$_GET['id'];
$res=mysqli_query($con,$sql);
$row=mysqli_fetch_array($res);
$hobby=explode(",",$row['hobbies']);
$desg=$row['designation'];
}
if(isset($_POST['btnUpd'])){
echo 'upd';
$name=$_POST['txtnm'];
$gen=$_POST['rdbgen'];
$cno=$_POST['txtcno'];
$addr=$_POST['txtaddr'];
$h=$_POST['chk'];
$desg=$_POST['selDesg'];
$hobby=implode(",",$h);
$sql="update tblPerson set pname='".$name."',gender='".$gen."',contactNo='".$cno."',hobbies='".$hobby."',address='".$addr."',designation='".$desg."' where pid=".$_GET['id'];
$res=mysqli_query($con,$sql);
}
?>
<!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>Untitled Document</title>
</head>

<body>
<form method="post">
<table width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Name :</th>
    <td><input type="text" name="txtnm" id="txtnm" value="<?php (isset($row))? print($row[1]):print("") ?>" /></td>
  </tr>
  <tr>
    <th scope="row">Gender :</th>
    <td>
    <?php
if(isset($row)){
if(strcmp($row['gender'],"Male")==0){
    ?>
            <input type="radio" name="rdbgen"  value="Male" checked="checked" />
    Male
           <input type="radio" name="rdbgen"  value="Female" />
                Female
    <?php
}
else{
    ?>
<input type="radio" name="rdbgen"  value="Male" />
      Male
    <input type="radio" name="rdbgen"  value="Female" checked="checked"/>
    Female
<?php
}
}
else{
?>
    <input type="radio" name="rdbgen"  value="Male" />
      Male
    <input type="radio" name="rdbgen"  value="Female"/>
    Female
    <?php
}
    ?>
 
    </td>
  </tr>
  <tr>
    <th scope="row">Contact No. :</th>
    <td><input type="text" name="txtcno" id="txtcno" value="<?php (isset($row))? print($row['contactNo']):print("") ?>" /></td>
  </tr>
  <tr>
    <th scope="row">Address :</th>
    <td><textarea name="txtaddr" id="txtaddr" cols="45" rows="5"><?php (isset($row))? print($row['address']):print("") ?></textarea></td>
  </tr>
  <tr>
    <th scope="row">Hobbies :</th>
    <td>
    <?php
$hb=array("Dancing","Sports","Singing");
if(isset($row)){
$j=0;
foreach($hb as $val){
if(strcmp($hobby[$j],$val)==0){
echo "<input type='checkbox' name='chk[]' value='".$val."' checked='checked' />".$val."<br/>";
$j++;
}
else{
echo "<input type='checkbox' name='chk[]' value='".$val."' />".$val."<br/>";
}
}
}
else{
foreach($hb as $val){
echo "<input type='checkbox' name='chk[]' value='".$val."' />".$val."<br/>";
}
}
?>
      </td>
  </tr>
  <tr>
    <th scope="row">Designation :</th>
    <td><select name="selDesg">
    <?php
$ch=array('Programmer','Project Manager','Analyst','Clerk');
foreach($ch as $c){
if(isset($row) && strcmp($c,$desg)==0){
?>
    <option value="<?php echo $c; ?>" selected="selected"><?php echo $c; ?></option>
   <?php
}
else{
?>
<option value="<?php echo $c; ?>"><?php echo $c; ?></option>
<?php
}
}
?>
    </select></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="<?php (isset($row))? print("btnUpd"):print("btnReg")?>" value="<?php (isset($row))? print("Update"):print("Register Me") ?>" />
    <input type="submit" name="btnDisp" value="Display" />
    </th>
  </tr>
</table>
</form>
</body>
</html> 

Monday, November 12, 2018

Function to validate email in PHP

<?php
function validate($mail){
if(strpos($mail,"@")>0 && strpos($mail,".")>0)
{
$atPos= strrpos($mail,"@");
$dotPos=strrpos($mail,".");
$len=strlen($mail);
if($dotPos>$atPos && strrpos($mail,".")<= $len-2){
return true;
}
else
return false;
}
else
return false;
}
?>

Sunday, November 11, 2018

Multiple delete using checkbox PHP MySql

<?php
include("./connect.php");
$sql="select * from tblPerson where 1";
$res=mysqli_query($con,$sql);
if(isset($_POST['btnDel'])){
echo "delete";
$id=$_POST['chk'];
$idList=implode(",",$id);
echo $idList;
$sql="delete from tblPerson where pid in(".$idList.")";
$res=mysqli_query($con,$sql);
header("Location:./disp.php");
}
?>
<!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>Untitled Document</title>
</head>

<body>
<form method="post">
<table border="1" width="779" cellpadding="10" cellspacing="0">
<tr>
<th colspan="9" align="right"><a href="ins_reg.php">Add New</a></th>
</tr>
<tr>
<th>Name</th>
    <th>Gender</th>
    <th>Contact No.</th>
    <th>Hobbies</th>
    <th>Address</th>
    <th>Designation</th>
    <th>Edit</th>
    <th>Delete</th>
    <th>Multiple Delete</th>
</tr>
<?php
while($row=mysqli_fetch_array($res)){
?>
<tr align="center">
    <td><?php echo $row['pname']; ?></td>
        <td><?php echo $row['gender']; ?></td>
        <td><?php echo $row['contactNo']; ?></td>
        <td><?php echo $row['hobbies']; ?></td>
        <td><?php echo $row['address']; ?></td>
        <td><?php echo $row['designation']; ?></td>
        <td><a href="ins_reg.php?id=<?php echo $row[0]; ?>&act=edit">Edit</a></td>
        <td><a href="disp.php?id=<?php echo $row[0]; ?>&act=del">Delete</a></td>
        <td><input type="checkbox" name="chk[]" value="<?php echo $row[0]; ?>"/></td>
    </tr>
<?php
}
?>
<tr>
<td colspan="9" align="right"><input type="submit" name="btnDel" value="Delete Marked"/></td>
</tr>
</table>
</form>
<?php
if(isset($_GET['act'])){

if(strcmp($_GET['act'],"del")==0){

$sql="delete from tblPerson where pid=".$_GET['id'];
$res=mysqli_query($con,$sql);
header("Location:./disp.php");
}
}
?>
</body>
</html>

Saturday, November 10, 2018

Program to count number of emails in a string in php

<!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>Untitled Document</title>
</head>

<body>
<form method="post">
<table width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Enter String :</th>
    <td><textarea name="txtstr" id="txtstr"></textarea></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit" /></th>
  </tr>
</table>
</form>
</body>
</html>
<?php
include("email.php");
$email=array();
if(isset($_POST['btnsubmit'])){
$mail=$_POST['txtstr'];
$a=explode("\n",$mail);
foreach($a as $val){
$arr=explode(" ",$val);
foreach($arr as $m){
$f=validate($m);
if($f)
$email[]=$m;
}
}
echo '<br/>There are '.count($email).' emails in this string.';
}
?>

Friday, November 9, 2018

Bad words Detection in Input PHP

<!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>Untitled Document</title>
</head>

<body>
<form method="post">
<table width="779" border="1" cellspacing="0" cellpadding="10">
  <tr>
    <th scope="row">Enter String :</th>
    <td><textarea name="txtstr" id="txtstr"></textarea></td>
  </tr>
  <tr>
    <th colspan="2" scope="row"><input type="submit" name="btnsubmit" id="btnsubmit" value="Submit" /></th>
  </tr>
</table>
</form>
</body>
</html>
<?php
$wrds=array('oh','allas','wah','omg');
if(isset($_POST['btnsubmit'])){
$str=$_POST['txtstr'];
$a=explode("\n",$str);
foreach($a as $val){
$arr=explode(" ",$val);
foreach($arr as $m){
foreach($wrds as $w){
if(strcmp($w,$m)==0){ echo 'contains bad words.'; exit;
}
}
}
}
}
?>

Saturday, June 27, 2015

Why Codeigniter Framework is Better than Custom or Core PHP Development?

CodeIgniter is one of the most popular PHP frameworks around. It uses the Model-View-Controller Architectural design pattern and it’s considered by lots of PHP developers as one of the best framework solution for small to medium projects. It has some great features that we are going to talk about them in this article.

Why use a framework ?

“Frameworks make my life easier.” said most of the programmers asked this question.

In order to make a good, cleanly coded, reliable, high performance, fast and maintainable application you need to do a lot of analysis and intensive software engineering and architectural design tasks first. Luckily, lots of those tasks will be previously done by a framework so you can focus on the development of your application.

Most of PHP frameworks available support the MVC pattern, good file organization, have loads of utilities and libraries, are well secured, have performance abilities e.g. caching and benchmarking and much more awesome capabilities. CodeIgniter has the mentioned features and more that we will talk about now.

MVC pattern

CodeIgniter uses Model-View-Controller pattern which is the most used architectural design pattern in web application. It can also be turned into supporting the more advanced Hierarchical Model View Controller(HMVC) pattern by installing the popular CodeIgniter HMVC Extension.

Awesome Documentation

It’s true that bad documentation can ruin a good product. But CodeIgniter has a great a well detailed documentation files that takes you from the first steps of knowing the basics about the framework to a full reference for each class, helper & driver with simple examples provided.

Community

While being stuck at something with CodeIgniter and didn’t find the answer in the documentation, just don’t panic. There is a developers’ community full of CI gurus who are always willing to help. You may even find that your question has been answered before.

Performance

Since performance has been a major concern for each web developer, CodeIgniter is optimized to boost you code performance. In addition, it contains a beautiful caching class that you should check and use.

Helpers

CodeIgniter contains wide variety of helper functions that were created to help you write cleaner code and save time and effort doing repetitive tasks. For example, there is form, form validation, array, path, security and more that you can check at the documentation part of CodeIgniter’s documentation section of the official website.