dirname(Yii::app()->request->scriptFile)
Tuesday, April 30, 2013
CGridView a CLinkColumn with sortable header using yii
I have it working with the latest yii (non-stable but might work with the current stable release). (could do with some improvement but it does what I need)
Try this; create a new component under components/SCLinkColumnWithSort.php
Try this; create a new component under components/SCLinkColumnWithSort.php
<?php class SCLinkColumnWithSort extends CLinkColumn { public $name; public $sortable = true; public $filter; protected function renderFilterCellContent() { $this->linkHtmlOptions['class'] = $this->name; if($this->filter!==false && $this->grid->filter!==null && strpos($this->name,'.')===false) { if(is_array($this->filter)) echo CHtml::activeDropDownList($this->grid->filter, $this->name, $this->filter, array('id'=>false,'prompt'=>'')); else if($this->filter===null) echo CHtml::activeTextField($this->grid->filter, $this->name, array('id'=>false)); else echo $this->filter; } else parent::renderFilterCellContent(); } protected function renderHeaderCellContent() { if($this->grid->enableSorting && $this->sortable && $this->name!==null) echo $this->grid->dataProvider->getSort()->link($this->name,$this->header); else parent::renderHeaderCellContent(); } } ?>
Use this as following:
<?php $this->widget('zii.widgets.grid.CGridView', array( 'dataProvider' => $dataProvider, 'columns' => array( array( 'class' => 'SCLinkColumnWithSort', 'name' => 'column_name_to_sort', ) ) )) ?>
Or you can directly use, but upper one used for more customization:
array( 'name' => 'mobile_number', 'type' => 'raw', 'value' => 'CHtml::link($data->mobile_number,$data->contact_id)' )
Where mobile_number is filed value in name, in value, mobile_number is for sorting
and contact_id is for making link.
Monday, April 29, 2013
Create and download csv file from array using php
Create and download csv file from array using php.
<?php function arrayToCsv( array $fields, $delimiter = ';', $enclosure = '"', $encloseAll = false, $nullToMysqlNull = false ) { $delimiter_esc = preg_quote($delimiter, '/'); $enclosure_esc = preg_quote($enclosure, '/'); $outputString = ""; foreach($fields as $tempFields) { $output = array(); foreach ( $tempFields as $field ) { if ($field === null && $nullToMysqlNull) { $output[] = "NULL"; continue; } // Enclose fields containing $delimiter, $enclosure or whitespace if ( $encloseAll || preg_match( "/(?:${delimiter_esc}|${enclosure_esc}|\s)/", $field ) ) { $output[] = $enclosure . str_replace($enclosure, $enclosure . $enclosure, $field) . $enclosure; } else { $output[] = $field; } } $outputString .= implode( $delimiter, $output )."\n"; } return $outputString; } ?>
<?php $dataArray = array(); array_push($dataArray, array( "First Name", "Last Name", "Number", "Group" )); foreach($dataList as $index => $data) { array_push($dataArray, array( "".$data["first_name"], "".$data["last_name"], "".$data["mobile_number"], "".$data["group_name"] )); } $csvString = arrayToCsv($dataArray); header("Content-type: text/csv"); header("Content-Disposition: attachment; filename=list.csv"); header("Pragma: no-cache"); header("Expires: 0"); echo $csvString; ?>
Php check a value exists in multi level array and return path
Search in array using php by value if the array is any depth data.
Output:
1. True (Find or not)
2. Path to this value.
<?php function array_value_exists($search = "", $searchArray = array(), $returnKey = false, &$returnKeyArray = array(), $level = 0) { $returnValue = false; $search = trim(strval($search)); if(strlen($search) == 0) { return false; } if(is_null($searchArray)) { return false; } if(!is_array($searchArray)) { return false; } foreach($searchArray as $key => $value) { array_push($returnKeyArray, $key); if(is_string($value)) { if($search == trim($value)) { $returnValue = true; break; } } else if(is_array($value)) { $returnValue = array_value_exists($search, $value, false, $returnKeyArray); if($returnValue == true) { break; } } } if($returnKey == true) { return $returnKeyArray; } return $returnValue; } ?> Use:<?php $ary = array( "bm" => array( "dev" => array( "pritom" => array( "email" => "pritomkucse@yahoo.com" ), "touhid" => array( "email" => "touhid@yahoo.com" ) ), "designer" => array( "dipu" => array( "email" => "dipu@yahoo.com" ) ) ), "gpit" => array( "dev" => array( "sanat" => array( "email" => "sanat@gpit.com" ) ) ) ); $has = array_value_exists("pritomkucse@yahoo.com", $ary); echo $has == true ? "True" : "False"; print_r(array_value_exists("pritomkucse@yahoo.com", $ary, true)); ?>
Output:
1. True (Find or not)
2. Path to this value.
Array ( [0] => bm [1] => dev [2] => pritom [3] => email )
Read and parse csv file using php code
Details http://code.google.com/p/parsecsv-for-php/
Or download from here
Use:
require_once('../parsecsv.lib.php');
# create new parseCSV object.
$csv = new parseCSV();
# Parse '_books.csv' using automatic delimiter detection...
$csv->conditions = 'author does not contain dan brown';
$csv->conditions = 'rating < 4 OR author is John Twelve Hawks';
$csv->conditions = 'rating > 4 AND author is Dan Brown';
$csv->sort_by = 'title';
# offset from the beginning of the file,
# ignoring the first X number of rows.
$csv->offset = 2;
# limit the number of returned rows.
$csv->limit = 3;
$csv->auto('_books.csv');
foreach ($csv->titles as $value);
foreach ($csv->data as $key => $row);
Or download from here
Use:
require_once('../parsecsv.lib.php');
# create new parseCSV object.
$csv = new parseCSV();
# Parse '_books.csv' using automatic delimiter detection...
$csv->conditions = 'author does not contain dan brown';
$csv->conditions = 'rating < 4 OR author is John Twelve Hawks';
$csv->conditions = 'rating > 4 AND author is Dan Brown';
$csv->sort_by = 'title';
# offset from the beginning of the file,
# ignoring the first X number of rows.
$csv->offset = 2;
# limit the number of returned rows.
$csv->limit = 3;
$csv->auto('_books.csv');
foreach ($csv->titles as $value);
foreach ($csv->data as $key => $row);
Saturday, April 27, 2013
Android custom dialog example
In this tutorial, we show you how to create a custom dialog in Android. See following steps :
File : res/layout/main.xml
File : res/layout/login.xml
Read the comment and demo in next step, it should be self-explorary.
- Create a custom dialog layout (XML file).
- Attach the layout to
Dialog
. - Display the
Dialog
. - Done.
1 Android Layout Files
Two XML files, one for main screen, one for custom dialog.File : res/layout/main.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnLogin" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Login Dialog" /> </LinearLayout>
<?xml version="1.0" encoding="UTF-8"?> <TableLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/tableLayout1" android:layout_width="fill_parent" android:layout_height="fill_parent" > <TextView android:id="@+id/textView1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@string/sldjfsjdflsdf" android:textAppearance="?android:attr/textAppearanceLarge" /> <LinearLayout android:id="@+id/linearLayout1" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView1" android:layout_width="45dp" android:layout_height="45dp" android:src="@drawable/user" /> <EditText android:id="@+id/editText1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" > <requestFocus /> </EditText> </LinearLayout> <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="wrap_content" > <ImageView android:id="@+id/imageView2" android:layout_width="45dp" android:layout_height="45dp" android:src="@drawable/password" /> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1" android:inputType="textPassword" /> </LinearLayout> <Button android:id="@+id/btnLoginPrompt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/login" /> </TableLayout>
Button btnLogin = (Button)findViewById(R.id.btnLogin); btnLogin.setOnClickListener(new OnClickListener() { public void onClick(View arg0) { final Dialog dialog = new Dialog(ContactspaceandroidActivity.this); dialog.setContentView(R.layout.login); dialog.setTitle("Title..."); dialog.show(); Button azxc = (Button) dialog.findViewById(R.id.btnLoginPrompt); azxc.setOnClickListener(new OnClickListener() { @Override public void onClick(View arg0) { System.out.println("BTN LOGIN CLICKED."); EditText txtUserName = (EditText) dialog.findViewById(R.id.txtUserName); EditText txtPassword = (EditText) dialog.findViewById(R.id.txtPassword); if(txtUserName.getText().length() <= 0) { Toast.makeText(ContactspaceandroidActivity.this, "Enter username.", Toast.LENGTH_LONG).show(); return; } if(txtPassword.getText().length() <= 0) { Toast.makeText(ContactspaceandroidActivity.this, "Enter password.", Toast.LENGTH_LONG).show(); return; } } }); } });
Php making a soap based server with basic authentication
Compared to the standard authentication in the rest of your applications, SOAP authentication isn't too complicated. There are two easy ways of integrating it into your SOAP server: using HTTP Basic authentication or processing a custom SOAP header.
Make sure you have soap enabled in your server machine. Write "phpinfo();" and look for the word "Soap Client" as below:
If you don't find above soap enabled then you have to enable it. Navigate explorer to /c/xampp/php and edit "php.ini" and uncomment the line marked in below image and restart server and check again if your server has soap enabled:
Soap-server:
<?php
function pc_authenticate_user($username, $password)
{
$is_valid = false;
if(strlen(trim($username)) > 0 && trim($username) == "adm" && strlen(trim($password)) > 0 && trim($password) == "pwd") {
$is_valid = true;
}
if ($is_valid) {
return true;
}
else {
return false;
}
}
class pc_SOAP_return_time
{
public function __construct() {
// Throw SOAP fault for invalid username and password combo
if (!pc_authenticate_user($_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW']) ) {
throw new SOAPFault("Incorrect username and password combination.", 401);
}
}
function return_time($name, $lastName) {
return "Time: ".time().", First Name: ".$name.", Last Name: ".$lastName;
}
function accept_xml() {
$postData = file_get_contents("php://input");
return $postData;
}
}
$server = new SOAPServer(null, array('uri' => 'urn:pc_SOAP_return_time'));
$server->setClass('pc_SOAP_return_time');
$server->handle();
Soap-client
<?php $opts = array( 'location' => 'http://localhost/ci/dragon71/soap-check/server.php', 'uri' => 'urn:pc_SOAP_return_time', 'login' => 'adm', 'password' => 'pwd' ); $client = new SOAPClient(null, $opts); $result = $client->__soapCall('return_time', array("Pritom", "Kumar")); print_r($result);
Or
<?php $xml = '<?xml version="1.0" encoding="utf-8"?>'. '<soap:Envelope xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'. ' xmlns:xsd="http://www.w3.org/2001/XMLSchema"'. ' xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">'. '<soap:Body>'. '<accept_xml xmlns="http://localhost/ci/dragon71/soap-check/server.php/">'. '<ItemId>15</ItemId>'. '</accept_xml>'. '</soap:Body>'. '</soap:Envelope>'; $url = "http://localhost/ci/dragon71/soap-check/server.php"; $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0); $headers = array(); array_push($headers, "Content-Type: text/xml; charset=utf-8"); array_push($headers, "Accept: text/xml"); array_push($headers, "Cache-Control: no-cache"); array_push($headers, "Pragma: no-cache"); if($xml != null) { curl_setopt($ch, CURLOPT_POSTFIELDS, "$xml"); array_push($headers, "Content-Length: " . strlen($xml)); } curl_setopt($ch, CURLOPT_USERPWD, "adm:pwd"); /* If required */ curl_setopt($ch, CURLOPT_POST, true); curl_setopt($ch, CURLOPT_HTTPHEADER, $headers); $response = curl_exec($ch); $code = curl_getinfo($ch, CURLINFO_HTTP_CODE); header('Content-Type: text/xml'); print_r($response);
Make sure you have soap enabled in your server machine. Write "phpinfo();" and look for the word "Soap Client" as below:
If you don't find above soap enabled then you have to enable it. Navigate explorer to /c/xampp/php and edit "php.ini" and uncomment the line marked in below image and restart server and check again if your server has soap enabled:
Subscribe to:
Posts (Atom)