Monday 26 March 2012

Join Multiple Tables from Multiple Databases in PHP & in Codeigniter


// Join Multiple Tables from Multiple Databases in PHP
PS: You just you use the default database connection to execute the query or you can used either of any database connection if you have already made any other database connection instance. 

// This is for default database connection.
$sqlStr = "SELECT d1t1.id, t1.name, d2t2.no_of_sales, d3t2.customer_type
    FROM db1.Table1 as d1t1, db2.Table1 as d2t1, db3.Table2 as d3t2 
    WHERE d1t1.id='3' AND d3t2.customer_type='sales'";
$result = mysql_query($sqlStr);  
while($row = mysql_fetch_array($result))  {
  echo $id  = $row['id'];
        echo '\n';
        echo $name  = $row['name'];
        ..........
}
In the case of CodeIgniter, please find code below to pull the data from two or multiple database tables.


PS: You just you use the default ($this->db) database connection to execute the query or you can used either of any database connection if you have already made any other database connection instance.

// This is for default database connection.
$this->sqlStr= "SELECT d1t1.id, t1.name, d2t2.no_of_sales, d3t2.customer_type
                   FROM db1.Table1 as d1t1, db2.Table1 as d2t1, db3.Table2 as d3t2
     WHERE d1t1.id='3' AND d3t2.customer_type='sales'";
$query = $this->db->query($this->sqlStr);
$result = $query->row_array(); 
print_r($result);

Wednesday 21 March 2012

Create Multiple Databases Connections in Codeigniter


// To connect to Multiple Databases Connection in Codeigniter
// Open config/database.php and build the db connection array.

// This is for default database.
$db['default']['hostname'] = 'localhost';
$db['default']['username'] = 'root';
$db['default']['password'] = '';
$db['default']['database'] = 'db_default';
........
.......

// This is for second database database.
$db['db2']['hostname'] = 'localhost';
$db['db2']['username'] = 'root';
$db['db2']['password'] = '';
$db['db2']['database'] = 'database2';
........
.......
Now, open your model files and create the db instance as per needs for second database.

class Rc_model extends CI_Model {
 private $db2; 
 public function Rc_model()
 {
  parent::__construct();
  // $this->db; // this is for default database connection.
  // $this->db = $this->load->database(); // this is also for default connection if you need a db instance.
  // Creating the db object for Second Database whose connection is available globally.
  $this->db2 = $this->load->database('db2', TRUE);
  $this->db2 =& $this->db2;
 }

 public function GetAllCategories()
 {
  $query = $this->db2->get_where('categories_table', array('active'=>'1'));
  return $query->result();
 }
}

Friday 16 March 2012

Get Random Float Value From Two Float Numbers

Calculate the random value between two float numbers.

/**
 * Calculate the random value between two float numbers. 
 * @param float $min Minimum float range value
 * @param float $max Maximum float range value
 * @return float Random Float value
 */
public function random_float ( $min, $max ) {
     return ( $min + lcg_value() * ( abs ( $max - $min ) ) );
}

Thursday 15 March 2012

Finds whether the type of a variable is true Float or not

The final version of function to check the actual float value and return boolean(true/false).

public function isTrueFloat($val) 
{
    if(is_string($val)) $val = trim($val);
    if( is_numeric($val) && ( is_float($val) || ( (float) $val > (int) $val
       || strlen($val) != strlen( (int) $val) ) && (ceil($val)) != 0 ))
    { 
       return true; 
    }
    else return false;
}

The following are the test results from the above function.
//Tests
'4.0'       returns true
'2.1'       returns true
0           returns false
"0"         returns false
3.          returns true
13          returns false
"12"        returns false
3.53        returns true
// Additional Tested ones
"0.27"      returns true
0.24        returns true

Wednesday 14 March 2012

Remove Inline NULL values from PHP arrays

Remove NULL values from PHP arrays with 1 line (Inline)
I had an array with something like the following: Array ( [0] => [1] => test [2] => fun ). But I don’t want [0], the empty value in the array.
After searching the web for a good solution, I saw that people were using anywhere from 4 to 10+ lines of code to remove null values from arrays. This is obviously overkill so I decided to tackle the problem myself.
Remove NULL values only
1
$new_array_without_nulls = array_filter($array_with_nulls, 'strlen');
Remove any FALSE values
This includes NULL values, EMPTY arrays, etc. Thanks to Paul Scott for pointing out this method.
1
$new_array_without_nulls = array_filter($array_with_nulls);