Using MySQL multi query and creating a counter variable in PHP

cntI had this problem where I need to change the order of the products we have and ¬†update them from the most popular to least popular. So what I did, I declared a variable that will act as a counter and set the column order_num to the current value of the counter. Here’s what I did:

$db = new mysqli("server", "user", "pass", "db_name");

$query = "set @cnt := 0;";

$query .= "update tbl set order_num = (@cnt := @cnt + 1);";


You can also order specific record by adding an IN clause like:

$query .= "update tbl set order_num = (@cnt := @cnt + 1) where id IN (23, 76, 9, 45) order by field(id, 23, 76, 9, 45);";

Adding PHP Mobile Detect in CakePHP1.3 as a component

cake-logoIt seems that the library is not available for old version of CakePHP specifically in 1.3 only in 2.x. So what I did I converted the library as a component, there’s is nothing special about the conversion actually only if you are new to the Framework. Just download the file and unzip it, put it in your /app/controller/components directory.

Download >> Mobile Detect

How to use it:

In one of your controllers, append it in your $components variable like:

Class MyPageController extends AppController
  public $components = array('MobileDetect'); // adding the libraby in to your components

  public function beforeFilter() {

  public function beforeRender() {
    // check the user's device and render the proper layout
    if($this->MobileDetect->isTablet()) {
      $this->layout = 'tablets';
    } elseif($this->MobileDetect->isMobile()) {
      $this->layout = 'mobiles';
    } else {
      $this->layout = 'default';

To download the original file go here.