Saturday, March 30, 2013

jquery find element contains text

eq expects a numerical index to only return a single row. If you want to match a td by its contents, you have to use the :contains selector. Saying "it doesn't work" and throwing it away is not the right approach to the problem, as the selector is (most likely) not at fault (Do note its case sensitive, which might be it...)
Anyhow, if you have a table like this:
<table>
  <tr>
    <td>Hello</td>
    <td>World</td>
  </tr>
  <tr>
    <td>World</td>
    <td>Hello</td>
  </tr>
  <tr>
    <td>Hello</td>
    <td>Hello</td>
  </tr>
</table>
This jQuery code:
$(function() {
    $("td:contains('Hello')").css('color','red');
});
Will turn all cells with "Hello" to red. Demo.
If you need a case insensitive match, you could do this, using the filter function:
$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase().indexOf(search) != -1;
    }).css('color','red');
});
If you need to match the exact contents of the cell, you could use something similar to the above:
$(function() {
    var search = 'HELLO'.toLowerCase();
    $("td").filter(function() {
        return $(this).text().toLowerCase() == search;
    }).css('color','red');
});
The above is case insensitive (by turning both the search and the contents to lower case when comparing) otherwise you can just remove those if you want case sensitivity. Demo.

Friday, March 1, 2013

php authentication basic username and password

index.php


<?php 
if (!isset($_SERVER['PHP_AUTH_USER']) || !isset($_SERVER['PHP_AUTH_PW'])) {
    header('WWW-Authenticate: Basic realm="Need Authentication"');
    header('HTTP/1.0 401 Unauthorized');
    echo 'Need Authentication';
    exit;
} else {
    if(empty($_SERVER["PHP_AUTH_USER"]) || $_SERVER["PHP_AUTH_USER"] != "apm"
        || empty($_SERVER["PHP_AUTH_PW"]) || $_SERVER["PHP_AUTH_PW"] != "gh3412") {
        header('WWW-Authenticate: Basic realm="Authentication Error"');
        header('HTTP/1.0 401 Unauthorized');
        echo 'Authentication Error';
        exit;
    }
}
?>

 
<?php 
$ch = curl_init();

if(is_array($get_variables))
{
    $get_variables = '?' . str_replace('&amp;','&',urldecode(http_build_query($get_variables)));
}
else
{
    $get_variables = null;
}
curl_setopt($ch, CURLOPT_URL, "http://dom.com/index.php" . $get_variables);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); //CURL doesn't like google's cert
curl_setopt($ch, CURLOPT_PROXYUSERPWD, 'adm:pwd3012');
$headers[0] = "Authorization: Basic " . base64_encode("apm:gh3412");

if($post_variables == null || !is_array($post_variables)) {
    $post_variables = array();
}
if(is_array($post_variables))
{
    curl_setopt($ch, CURLOPT_POST, true);
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post_variables);
}

if(is_array($headers))
{
    curl_setopt($ch, CURLOPT_HTTPHEADER,$headers);
}

$response = curl_exec($ch);
$code = curl_getinfo($ch,CURLINFO_HTTP_CODE);

curl_close($ch);
?> 
 

Sending a username and password with PHP file_get_contents()

In the example below, $url is the web address you want to get data from, and $username and $password are the login credentials.
$context = stream_context_create(array(
    'http' => array(
        'header'  => "Authorization: Basic " . base64_encode("$username:$password")
    )
));
$data = file_get_contents($url, false, $context);
If the login details were incorrect, or you were attempting to access a password protected location and didn't pass any credentials at all, you'll see an error like this:
Warning: file_get_contents(...): failed to open stream: HTTP request failed! 
HTTP/1.1 401 Authorization Required in ... on line 4

http://www.electrictoolbox.com/php-file-get-contents-sending-username-password/ 

How to find if an array contains a specific string in JavaScript/jQuery?

var cat = [];
cat.push('1');
cat.push('2');
cat.push('3');

var found = $.inArray('1', cat) > -1;

Wednesday, February 27, 2013

use multiple database in one cakephp project

Your app/Config/database.php file should look like this

<?php 
class DATABASE_CONFIG {

 public $default = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'root',
  'password' => '',
  'database' => 'spamassassin',
  'prefix' => '',
  //'encoding' => 'utf8',
 );

 public $vpopmail = array(
  'datasource' => 'Database/Mysql',
  'persistent' => false,
  'host' => 'localhost',
  'login' => 'root',
  'password' => '',
  'database' => 'vpopmail',
  'prefix' => '',
  //'encoding' => 'utf8',
 );
}
?> 

Your Model class should look like this to use database "spamassassin"

<?php
class Userpref extends AppModel {
    public $name = 'Userpref';
    var $primaryKey = "prefid";
    public $useTable = 'userpref';
}
?>

Your Model class should look like this to use database "vpopmail"


<?php
class VPopMailSetting extends AppModel {
    public $name = "VPopMailSetting";
    var $useDbConfig = 'vpopmail';
    var $useTable = 'settings';
}
?>