How to Detect Browser Type in PHP


Check your Browser Type in PHP

Here’s an example code which can be used in PHP to detect and retrieve the web browser type as variable or used directly in comparison function.


<?php

$user_agent = $_SERVER['HTTP_USER_AGENT']; 

if (preg_match('/MSIE/i', $user_agent))
{ 
      echo "Internet Explorer";
}else{
      echo "Non-IE Browser";
} 
OR

if (strstr($user_agent,'MSIE'))
{
      echo "Internet Explorer";
} else {
      echo "Non-IE Browser";
} 
>>

Script above will detect if the visitor is using IE (Internet Explorer) browser. To code can be expanded to detect additional browser with elseif conditional tag. To check for other browser, just replace the MSIE with the user agent name for other browser. Some popular web browsers can be identified with the following string:

Internet Explorer: MSIE
Mozilla Firefox: Firefox
Google Chrome: Chrome
Apple Safari: Safari
Opera: Opera
Netscape Navigator: Netscape
Flock: Flock
Lynx: Lynx

Tip: It’s possible to use “strtolower” to make the $user_agent to all lower case so that all PHP script does not miss out on browsers which do not use standard naming convention in user agent. In this case, remember to compare against user agent string which is lower case too, such as msie, firefox and etc.

How to read xml value with specific attribute


 

php getElementsByTagName with specific attribute

Read xml value with specific attribute

XML format is :

File Name = locallang_db.xml

<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<T3locallang>
    <meta type="array">
        <type>database</type>
        <description>Description..</description>
    </meta>
    <data type="array">
        <languageKey index="default" type="array">
            <label index="name">Jainish</label>
            <label index="surname">Senjaliya</label>
            <label index="dob">19-11-19XX</label>
            <label index="age">Top Secret</label>
        </languageKey>
    </data>
</T3locallang>	

Read XML File from PHP

$xmlPath = 'locallang_db.xml';
$objDOM = new DOMDocument('1.0', 'utf-8');
$objDOM->load($xmlPath); //make sure path is correct 
$label = $objDOM->getElementsByTagName("label"); 

foreach ($label as $val)
{
    if($val->getAttribute('index') === 'name')
    {
        echo  $val->nodeValue;
        // do something
    }
 // You can also get tag name by : $val->tagName OR $val->nodeName;
}		

OutPut :

 Jainish

Best Luck 🙂

If you have any query then feel free to contact me at Jainish Senjaliya

How to hack mysql group concat to fetch a limited number of rows


 

Limit record in group_concat + mysql

SELECT SUBSTRING_INDEX(GROUP_CONCAT(Field1 SEPARATOR ‘,’), ‘,’, [# of elements to return]) FROM TABLE;

Below SQL Query is for fetch subcategory value in a single field using “GROUP_CONCAT”.
if you want fetch limited no. of record then you can use

SUBSTRING_INDEX(GROUP_CONCAT(Field1 SEPARATOR ','), ',', 
  [# of elements to return])

SQL Query for display 4 subcategory list in a single query :

SELECT a.*, 
SUBSTRING_INDEX(GROUP_CONCAT(cast( b.uid AS char )), ',',4)
   as subcategories,
SUBSTRING_INDEX(GROUP_CONCAT(b.title SEPARATOR '#'), '#',4) 
  as subcategories_name
FROM `tx_jsproduct_category` AS a
LEFT JOIN `tx_jsproduct_category` AS b ON b.parent_id = a.uid
WHERE a.deleted =0
AND a.hidden =0
AND b.deleted =0
AND b.hidden =0
AND a.uid IN ( 1, 3, 2, 4  )
GROUP BY (
a.uid
)
ORDER BY  field( a.uid, 1, 3, 2, 4 )
LIMIT 0 , 30

Note:

SUBSTRING_INDEX(GROUP_CONCAT(b.uid SEPARATOR ','), ',',4) as 
  subcategories

4 is stand for limited record

Best Luck 🙂

If you have any query then feel free to contact me at Jainish Senjaliya

How to get currnet page title and page id in custom extension.


Below code is for display current page id in custom extension

$GLOBALS['TSFE']->id;

This code is for display current page title in custom extension

$arr  = $GLOBALS['TSFE']->rootLine;
$titlArr = array_shift(array_values( $arr ));
echo $titlArr['title'];

If you have any query then feel free to contact me at Jainish Senjaliya

Redirect page when page is not found in typo3


404 redirection in typo3

You should have to set below 2 line in “localconf.php” file

$TYPO3_CONF_VARS['FE']['pageNotFound_handling'] = 
	'REDIRECT:http://YOUR_SITE_PATH/index.php?id=420';
$TYPO3_CONF_VARS["FE"]["pageNotFound_handling_statheader"] = 
	'HTTP/1.1 404 Not Found';

Best Luck 🙂

If you have any query then feel free to contact me at Jainish Senjaliya

How to add blank value in the starting of the selection box of flexform


You can add a blank value in the beginning of the selection box (flexform).

Add below code in flexform for add blank value in the starting of the selection box.

<items type="array">
   <numIndex index="100">
   <numIndex index="0">==== Select ==== </numIndex>
      <numIndex index="1">0</numIndex>
   </numIndex>
</items>

For Selection box in flexform

<companyList>
  <TCEforms>
    <label>
      LLL:EXT:EXTENSION_NAME/flex-label.xml:
      EXTENSION_NAME.select_category
    </label>
  <config>
    <type>select</type>
      <foreign_table>TABLE_NAME</foreign_table>
      <foreign_table_where> 
          AND TABLE_NAME.deleted=0 AND TABLE_NAME.hidden=0 
          ORDER BY  TABLE_NAME.uid
      </foreign_table_where>
      <size>10</size>
      <autoSizeMax>10</autoSizeMax>
      <minitems>0</minitems>
      <maxitems>1</maxitems>
      <items type="array">
          <numIndex index="100">
               <numIndex index="0">==== Select ==== </numIndex>
               <numIndex index="1">0</numIndex>
          </numIndex>
      </items>
      <show_thumbs>1</show_thumbs>
      <allowNonIdValues>1</allowNonIdValues>
  </config>
  </TCEforms>
</companyList>

If you have any query then feel free to contact me at Jainish Senjaliya