Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

How to create array in PHP

agyanadda

Array is used to stores multiple values in one single variable

We can create array by using "array" Keyword

Type of Array

  • Indexed arrays - Numeric index
  • Associative arrays -Named keys
  • Multidimensional arrays - Contain one or more arrays

Indexed Array Examples

Example 1 $color=array("red","Green","Blue"); //echo $color[1]; foreach($color as $val){ echo $val."<br>"; } Example 2 $color=array("red","Green","Blue"); //echo $color[1]; foreach($color as $val){ echo $val."<br>"; } Example 3 $array[0]="Shubham"; $array[1]="Rafiya"; foreach($array as $val){ echo $val."<br>"; } echo count($val); Example 4 $col=array("abc","ccv","dsf"); echo count($col); Example 5 $col=array("abc","ccv","dsf"); $arrlen=count($col); for($i=0;$i<$arrlen;$i++){ echo $col[$i]."<br>"; } Example 6 $col=array("abc","ccv","dsf"); $arrlen=count($col); for($i=0;$i<$arrlen;$i++){ echo $col[$i]."<br>"; }

Associative Array Example

Example 1 $col['Green']="#123"; $col['Red']="#123"; $col['White']="#fff"; echo $col['Red']; Example 2 $col=array("Green"=>"#123","White"=>"#231","Red"=>"#44"); echo $col['Green']; Example 3 $col=array("Green"=>"#123","Green"=>"#231","Red"=>"#44"); echo $col['Green']; Example 4 $col['Green']="#123"; $col['Red']="#123"; $col['White']="#fff"; foreach($col as $val=>$n){ echo $val."<br>"; }

Multi Multidimensional Array Example

Example 1 $arr=array( array(1,2,3), array(4,5,6), array(7,8,9) ); echo $arr[1][2]; } Example 2 $arr=array( array(1,2,3), array(4,5,6), array(7,8,9) ); for($r=0;$r<3;$r++){ for($c=0;$c<3;$c++){ echo $arr[$r][$c]; } echo "<br>"; } }

HOW TO CREATE LINK WHEN OPTION SELECT IN HTML

agyanadda

How to create link in dropdown in HTML.It is so simple to create you can see the given example

<select onchange="javascript:Select(this)"> <option value="home">Home</option> <option value="text">Contact</option> </select> <script type="text/javascript"> function Select(elm) { window.location = elm.value+".php"; } </script>

Language Constructors In PHP

agyanadda


Language Constructors in PHP



There is huge collection of in build Language constructor in PHP. That helps us to call 
directly by PHP files.

 Like echo() is not a function. It is Language constructor.

We can use it by using 

echo “PHP”; or echo (“PHP);

Example:  
  • include
  • require  
  • case 
  • if, else if 
  • die()
  • new
  • return etc



Escaping From HTML IN PHP

agyanadda

Escaping From HTML:

Everything outside of a pair of opening and closing tags  ore Elements is ignored by the PHP parser. It allows PHP files to have mixed content. 
It allows PHP to be embedded in HTML documents.
Following are the some example of PHP

Example 1:
  <p>Ignored by PHP and displayed on browser.</p>
  <?php echo 'Hello display PHP '?>
  <p>Ignored by PHP and displayed on browser.</p>


Example 2:

<?php if ($expression == true): ?>
  This will show if the expression is true.
<?php else: ?>
  Otherwise this will show Hello PHP.
<?php endif; ?>

Example 3:

<?php for ($i 0$i 10; ++$i): ?>
Hello, PHP!
<?php endfor; ?>


PHP Ajax Display Dynamic MySQL Data in Bootstrap Modal

