Latest Post
Tampilkan postingan dengan label basic php. Tampilkan semua postingan
Tampilkan postingan dengan label basic php. Tampilkan semua postingan

Implode and Explode function of Php

Written By Unknown on Kamis, 19 September 2013 | 23.20

Implode and Explode function of Php


<?php
// function 1:  implode()
echo "<h1>IMPLODE</h1>";
$arr=array("test1","test2","test3","test4","test5","test6","test7","test7");
echo "<pre>";  print_r($arr); echo "</pre>";

//implode(glue, pieces)

echo "Array with glue=none";
echo "</br>";echo "</br>";
$str=implode('', $arr);
echo $str;

echo "</br>";echo "</br>";
echo "Array with  ',' as glue to get comma separarted value from array ";
echo "</br>";echo "</br>";
$str=implode(',', $arr);
echo $str;
echo "</br>";echo "</br>";
// implode() function is used to join array elements and return string

// function 2:  explode(delimiter, string)
echo "<h1>EXPLODE</h1>";
echo $str="This is my string i am going to explode";
echo "</br>";echo "</br>";
echo "First using space as delimiter";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr=explode(' ',$str);

echo "</br>";echo "</br>";
echo "Now using t as delimiter";
$arr=explode('t',$str);
echo "<pre>";  print_r($arr); echo "</pre>";
// explode breaks string into array where delimiter comes

?>

Array() / Array_fill() / Array_Keys() in php

Array() / Array_fill() / Array_Keys() in php

<?php
// function 1:  array()
$arr=array();
echo "Empty array created";
echo "<pre>";  print_r($arr); echo "</pre>";
// array() create an empty array

// function 2:  array_fill(start_index, num, value)
$arr=array_fill(0, 5, "test");
echo "Array filled with value";
echo "<pre>";  print_r($arr); echo "</pre>";
$arr[1]="failed";
$arr[3]="failed";
echo "Array after changes";
echo "<pre>";  print_r($arr); echo "</pre>";
// array_fill() fill the array with value given in parameter

// function 3:  arrays_keys(array,value)
echo "Keys where value is test in array";
$keyz=array_keys($arr,"test");
echo "<pre>";  print_r($keyz); echo "</pre>";
// arrays_keys() return the array of keys where value is same as parameter

?>

Append some (text/img) after some html element using jquery

Written By Unknown on Jumat, 13 September 2013 | 04.56

How to append some (text/img) after some html element using jquery 

<html>
    <head>
        <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
        <script type="text/javascript" >
        $(document).ready(function(){
var i=0;
            $("#sub").on("click",function(){
$( "#1" ).append("Test "+i+" ... ");
                 i++;
                 });      
            });
 
        </script>
        </head>
    <body>
<div id="1">This is my test...</div>
<button type="button"  id="sub">Append</button>
</form>
</body>

</html>

Get session information in cakephp

Written By Unknown on Kamis, 12 September 2013 | 23.30

How to get session information in cakephp 

In the controller you can use

$this->Session->read();

to get all details of session variable . And you can use

$this->set('session',$this->Session->read());


to use these values in corresponding view page.

-----------------------------------------------------------------------------------------------------------------------

If you wish to use session variable in each page then use set method in appcontroller to set its value

public function beforefilter(){
$this->set('session',$this->Session->read());
}

Now this session variable is accessible in every page of view.

Note: this should be placed in Appcontroller...

Find position of string in a given string

Written By Unknown on Kamis, 05 September 2013 | 04.11

Strpos Function


<html>
<body>
    <?php
        $str1="test";
        $str2="This is a testing string ";
        $pos=strpos($str2,$str1);
        echo "Given String : ".$str2."</br>";
        echo "String to be matched : ".$str1."</br>";
        echo "Match found at position : ".$pos;
        ?>
    </body>
</html>

Paging in php

Written By Unknown on Kamis, 29 Agustus 2013 | 21.49

To implement paging we need 3 things :
1. php code
2. css file
3. database

So first create a database with name paging (you can take any name but this is the name i am taking ).
Then create a table with name tb_page with 3 fields (id , title , description).


Then go to link below and download 3 files and save them with same name in your root directory ..
Download link :  https://gist.github.com/codesplanet/6386348

Note : Dont forget to change your host name , user name and password of database in pagination.php file ..


How to design a basic joomla template ?

Written By Unknown on Senin, 26 Agustus 2013 | 23.46

