Posts

Showing posts with the label PHP

PHP Data Caching

Sometimes it becomes necessary to limit the number of queries to an external data source. Especially if they do not change constantly. For example, the exchange rate in the central bank. Or simply speed up the page loading, giving the script an already generated file. $expires = 3600; //Cache lifetime in seconds $curTime = time(); $cacheFile = 'data.json'; function writeCache($cacheFile) { //get fresh data file_put_contents($cacheFile, file_get_contents('http://somesite.com/api')); } if (!file_exists($cacheFile)) { writeCache($cacheFile); } else { $fMtime = filemtime($cacheFile); if (($curTime - $fMtime) > $expires) { writeCache($cacheFile); } } This caching method is based on comparing the date of the file change with the cache with the current time.

Joomla DB Queries

Consider two types of queries into the Joomla database by its own means. The code was written for CMS version 3. Perhaps in the versions below it will also work, but it was not checked. For clarity, the examples will be wrapped in a function. The first is when we need to get multiple values. function getAllCategoryesId ( ) { $db = JFactory : : getDBO ( ) ; $query = "SELECT `category_id` FROM `#__jshopping_categories` WHERE `category_publish` = 1" ; $db - > setQuery ( $query ) ; $categoriesId = $db - > loadObjectList ( ) ; $ids = array ( ) ; foreach ( $categoriesId as $catId ) { array_push ( $ids , $catId - > category_id ) ; } return $ids ; } In this case, we received a list of identifiers of published categories as an array. But there is a situation when we need to get one specific value. For this fit a slightly different design. Also we will wrap everything in a function. function getDescriptionCategory ( ) { ...

Adding fields to the ZOO category

This article is about the ZOO component for Joomla CMS . First of all consider adding a simple text field to the right side of the admin panel category ZOO . 1. Open media/zoo/applications/blog/aplication.xml and after the line < param name = " image " type = " zooimage " label = " Image " description = " Choose a category image. " /> add: < param name = " ourname " type = " text " label = " Our field name " description = " Our tip " /> 2. Output in category template media/zoo/applications/blog/templates/default/category.php <?php echo $this - > category - > getParams ( ) - > get ( 'content.ourname' ) ; ?> ourname is our unique name. You can add a picture field ZOO instead of a text field. Сhange type="text" to type="zooimage". We get src of the picture in the output. Adding fields to the main area of the ZOO category admin panel Wh...

Sending form data to email (PHP)

Let's write a simple form for sending data to email using the PHPmail() function. Our form will also write data to a file on the server. The code is not related to any CMS and can be used anywhere. Code: <?php if ( isset ( $_POST [ 'spam' ] ) && isset ( $_POST [ 'submit' ] ) && ! $_POST [ 'spam' ] ) { //departure date $date = date ( 'd-m-Y H:i' ) ; //the value from the name field is limited to 100 characters $field_name = substr ( htmlspecialchars ( trim ( $_POST [ 'name' ] ) ) , 0 , 100 ) ; //the value of the message field is limited to 1000 characters $field_message = substr ( htmlspecialchars ( trim ( $_POST [ 'message' ] ) ) , 0 , 1000 ) ; $to = "user@example.com" ; //to $subject = "Data from the feedback form" ; //text of the letter $msg = "Name: $field_name \nMessage: $field_message " ; $he...