Pages

Showing posts with label PHP. Show all posts
Showing posts with label PHP. Show all posts

Wednesday, March 6, 2013

Get resource id from resource name in modx revolution


$resource = $modx->getObject('modResource',Resource_Id);
$longTitle = $resource->get('pagetitle');

Here, from above code you can get pagetitle from resource id(Resource_Id).



Get resource id from resource name

$resource = $modx->getObject('modResource','Resource_Name');
$longTitle = $resource->get('id');

in this code you can get resource id from resource pagetitle('Resource_Name').

Get template id from template name modx revolution

$templateObj = $modx->resource->getOne('Template',array('templatename'=>'Template_Name'));
$templateid = $templateObj->get('id');

Here, in this code "Template_Name" is template name from which user want to get template id.

Wednesday, December 12, 2012

TV image preview modx Revo

There is a bug in modx revo that in manager i can't see image preview in TV.

Solutions is :

So, following the method mentioned in the bug report, I simply changed Line 219 in core/model/phpthumb/phpthumb.class.php:

From :
$this->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? $_SERVER['DOCUMENT_ROOT'] : $this->config_document_root);

To :
$this->config_document_root = (@$_SERVER['DOCUMENT_ROOT'] ? '/home/username/public_html' : $this->config_document_root);

and it's worked for me.

Saturday, February 25, 2012

How to check if GD Library is installed?

Try this:

php Code: 
<?php 
if (extension_loaded('gd') && function_exists('gd_info')) {
     echo "It looks like GD is installed";  
}
?>


You could also spit some info about your GD version by doing this:

php Code: 
<?php
  echo "<pre>"; var_dump(gd_info()); echo "</pre>";
 ?>
 
You will definitely get the ans of your GD Library is installed or not.

Tuesday, January 10, 2012

Convert BBCode to HTML in PHP