File Structure : (to be saved in template directory of joomla)

-------------------------------------------------------------------------------------------------
mytemplate(Folder)
                  -->index.php
                  -->template.xml
                  -->css(Folder)
                                ---> style.css
-------------------------------------------------------------------------------------------------

index.php

<html>
    <head>
        <jdoc:include type="head" />
        <link rel="stylesheet" href="<?php echo $this->baseurl ?>/templates/<?php echo $this->template; ?>/css/style.css" type="text/css" />
    </head>
    <body>
        Hello !!.. This is a testing template
        <jdoc:include type="modules" name="top" />
        <jdoc:include type="component" />
        <jdoc:include type="modules" name="bottom" />
    </body>
</html> 

template.xml 

<?xml version="1.0" encoding="utf-8"?>
<extension version="2.5" type="template">
        <name>mynewtemplate</name>
        <creationDate>2013-08-27</creationDate>
        <author>Amit kumar</author>
        <authorEmail>ak221189@gmail.com</authorEmail>
        <authorUrl>http://www.id-script.com</authorUrl>
        <description>My New Template</description>
        <files>
                <filename>index.php</filename>
                <filename>template.xml</filename>
                <folder>images</folder>
                <folder>css</folder>
        </files>
        <positions>
                <position>breadcrumb</position>
                <position>left</position>
                <position>right</position>
                <position>footer</position>
        </positions>
</extension>

------------------------------------------------------------------------------------


Now for joomla version 2.5 ,
1. go to extension(in admin panel)
2. click on extension manager
3. click on discover tab
4. click on discover button
5. now select your template and click install.

:) Happy coding

 







 

How to disable checkbox set using jquery ?

If you got two set of checkboxes and you have to use one at a time ... then using this code when you click on either of checkbox set .. the other will get disabled..



<html>
<head>
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>
<script type="text/javascript" >
$(document).ready(function(){
    $( document ).on("click",function(){
        if($("input[name='test[]']:checked").val()){
        $('input:checkbox[name="test_2\[\]"]').attr('disabled', true);
        }
        if($("input[name='test_2[]']:checked").val()){
        $('input:checkbox[name="test\[\]"]').attr('disabled', true);
        }
        });
});
</script>
</head>
<body>


<form id="test_form">
    <b> Checkboxes1</b><br>
<input type="checkbox" name="test[]"   value="Test1">Test1<br>
<input type="checkbox" name="test[]"  value="Test2">Test2 <br>
<input type="checkbox" name="test[]"   value="Test3">Test3<br>
<input type="checkbox" name="test[]"  value="Test4">Test4 <br>
<b> Checkboxes2</b><br>
<input type="checkbox" name="test_2[]"   value="Test1">Test1<br>
<input type="checkbox" name="test_2[]"  value="Test2">Test2 <br>
<input type="checkbox" name="test_2[]"   value="Test3">Test3<br>
<input type="checkbox" name="test_2[]"  value="Test4">Test4 <br>
</form>

</body>
</html>

How to sort an array in php ?

Written By Unknown on Jumat, 23 Agustus 2013 | 21.57


<html>
<head>
<title>
Sorting an array in php
</title>
</head>
<body>
<?php
$arr=array("Rahul","George","tina","Angel");
echo "Array before sorting"."</br>";
echo "<pre>";
print_r($arr);
echo "</pre>";
sort($arr);
echo "Array After sorting"."</br>";
echo "<pre>";
print_r($arr);
echo "</pre>";
?>
</body>
</html>

How to send an array in url in php ?

Written By Unknown on Rabu, 21 Agustus 2013 | 23.04

File.php  (This file contain array )

<?php

$data = array('foo'=>'bar',
              'baz'=>'boom',
              'cow'=>'milk',
              'php'=>'script');

$arr=http_build_query($data);

?>
<a href="nextpage.php?<?php echo $arr; ?>">Sendarray</a>


--------------------------------------------------------------------------------------------------------------------------------

nextpage.php (This file will receive array and print it)


<?php
echo "<pre>";
print_r($_GET);
echo "</pre>";
?>

How to read a file character by character in php ?

<?php

$file=fopen("filename.txt","r") or exit("Unable to open file!");
while (!feof($file))
  {
  echo fgetc($file);
  }
fclose($file);

?>

Use linkedin api to fetch contact information in php ..

First Register your domain for linkedin api .

Then get your API_KEY, API_SECRET REDIRECT_URI , SCOPE ...

