Categories
PHP

How to detect a user agent or device in PHP

In this tutorial we will see how you can check for a device or user agent using a third party script known as Mobile_Detect.php.

It is a lightweight PHP Class and can be integrated by downloading and requiring in your php script or can be installed via Composer.

For a quick integration, you can simply download the class from here:

https://github.com/serbanghita/Mobile-Detect/blob/master/Mobile_Detect.php

Below is a sample php script for testing whether the device or user agent is a Tablet, Desktop, Mobile etc.

<?php 
// Include and instantiate the class.
require_once 'Mobile_Detect.php';
$detect = new Mobile_Detect;
 
// Any mobile device (phones or tablets).
if ( $detect->isMobile() ) {
  echo "I am a mobile phone";
}
 
// Any tablet device.
if( $detect->isTablet() ){
  echo "I am a tablet";
}
 
// Exclude tablets.
if($detect->isMobile() && !$detect->isTablet() ){
  echo "I am  a mobile not a tablet";
}
 
if(!$detect->isMobile() && !$detect->isTablet() ){
  echo "I am neither a mobile nor a tablet";
}

// Check for a specific platform with the help of the magic methods:
if( $detect->isiOS() ){
  echo "I am on iOS"; 
}
 
if( $detect->isAndroidOS() ){
  echo "I am on Android"; 
}
 
 
// Batch mode using setUserAgent():
$userAgents = array(
'Mozilla/5.0 (Linux; Android 4.0.4; Desire HD Build/IMM76D) AppleWebKit/535.19 (KHTML, like Gecko) Chrome/18.0.1025.166 Mobile Safari/535.19',
'BlackBerry7100i/4.1.0 Profile/MIDP-2.0 Configuration/CLDC-1.1 VendorID/103',
// [...]
);
foreach($userAgents as $userAgent){
 
  $detect->setUserAgent($userAgent);
  $isMobile = $detect->isMobile();
  $isTablet = $detect->isTablet();
  // Use the force however you want.
 
}
 
// Get the version() of components.
// WARNING: this method is in BETA, some keyword properties will change in the future.
if($detect->version('iPad'))
  echo "I am iPad: "; // 4.3 (float)

if($detect->version('iPhone'))
  echo "I am iPhone: " ; // 3.1 (float)

if($detect->version('Android'))
  echo "I am Android: " ; // 2.1 (float)

if($detect->version('Opera Mini'))
  echo "I am Opera Mini: "; // 5.0 (float)

Categories
PHP Qcubed/Qcodo

How to use jQuery Datatables with Qcubed/Qcodo using Ajax Actions

In this tutorial we are going to look in to a quick way to use jQuery data tables with Ajax Actions. This can save you a lot of time if you are wanting to display custom reports or data in a tabular form that can be easily sorted, searched and paginated as well.

We will not use Qcubed/Qcodo datagrids for this example, Below are the sample php form and template file you will require. The code is pretty simple and self explanatory.

Below is the demo.php file

<?php
require ("framework/qcubed.inc.php"); 

	class DemoForm extends QForm {
		  
		protected $lstProduct;
		protected $calStartDate;
		protected $calEndDate;
		protected $btnSearch;
		protected $lblReport;
 
	    protected function Form_Run() { 
	    }
	    
	    protected function Form_Create() {
	        parent::Form_Create();
	         
			$this->lstProduct = new QListBox($this);
			$this->lstProduct->Name = "Product";
			$this->lstProduct->AddItem('-All Products-', null);
 			$arrAndCon = array();
 			$arrAndCon[] = QQ::Equal(QQN::Product()->Enabled, 1);
			$arrProduct = Product::QueryArray(QQ::AndCondition($arrAndCon), QQ::Clause(QQ::OrderBy(QQN::Product()->Name, true)));
			foreach($arrProduct as $objProduct){
				$this->lstProduct->AddItem($objProduct->Name, $objProduct->Id); 
			} 
			
			$this->calStartDate = new QCalendar($this);
 			$this->calStartDate->ShowButtonPanel = true;
			$this->calStartDate->Name = "Start Date"; 
 
			$this->calEndDate = new QCalendar($this);
 			$this->calEndDate->ShowButtonPanel = true;
			$this->calEndDate->Name = "End Date";
    
			$this->btnSearch = new QButton ( $this );
			$this->btnSearch->Text = QApplication::Translate ( 'Search' );
			$this->btnSearch->AddAction ( new QClickEvent (), new QAjaxAction ( 'btnSearch_Click' ) );
 			
			$this->lblReport = new QLabel($this);   
			$this->lblReport->HtmlEntities = false;
  	    } 

 

		protected function btnSearch_Click(){
		 
			if(!$this->calStartDate->DateTime) { $this->calStartDate->Warning = ("Start Date is required."); return false; }
			if(!$this->calEndDate->DateTime) { $this->calEndDate->Warning =  ("End Date is required."); return false; }
		

			$strHtml = <<<TTT
<table id="tblProduct" class="display" style="width:100%">
<thead>
<tr>
<th>Product</th> 
<th>Amount</th> 
</tr>
</thead>
<tbody>
TTT;
 

$arrProduct = array('TEST Product 1' => 12, "TEST Product 2" => 3);
foreach($arrProduct as $strName => $fltValue){  
 
$strHtml .=  "<tr>
<td>$strName</td> 
<td>".$fltValue."</td> 
</tr>";

}

$strHtml .= <<<TTT
</tbody>
<tfoot>
	<tr>
	<th>Product</th> 
	<th>Amount</th> 
	</tr>
</tfoot>
</table>
TTT;
 

$this->lblReport->Text = $strHtml;  

$strJS = "jQuery('#tblProduct').DataTable({'order': [[1, 'desc']]});";
QApplication::ExecuteJavascript($strJS);

		  
		}

    }
		     
	DemoForm::Run('DemoForm');
?>

and the demo.tpl.php

<?php $this->RenderBegin(); ?>


<section class="content">
    <div class="row">
        <div class="col-xs-12"><strong>Product:</strong><?php $this->lstProduct->RenderWithError(); ?></div>
    </div>

    <div class="row">
        <div class="col-xs-6"><strong>From Date:</strong><?php $this->calStartDate->RenderWithError(); ?></div>
        <div class="col-xs-6"><strong>To Date:</strong><?php $this->calEndDate->RenderWithError(); ?></div>
    </div>

    <div class="row">
        <div class="col-xs-12"> <?php $this->btnSearch->Render('CssClass=btn btn-primary'); ?></div>
    </div>
 
    <?php
    $this->lblReport->Render();
    ?>  
</section>
<?php $this->RenderEnd(); ?>
 

Please note in the tpl.php, in addition to the above code, the following Javascript library files and css files are required:

  • https://code.jquery.com/jquery-3.5.1.js
  • https://cdn.datatables.net/1.10.25/js/jquery.dataTables.min.js
  • https://cdn.datatables.net/1.10.25/css/jquery.dataTables.min.css