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

Retaining checkbox status of a form after submitting it

we will learn here how to post back or retain the value ( status ) of the checkbox. We will use php header redirection to post back the values to our main page where checkbox is present. We will call form page as first page and action or submitted page as second page. The form data will be received by second page and it is supposed to post back the form data back to form page by using query string through address bar. To keep the second page simple we will keep only the redirection part. Here we have excluded the part which takes care if the form passes its validation and execute the actual script (redirection part). Here is the code of our form with two checkboxes, pb-check.php <?php $t1=$_GET['t1']; $t2=$_GET['t2']; if($t1=="yes"){$t1v="checked";} else{$t1v=="";} if($t2=="yes"){$t2v="checked";} else{$t2v=="";} ?> HTML Form code: <form method="post" action="pb-chk.php"> <inp...

Identify the user's browser

Sometimes there arises a need to identify the user's browser. It is pretty easy to find browser with jquery. So if you have included jquery in your page, it takes a simple line to identify the browser:  if ($. browser . msie ) {} // IE if (($.browser.msie) && ($.browser.version == '6.0')) {} // IE6 if ($. browser . opera ) {} // opera if ($. browser . mozilla) {} // Firefox With the help of php, you can get the browser  details with the help of:  $_SERVER['HTTP_USER_AGENT']; Or you can even use the combined version of php and javascript. php + javascript code: <script type="text/javascript"> var string1 = "<?php echo $_SERVER['HTTP_USER_AGENT']; ?>"; // Browser string var myRegExp1 = /Firefox/; var matchPos1 = string1.search(myRegExp1); var myRegExp2 = /Chrome/; var matchPos2 = string1.search(myRegExp2); var myRegExp3 = /MSIE/; var matchPos3 = string1.search(myRegExp3); if(matchPos1 != -1) ...

Facebook connect using graph api

An example of using facebook graph api to authenticate users: Setup a new application in facebook apps and get the app id and secret. Now follow the steps: Remember to give the same url in the below code, as that of the app settings. <?php if( !isset($_GET['code'])) { ?> <a href="https://graph.facebook.com/oauth/authorize?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxxx/">Connect using Facebook</a> <?php } $code = trim($_GET['code']); if(isset($_GET['code'])) $token = file_get_contents("https://graph.facebook.com/oauth/access_token?client_id=xxxxxxxx&redirect_uri=http://xxxxxxxxxxxxxxx&client_secret=xxxxxxxxxxxx&code=$code"); $token = explode("=", $token); if(isset($_GET['code'])) $user = json_decode(file_get_contents('https://graph.facebook.com/me?access_token='.$token[1]));  print_r($user); ?>