Skip to main content

Fetch data by functions

If you are a hard coder, you might write hundreds of lines of php code to fetch the data from database, to show it in the form. But it may eat up lots of time and also costs the energy of your fingers.
Why not we use the php functions to fetch the data and avoid rewriting of the code?

Suppose we want to display the information of a product based on the category it belongs to. For this, we write a query like: select * from products where category_id=1 and product_id=2.
This will fetch the details about a product with id=2(say, Reynolds) and category with id=1(say, Pens). Then we can fetch the Reynolds product data and display it as needed.

But suppose you need to fetch the details of the Reynolds product often and display it. Its not easy for a common developer to write queries now and then to fetch 1 or 2 columns of data (say, we just need the price and color).

Then we can use custom php functions to get the work done. For ex, i frequently need to fetch the price of a product, and display it beside a product name, that is chosen random, or based on a condition such as the latest product. Then i write a php function like: getPrice(id), and include it in the file includes/functions.php.

This function will take the id of the product and queries the database to fetch the price of it and returns it. Thus our effort to write the query, fetch the result set and the required column (price) is now reduced to just calling the getPrice(id) function.

Similarly, we can write many functions to fetch the desired data and use it as often, without manually writing the queries everytime.

Comments

Popular posts from this blog

Joomla validate chrono forms using jquery

It is a common practice to use Chrono forms in our joomla site to setup various forms in the site, be it a contact us form, submit a ticket form, or whatever. I have set the option "validate form" to "No" under chronoform settings in admin panel, and also preferred to not include any js or css files. For validating the chrono forms, i prefer jquery. So first lets add jquery support in our joomla. It is quite simple: 1. Download jquery.js and jquery_min.js (1.4.2 version is enough) and place them in media/system/js folder of your joomla. 2. Edit libraries->joomla->html->html->behavior.php, and add the following function below the mootools() function: function jQuery($debug = null)     {         static $loaded;         global $mainframe;         // Only load once         if ($loaded) {    ...

joomla remove category name in url

-) If anyone is using the lyftenbloggie component, and want to remove the component name and category title from the sef url, follow this hack: (Note: You also need to install the lyftenbloggie extension for ARTIO SEF) Under components/com_sef/joomsef.php, After this code: $location = array();         foreach ($title as $titlePart) {             if (strlen($titlePart) == 0) continue;             $location[] = JoomSEF::_titleToLocation($titlePart);         } Just add this code: if($location[0] == 'lyftenbloggie')         {         $temp_sef = end($location);         $location = array();            $location[] = $temp_sef;         ...

php get content between tags

This involves parsing the dom document. <?php function getTextBetweenTags($tag, $html) {     $dom = new domDocument;     @$dom->loadHTML($html);         $dom->preserveWhiteSpace = false;     $content = $dom->getElementsByTagname($tag);     $out = array();         foreach ($content as $item)     {         $out[] = $item->nodeValue;     }     return $out; } $xhtml = '<tag>abc def</tag><tag>123 456</tag>'; $content2 = getTextBetweenTags('tag', $xhtml); foreach( $content2 as $item ) {     echo $item.'<br />'; } ?> Output: abc def 123 456