Friday, February 1, 2013

php array delete by value (not key)

Using array_seach(), try the following:
if(($key = array_search($del_val, $messages)) !== false) {
    unset($messages[$key]);
}

array_search() returns the key of the element it finds, which can be used to remove that element from the original array using unset(). It will return FALSE on failure, however it can return a "falsey" value on success (your key may be 0 for example), which is why the strict comparison !== operator is used.
The if() statement will check whether array_search() returned a value, and will only perform an action if it did.

View print cakephp generated mysql query

$log = $this->controller->UserRequestFeed->getDataSource()->getLog(false, false);
debug($log);

Tuesday, January 22, 2013

A find condition about hasMany(has many) relationship in cakephp

Suppose you have two model User and UserRole.
And User has many UserRole.
Now you want to find User By specific UserRole.

User:
Fields: id, name, others........
var $hasMany = array(
    "UserRole"
);

UserRole:
Fields: id, user_id, role_id, others...var 
$belongsTo = array("User");

First you need to find all UserRole by your own conditions:

$userRoles = $this->UserRole->find("all", array(
    "conditions" => array(
        "UserRole.condition" => $value
    )
));

Now find User by UserRole as follows:

$users = $this->User->find("all", array(
    "conditions" => array(
        "User.id" => Set::extract("/UserRole/user_id", $userRoles)
    )
));


BINGO...

Monday, January 21, 2013

stop a page from exit or unload using jquery

<html>
<head>
<title>Refresh a page in jQuery</title>
 
<script type="text/javascript" src="jquery-1.4.2.min.js"></script>
 
</head>
 
<body>
 
<h1>Stop a page from exit with jQuery</h1>
 
<button id="reload">Refresh a Page in jQuery</button>
 
<script type="text/javascript">
 
 $('#reload').click(function() {
 
   location.reload();
 
 });
 
 $(window).bind('beforeunload', function(){
  return 'Are you sure want to leaving?';
 });
 
</script>
 
</body>
</html>

convert string to input stream(InputStream) using java

package com.pritom;
 
import java.io.BufferedReader;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
 
public class StringToInputStreamExample {
    public static void main(String[] args) throws IOException {
    String str = "This is a String to InputStream";
 
    // convert String into InputStream
    InputStream is = new ByteArrayInputStream(str.getBytes());
 
    // read it with BufferedReader
    BufferedReader br = new BufferedReader(new InputStreamReader(is));
 
    String line;
    while ((line = br.readLine()) != null) {
       System.out.println(line);
    }
 
    br.close();
   }
}

Friday, January 18, 2013

Cakephp delete and update records by conditions

Delete Records:
$this->ModelName->deleteAll(
    array(
        "ModelName.id" => 10
    )
);

Update Records:
$this->ModelName->updateAll(
    /* UPDATE FIELD */
    array(
        "ModelName.is_active" => 0,
    ),
    /* CONDITIONS */
    array(
        "ModelName.id" => 20,
        "ModelName.name LIKE" => '%SEARCH_TEXT%',
        "ModelName.name <>" => 'NOT_EQUAL_VALUE',
        'ModelName.integer_field > ' => 50, [Can use operand here]
        'TIMESTAMPDIFF(DAY, ModelName.date, NOW())' => 22 [number of day]
    )
);

jQuery post call using ajax

jQuery.ajax({
    type: "POST",
    url: "http://domain.com/save_form_data",
    data: jQuery("form").serializeArray(),
    success: function(data) {
        console.log(data);
        alert("Success");
    },
    error: function() {
       alert("Error occurred."); 
    }
});