function bb2html($message)
        {                               
            $preg = array(
                '/(?<!\\\\)\[COLOR(?::\w+)?=(.*?)\](.*?)\[\/COLOR(?::\w+)?\]/si'   => "<span style=\"color:\\1\">\\2</span>",
                '/(?<!\\\\)\[SIZE(?::\w+)?=(.*?)\](.*?)\[\/SIZE(?::\w+)?\]/si'     => "<span style=\"font-size:\\1\">\\2</span>",
                '/(?<!\\\\)\[FONT(?::\w+)?=(.*?)\](.*?)\[\/FONT(?::\w+)?\]/si'     => "<span style=\"font-family:\\1\">\\2</span>",
                '/(?<!\\\\)\[ALIGN(?::\w+)?=(.*?)\](.*?)\[\/ALIGN(?::\w+)?\]/si'   => "<div style=\"text-align:\\1\">\\2</div>",
                '/(?<!\\\\)\[B(?::\w+)?\](.*?)\[\/B(?::\w+)?\]/si'                 => "<span style=\"font-weight:bold\">\\1</span>",
                '/(?<!\\\\)\[I(?::\w+)?\](.*?)\[\/I(?::\w+)?\]/si'                 => "<span style=\"font-style:italic\">\\1</span>",
                '/(?<!\\\\)\[U(?::\w+)?\](.*?)\[\/U(?::\w+)?\]/si'                 => "<span style=\"text-decoration:underline\">\\1</span>",
                '/(?<!\\\\)\[CENTER(?::\w+)?\](.*?)\[\/CENTER(?::\w+)?\]/si'       => "<div style=\"text-align:center\">\\1</div>",
                               
                // [email]
                '/(?<!\\\\)\[EMAIL(?::\w+)?\](.*?)\[\/EMAIL(?::\w+)?\]/si'         => "<a href=\"mailto:\\1\" class=\"bb-email\">\\1</a>",
                '/(?<!\\\\)\[EMAIL(?::\w+)?=(.*?)\](.*?)\[\/EMAIL(?::\w+)?\]/si'   => "<a href=\"mailto:\\1\" class=\"bb-email\">\\2</a>",
                // [url]
                '/(?<!\\\\)\[URL(?::\w+)?\]www\.(.*?)\[\/URL(?::\w+)?\]/si'        => "<a href=\"http://www.\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?\](.*?)\[\/URL(?::\w+)?\]/si'             => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\1</a>",
                '/(?<!\\\\)\[URL(?::\w+)?=(.*?)?\](.*?)\[\/URL(?::\w+)?\]/si'      => "<a href=\"\\1\" target=\"_blank\" class=\"bb-url\">\\2</a>",
                // [img]
                '/(?<!\\\\)\[IMG(?::\w+)?\](.*?)\[\/IMG(?::\w+)?\]/si'             => "<img src=\"\\1\" alt=\"\\1\" class=\"bb-image\" />",
                '/(?<!\\\\)\[IMG(?::\w+)?=(.*?)x(.*?)\](.*?)\[\/IMG(?::\w+)?\]/si' => "<img width=\"\\1\" height=\"\\2\" src=\"\\3\" alt=\"\\3\" class=\"bb-image\" />",
                                       
                // [list]
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\*(?::\w+)?\](.*?)(?=(?:\s*<br\s*\/?>\s*)?\[\*|(?:\s*<br\s*\/?>\s*)?\[\/?LIST)/si' => "\n<li class=\"bb-listitem\">\\1</li>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST(:(?!u|o)\w+)?\](?:<br\s*\/?>)?/si'    => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:u(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ul>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[\/LIST:o(:\w+)?\](?:<br\s*\/?>)?/si'         => "\n</ol>",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(:(?!u|o)\w+)?\]\s*(?:<br\s*\/?>)?/si'   => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:u(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ul class=\"bb-list-unordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST:o(:\w+)?\]\s*(?:<br\s*\/?>)?/si'        => "\n<ol class=\"bb-list-ordered\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=1\]\s*(?:<br\s*\/?>)?/si' => "\n<ol class=\"bb-list-ordered,bb-list-ordered-d\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=i\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-lr\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=I\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ur\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=a\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-la\">",
                '/(?<!\\\\)(?:\s*<br\s*\/?>\s*)?\[LIST(?::o)?(:\w+)?=A\]\s*(?:<br\s*\/?>)?/s'  => "\n<ol class=\"bb-list-ordered,bb-list-ordered-ua\">",
                                       
                //line breaks
                '/\n/'                                                               => "<br>",
                // escaped tags like \[b], \[color], \[url], ...
                '/\\\\(\[\/?\w+(?::\w+)*\])/'                                      => "\\1"
                               
            );
                               
            $message = preg_replace(array_keys($preg), array_values($preg), $message);
            return $message;
        }


// Call Function bb2html and echo

$htmltext = bb2html($message);
echo $htmltext;

Tuesday, November 29, 2011

Sorting strings and numbers(varchar type column) in SQL query

Problem :
                  I have a problem with sorting by a string-column when i use SQL query on MySQLServer. There are both numbers, strings and mixture of both in this column and when i sort it numbers are not looks in sorting. ex. 7.5 6 14 8.5 9. I would like to sort numbers numerically and both strings and mixed alphabetically, 14 6 7.5 8.5 9.


Soluation :

Set your query in sorting by using "order by '0000000000'+rtrim(FEILDNAME)" 

Where FEILDNAME is your varchar type column name which you want to sort. This will build up a string for the numbers with leading zeros, so they will get sorted correctly.

Thursday, August 18, 2011

Image download using CURL - PHP

$url = 'IMAGE PATH URL';
  //$random_no = rand(10000,99999999);  // for random number
  //$file_handler = fopen($random_no.'.jpeg', 'w');  // random number image name
$file_handler = fopen('image1.jpeg', 'w');   // static image name as image1
$curl = curl_init($url);
curl_setopt($curl, CURLOPT_FILE, $file_handler);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_exec($curl);
curl_close($curl);
fclose($file_handler);

Using this code you can download image from server where 'IMAGE PATH URL' is the url of image. Image is generated in jpeg file extension named as image1 or you can use random generated name.

Hope its help you.

Monday, August 8, 2011

Assign Different Menu for Login user and unregistered user

Sometimes you need to check whether user login or not for assign different condition to both login user and unregistered user in joomla customization. Only you have to write if else condition to check that. For example in joomla site you need to assign different menus for frontend. before login users can see a normal menu but after user login they will see another menu. you can write the code in the template index file and assign different menu.

<?php  
   $user =& JFactory::getUser(); 
   if ($user->get('id') == NULL) {   
?>   
    <jdoc:include type="modules" name="nonregister_menu" 
<?php
   } 
  elseif ($user->get('id') != NULL) {  
?>
   <jdoc:include type="modules" name="loginuser_menu">    
<?php 
   }
 ?> 

How to get current joomla User ID and Username

For joomla starter, sometime we need to get joomla user ID and username to customize in joomla component, plugins or modules. for that we have to write simple code

<?php
    $user =& JFactory::getUser();   
    $usr_id = $user->get('id');   
    echo "Current Login User Id : ".$usr_id;  // this is the log-in user ID :-)   
    
    $usr_name$user->get('username');   
    echo "Current Login User Name : ".$usr_name;  // this is the log-in username :-)  

    //after getting the user ID and username you can run your custom code
?>

Friday, August 5, 2011

Find string between two strings - PHP

This is a little function to find a string between two specified pieces of strings. This could be used to find any string between two strings.

function get_string($string, $start, $end){
 $string = " ".$string;
 $pos = strpos($string,$start);
 if ($pos == 0) return "";
 $pos += strlen($start);
 $len = strpos($string,$end,$pos) - $pos;
 return substr($string,$pos,$len);
}

$fullstring = "Find string between two strings in PHP";
$parsed = get_string($fullstring, "between ", " in PHP");

echo $parsed; 
(result = two strings)

Thursday, August 4, 2011

Assign an smarty variable to a PHP variable

Developer can use PHP code in smarty(.tpl file code) within a {php} block or an {include_php}.

when developer use a {php} block inside a Smarty template developer are inside the Smarty scope (object)
So when
developer need to reference a Smarty method developer would use $this
so when developer assign an smarty variable to a php variable it looks something like this in smarty:

Code:
{php}
   $t = $this->get_template_vars( 'foo' );
{/php}

print_r in Smarty development

{$foo|@print_r}

The @ tells smarty to pass the entire array to the modifier rather than calling the modifier for each element of the array. 


Smarty also print array like php print_r() function: {$foo|@debug_print_var}

Developer may also consider smarty's debug facility (which developer can enable in-template by inserting a {debug} tag).