Run the script .. Njoy :)
------------------------------------------------------------------------------------------------------------------------
<?php

// Change these
define('API_KEY',      ' '          );  /*put API_KEY between '  ' */
define('API_SECRET',   ' '      ); /*put between API_SECRET '  '*/
define('REDIRECT_URI', ' '); /*put REDIRECT_URI between '  '*/
define('SCOPE',        ' '     ); /*put Scope between '  '*/


session_name('linkedin');
session_start();
$_SESSION['state']="India" // Name the country you live in
;
// OAuth 2 Control Flow
if (isset($_GET['error'])) {
    // LinkedIn returned an error
    print $_GET['error'] . ': ' . $_GET['error_description'];
    exit;
} elseif (isset($_GET['code'])) {
    // User authorized your application
    if ($_SESSION['state'] == "India") {
        // Get token so you can make API calls
        getAccessToken();
    } else {
        // CSRF attack? Or did you mix up your states?
        exit;
    }
} else {
    if ((empty($_SESSION['expires_at'])) || (time() > $_SESSION['expires_at'])) {
        // Token has expired, clear the state
        $_SESSION = array();
    }
    if (empty($_SESSION['access_token'])) {
        // Start authorization process
        getAuthorizationCode();
    }
}

// Congratulations! You have a valid token. Now fetch your profile by defining field you wanted

$user = fetch('GET', '/v1/people/~:(id,firstName,lastName,skills,location,industry,network)');
echo "<pre>";
print_r($user);
echo "<pre>";
exit;

function getAuthorizationCode() {
    $params = array('response_type' => 'code',
                    'client_id' => API_KEY,
                    'scope' => SCOPE,
                    'state' => uniqid('', true), // unique long string
                    'redirect_uri' => REDIRECT_URI,
              );

    // Authentication request
    $url = 'https://www.linkedin.com/uas/oauth2/authorization?' . http_build_query($params);
   
    // Needed to identify request when it returns to us
    $_SESSION['state'] = $params['state'];

    // Redirect user to authenticate
    header("Location: $url");
    exit;
}
   
function getAccessToken() {
    $params = array('grant_type' => 'authorization_code',
                    'client_id' => API_KEY,
                    'client_secret' => API_SECRET,
                    'code' => $_GET['code'],
                    'redirect_uri' => REDIRECT_URI,
              );
   
    // Access Token request
    $url = 'https://www.linkedin.com/uas/oauth2/accessToken?' . http_build_query($params);
   
    // Tell streams to make a POST request
    $context = stream_context_create(
                    array('http' =>
                        array('method' => 'POST',
                        )
                    )
                );

    // Retrieve access token information
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    $token = json_decode($response);

    // Store access token and expiration time
    $_SESSION['access_token'] = $token->access_token; // guard this!
    $_SESSION['expires_in']   = $token->expires_in; // relative time (in seconds)
    $_SESSION['expires_at']   = time() + $_SESSION['expires_in']; // absolute time
   
    return true;
}

function fetch($method, $resource, $body = '') {
    $params = array('oauth2_access_token' => $_SESSION['access_token'],
                    'format' => 'json',
              );
   
    // Need to use HTTPS
    $url = 'https://api.linkedin.com' . $resource . '?' . http_build_query($params);
    // Tell streams to make a (GET, POST, PUT, or DELETE) request
    $context = stream_context_create(
                    array('http' =>
                        array('method' => $method,
                        )
                    )
                );


    // Hocus Pocus
    $response = file_get_contents($url, false, $context);

    // Native PHP object, please
    return json_decode($response);
}

Recursion in php

Written By Unknown on Selasa, 20 Agustus 2013 | 23.28

<?php

function recursion($int){
    if($int>0){
        echo $int."</br>";
        recursion($int-1);
        echo $int."</br>"; /*values from stack*/
        }
    }
   
recursion(5);

?>

How to preview array in Php ?

Array

<?php

$arr=array(1,2,3,4,5);
echo "<pre>";
print_r($arr);
echo "</pre>";

?>

How to write your first php script ?

What you need ?

1. A server
2. A php file to be put in your www. or htdocs directory..

Phpfile

<?php

echo "My first PHP script!";
 

?>
 
Support : Creating Website | Johny Template | Mas Template
Copyright © 2011. Kumpulan Kata Broadcast Blackberry - All Rights Reserved
Template Created by Creating Website Published by Mas Template
Proudly powered by Blogger