Wednesday, 27 November 2013

SugarCRM:: Retrieving Record of Multiple ID's from SugarCRM.

This post give demonstration of Retrieving record of Multiple ID's From SugarCRM.

<?php

    $url = "< YOUR SUGARCRM INSTANCE >/service/v4_1/rest.php";
    $username = "username";
    $password = "password";

    //function to make cURL request
    function call($method, $parameters, $url)
    {
        ob_start();
        $curl_request = curl_init();

        curl_setopt($curl_request, CURLOPT_URL, $url);
        curl_setopt($curl_request, CURLOPT_POST, 1);
        curl_setopt($curl_request, CURLOPT_HTTP_VERSION, CURL_HTTP_VERSION_1_0);
        curl_setopt($curl_request, CURLOPT_HEADER, 1);
        curl_setopt($curl_request, CURLOPT_SSL_VERIFYPEER, 0);
        curl_setopt($curl_request, CURLOPT_RETURNTRANSFER, 1);
        curl_setopt($curl_request, CURLOPT_FOLLOWLOCATION, 0);

        $jsonEncodedData = json_encode($parameters);

        $post = array(
             "method" => $method,
             "input_type" => "JSON",
             "response_type" => "JSON",
             "rest_data" => $jsonEncodedData
        );

        curl_setopt($curl_request, CURLOPT_POSTFIELDS, $post);
        $result = curl_exec($curl_request);
        curl_close($curl_request);

        $result = explode("\r\n\r\n", $result, 2);
        $response = json_decode($result[1]);
        ob_end_flush();

        return $response;
    }

    //login ------------------------------------------------

    $login_parameters = array(
         "user_auth"=>array(
              "user_name"=>$username,
              "password"=>md5($password),
              "version"=>"1"
         ),
         "application_name"=>"RestTest",
         "name_value_list"=>array(),
    );

    $login_result = call("login", $login_parameters, $url);

    /*
    echo "<pre>";
    print_r($login_result);
    echo "</pre>";
    */

    //get session id
    $session_id = $login_result->id;

    //retrieve records --------------------------------------

    $get_entries_parameters = array(

         //session id
         'session' => $session_id,

         //The name of the module from which to retrieve records
         'module_name' => 'Accounts',

         //An array of SugarBean IDs
         'ids' => array(
             'd8ab358c-30c6-9744-130b-5292cbb3d686',
             '44357b2e-b0a6-219d-e1ce-5292cdba1965'
         ),

         //Optional. The list of fields to be returned in the results
         'select_fields' => array('id','name','billing_address_city','billing_address_country'),

         //A list of link names and the fields to be returned for each link name
         'link_name_to_fields_array' => array(
         ),
    );

    $get_entries_result = call("get_entries", $get_entries_parameters, $url);

    $filteredDetail =array();
    $j=0;
    foreach ($get_entries_result as $stdClass) {
        foreach($stdClass as $stdClassObject) {
            foreach($stdClassObject as $acc_details_package) {
                foreach($acc_details_package as $acc_details1) {
                   foreach($acc_details1 as $acc_details2) {
                     echo "<pre>";                  
                     $filteredDetail[$j] = $acc_details2;
                     echo "Data[$j] = ".$acc_details2."\n";
                     $j++;                  
                    }
                    echo "\n\n\n";
                }
            }
        }
    }

   $filtered_array = array();  $a = 0;
 
    for($index=1;$index<=sizeof($filteredDetail);$i++){
if((($index % 2)!= 0)) { $filtered_array[$a] = $filteredDetail[$index]; $index++; $a++;} else { $index++; }
    }

print_r($filtered_array);

?>

Tuesday, 19 November 2013

PHP:: Search Pattern in Uploaded File using php



<pre>
<?php

if(isset($_FILES['myfile']) && isset($_POST['pattern'])) {


$uploaddir = '/var/www/PhpTraining/uploads';
        $fileName = $_FILES['myfile']['name'];

$test = move_uploaded_file($_FILES['myfile']['tmp_name'], "$uploaddir/$fileName");
if($test) { echo "Success Uploads"; } else { echo "Not Uploaded"; }

$content = '  ';
$pattern = '  ';
$pattern = $_POST['pattern'];


        foreach(file("$uploaddir/$fileName") as $line) {
if(preg_match("/$pattern/i",$line)) {
   $content.=$line;
}
}
}


?>
</pre>
<html>
<head><center><h2>Assignment 1: Search Pattern in Uploaded File</h2></center></head>
<body bgcolor='lightgreen'>
<form enctype="multipart/form-data" method='post'>
 <label>Enter pattern to Search: </label>
 <input type='text' name='pattern' value='<?php echo $pattern;?>'/><br /><br>

 <label>Choose File:</label>  <input type='file' name='myfile' /> <br><br>

 <input type='submit' value='upload and Search' />