agyanadda
===============[getcode.php]====================== <!--php $id=$_GET['allrec']; include 'useroperation.php'; $dcp = new Dbconnection(); $conn = $dcp--->connect(); $sql="select *from gradienttb where id='$id'"; $result=$conn->query($sql); if($result->num_rows>=0){ $row=$result->fetch_assoc(); echo json_encode($row); // echo $row['id']."".$row['cidd']; } ?> ================[ json part ]=================== <script> function showdetails(a){ var allrec=a.id; console.log(allrec); $.ajax({ url:"getcode.php", method:"GET", data:{"allrec":allrec}, success:function(response){ var dataa=JSON.parse(response); $("#sid").html(dataa['cidd']); }, error:function(err){ alert("err.error"); }, }); } </script> =================[ Get ID from Database ]================== echo "<a href='#' id='$row[id]' onclick='showdetails(this);' data-toggle='modal' data-target='#myModal'><div>".$row[cname]."</div></a>"; ===========================================================

How to insert and get images from database (blob column)

agyanadda

How to insert and fetch image through database in PHP

$img= addslashes(file_get_contents($_FILES["img"]["tmp_name"])); echo '<img src="data:image/png;base64,&#39; . base64_encode($row[&#39;image&#39;]).&#39;" />';

Session and Cookies in PHP

agyanadda

   What is the difference between Session and  Cookies in PHP ?                                                              
     There are given following differences between Session and Cookies in PHP.

      PHP COOKIES 
  1.  It store data at client side browser as text file format.           
  2. It store limit amount of Data.                                                             
  3. It store only 4kb (4096 bytes) file.                                    
  4. It doesn’t hold multiple variables.                               
  5. It is less secure.                                                                       
  6. We can set the expiry time of cookie                                   
  7. Cookies function must be appear before the HTML.         

     PHP SESSION
  1. It stores large amount of   data.                                    
  2. It holds multiple variables in sessions.
  3.  It holds multiple variables in sessions.
  4.  It is secure in compression to cookies.
  5.  session_destroy() function we can destroy .
  6. session function must be appear before the 

     

Types of Error in PHP

agyanadda

What is an Error in PHP?
An error is a type of mistake, in another words we can say that error is a condition of having incorrect false knowledge or it is defined as an unexpected.
and Mistake is an error caused by a fault.

Types of error
  1. Parse Error or Syntax Error
  2. Fatal Error
  3. Warning Error
  4. Notice Error
  1. Parse Errors (syntax errors) :                                                              It will occur when there is a syntax mistake in the script.
     Following are the reason of syntax errors:
  • Unclosed quotes
  • Missing or Extra parentheses
  • Unclosed braces 
  • Missing semicolon
Example
<?php
echo "Correct syntax";
echo "synatx Error"
echo "Synatx Error":
?>

 2  Fatal Errors
It will occur when PHP couldn’t understand what you've written. It stop the execution of the script.
       Example
<?php
function fun1(){
echo "Hello PHP";
}
fun2();
echo "Fatal Error !!";
?>

     3. Warning Errors
 It will occur when the file is missing or incorrect number of parameter . It   doesn’t stop the execution of script.
<?php echo "Warning Error!!";
include ("index.php");
?>

 4. Notice Errors
  It will occur when you have not declare variable. It is same as warning error i.e.
          <?php
           $a="Hello PHP";
           echo "Notice Error !!";
           echo $b;
           ?>


How to send multiple values in a href Link in PHP?

agyanadda
You can use same Way this code that is working properly ========================================================================= echo "<option value='result.php?topic=$row[subject]&sid=$row[sid]' selected>".$row['subject']."</option>";

PHP SIMPLE CRUD APPLICATION

agyanadda
========================================================================= |====================== index.php ======================================| ========================================================================= <p> <!--DOCTYPE html--> <html> <head> <title>CRUD</title> </head> <body> <h1 align="center">CRUD APPLICATION</h1><hr> <form action="dbcreate.php" method="POST" align="center"> <input type="text" name="name" placeholder="Name"><br> <input type="text" name="class" placeholder="class"><br> <input type="number" name="roll" placeholder="roll"><br> <input type="submit" value="SUBMIT"> <a href="dbshow.php">Show</a> </form> </body> </html> ========================================================================= |====================== connection.php =================================| ========================================================================= <!--php $conn=new mysqli('localhost','root',"",'php'); if($conn--->connect_error){ die("Eoor to connect".$conn->connect_error); } ?> ========================================================================= |======================== dbcreate.php =================================| ========================================================================= <!--php include ('connection.php'); $sql="insert into reg(name,class,roll) values('$_POST[name]','$_POST[class]','$_POST[roll]')"; if($conn--->query($sql)===False){ echo "Error to insert"; }echo "<script>alert ('Record saved, Do You want to insert ?')</script>"; echo "<script>window.location.assign('index.php')</script>"; ?> ========================================================================= |======================== dbshow.php ===================================| ========================================================================= <!--php include ('connection.php'); $sql="select *from reg"; $result=$conn--->query($sql); if($result->num_rows>0){ echo "<table border='1' cellpadding='10' align='center'>"; echo "<tr> <th>SR</th> <th>NAME</th> <th>CLASS</th> <th>ROLL</th> <th>UPLOAD</th> <th>DELETE</th> </tr>"; while($row=$result->fetch_assoc()){ echo "<tr><td>".$row['sr']."</td>"; echo "<td>".$row['name']."</td>"; echo "<td>".$row['class']."</td>"; echo "<td>".$row['roll']."</td>"; echo "<td>"."<a href='update.php?r=$row[sr]'>Edit</a>"."</td>"; echo "<td>"."<a href='dbdelete.php?r=$row[sr]'>Delete</a>"."</td></tr>"; } } echo "</table>"; ?> ========================================================================= |======================== dbdelete.php =================================| ========================================================================= <!--php include('connection.php'); $sql="delete from reg where sr='$_GET[r]'"; if($conn--->query($sql)===True){ echo "<script>alert('delete Successfull');</script>"; echo "<script>window.location.assign('dbshow.php');</script>"; } ?> ========================================================================= |======================== Update.php =================================| ========================================================================= <!--php include('connection.php'); $sql="select *from reg where sr='$_GET[r]'"; $result=$conn--->query($sql); $row=$result->fetch_assoc(); ?> <!--DOCTYPE html--> <html> <head> <title></title> </head> <body> <h3 align="center">UPDATE PAGE</h3><hr> <form align="center" action="dbupdate.php" method="POST"> <input type="text" name="sr" readonly="true"s value="<?php echo $row['sr']?>"><br> <input type="text" name="name" value="<?php echo $row['name']?>"><br> <input type="text" name="class" value="<?php echo $row['class']?>"><br> <input type="number" name="roll" value="<?php echo $row['roll']?>"><br> <input type="submit" value="UPDATE"> </form> </body> </html> ========================================================================= |========================== dbupdate.php ===============================| ========================================================================= <!--php include('connection.php'); $sql="update reg set name='$_POST[name]',class='$_POST[class]',roll='$_POST[roll]' where sr='$_POST[sr]'"; if($conn--->query($sql)===True){ echo "<script>alert('Update succesfully');</script>"; echo "<script>window.location.assign('dbshow.php');</script>"; } ?> ========================================================================= |=======================================================================| ============================[DataBasee Query]============================ -- phpMyAdmin SQL Dump -- version 4.7.0 -- https://www.phpmyadmin.net/ -- -- Host: 127.0.0.1 -- Generation Time: Jul 17, 2017 at 03:41 AM -- Server version: 10.1.22-MariaDB -- PHP Version: 7.1.4 SET SQL_MODE = "NO_AUTO_VALUE_ON_ZERO"; SET AUTOCOMMIT = 0; START TRANSACTION; SET time_zone = "+00:00"; /*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */; /*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */; /*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */; /*!40101 SET NAMES utf8mb4 */; -- -- Database: `php` -- -- -------------------------------------------------------- -- -- Table structure for table `reg` -- CREATE TABLE `reg` ( `sr` int(5) NOT NULL, `name` varchar(50) NOT NULL, `class` varchar(50) NOT NULL, `roll` int(11) NOT NULL ) ENGINE=InnoDB DEFAULT CHARSET=latin1; -- -- Dumping data for table `reg` -- INSERT INTO `reg` (`sr`, `name`, `class`, `roll`) VALUES (1, 'ALOK Nandan', 'MBA', 152161), (2, 'Rahul', 'BCA', 1216), (3, 'Mohan', 'MCAA', 2345), (4, 'Ranjan singh', 'BBM', 2224); -- -- Indexes for dumped tables -- -- -- Indexes for table `reg` -- ALTER TABLE `reg` ADD PRIMARY KEY (`sr`); -- -- AUTO_INCREMENT for dumped tables -- -- -- AUTO_INCREMENT for table `reg` -- ALTER TABLE `reg` MODIFY `sr` int(5) NOT NULL AUTO_INCREMENT, AUTO_INCREMENT=5;COMMIT; /*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */; /*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */; /*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;