</form>
<textarea rows='10' cols='60' readonly="readonly"><?php echo $content; ?></textarea>
</body>
</html>





Monday, 18 November 2013

ANDROID-PHP:: How to insert Data using PHP in Android?

Lets make an application in Android for storing Employee Name, Employee Phone Number, Employee Country


Database Name :  db_employee

CREATE TABLE IF NOT EXISTS `tbl_employee` (
  `id` int(2) NOT NULL AUTO_INCREMENT,
  `name` varchar(50) NOT NULL,
  `phone` varchar(15) NOT NULL,
  `country` varchar(50) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=1 ;

Android

  • activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context=".MainActivity" >

<TextView
        android:id="@+id/lblName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="15dp"
        android:text="Employee Name:"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
        android:id="@+id/txtEmployeeName"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblName"
        android:layout_below="@+id/lblName"
        android:layout_marginTop="18dp"
        android:ems="10" >
        <requestFocus />
    </EditText>
<TextView
        android:id="@+id/lblPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtEmployeeName"
        android:layout_below="@+id/txtEmployeeName"
        android:layout_marginTop="32dp"
        android:text="Phone:"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
        android:id="@+id/txtPhone"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblPhone"
        android:layout_below="@+id/lblPhone"
        android:layout_marginTop="16dp"
        android:ems="10"
        android:inputType="phone" />
<TextView
        android:id="@+id/lblCountry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtPhone"
        android:layout_below="@+id/txtPhone"
        android:layout_marginTop="34dp"
        android:text="Country"
        android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
        android:id="@+id/txtCountry"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/lblCountry"
        android:layout_below="@+id/lblCountry"
        android:layout_marginTop="17dp"
        android:ems="10" />
<Button
        android:id="@+id/btnSave"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/txtCountry"
        android:layout_below="@+id/txtCountry"
        android:layout_marginTop="17dp"
        android:text="Save Data" />
<Button
        android:id="@+id/btnReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/btnSave"
        android:layout_alignBottom="@+id/btnSave"
        android:layout_alignRight="@+id/txtCountry"
        android:text="Reset" />


</RelativeLayout>

  • MainActivity.java


package com.groupsix.mobile_dashboard;
import java.io.BufferedReader;
import java.io.InputStreamReader;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

final Button btnSave=(Button)findViewById(R.id.btnSave);
final Button btnReset=(Button)findViewById(R.id.btnReset);
final EditText txtName=(EditText)findViewById(R.id.txtEmployeeName);
final EditText txtPhone=(EditText)findViewById(R.id.txtPhone);
final EditText txtCountry=(EditText)findViewById(R.id.txtCountry);

btnReset.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
txtName.setText("");
txtPhone.setText("");
txtCountry.setText(""); }
});

btn_save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(getApplicationContext(), "Connection not available", Toast.LENGTH_LONG).show();
String url = "http://127.0.0.1/tutorial/insert.php?";
url = url + "txtName=" + txtName.getText().toString() + "&";
url = url + "txtPhone=" + txtPhone.getText().toString() + "&";
url = url + "txtCountry=" + txtCountry.getText().toString(); executeQuery(url);
}
});
}
public void executeQuery(String url)  {
    //execute the URL, and get the result
try {
HttpClient http = new DefaultHttpClient();
HttpGet http_get = new HttpGet(url);
HttpResponse http_response = http.execute(http_get);
HttpEntity http_entity = http_response.getEntity();
BufferedReader br = new BufferedReader(new InputStreamReader(http_entity.getContent()));
String result = br.readLine();
Toast.makeText(Add_Schedule.this, result, Toast.LENGTH_LONG).show();
   }
catch (Exception ex) {
Toast.makeText(Add_Schedule.this, ex.getMessage(), Toast.LENGTH_LONG).show();
}
    }
}

PHP 

  • insert.php
Place this file inside:  "www/tutorial/".
For Linux give this Permission to "insert.php" file:  sudo chmod -R 777 insert.php

<?php

if( isset($_GET['txtName']) && isset($_GET['txtPhone']) && isset($_GET['txtCountry']) ) {
    
    $name = $_GET['txtName'];
    $phone = $_GET['txtPhone'];
    $country = $_GET['txtCountry'];

    $conn = mysql_connect("localhost","root","root");

   if(!$conn) {
       die("Could not connect to database:".mysql_error());
    }
    
    mysql_select_db("db_employee");
    $query = "INSERT INTO tbl_employee VALUES ('$name','$phone','$country')";

    $result = mysql_query($query,$conn);
    if(!$result) {
        die("Could not get data:".mysql_error());
    }
     
     echo "Data Saved!";
}