How to use Grocery crud and Tank auth together?

Step 1). Download and extract all the files from archives of both grocery crud and tank auth in Codeigniter.

Step 2) Import their schema and install them according to the instruction given on their respective sites and test whether they are working individually.

Step 3) Change address of your default route controller at routes.php to
$route['default_controller'] = "examples"; // grocery crud controller

Step 4)  In examples controller construct paste following line
$this->load->database();
$this->load->helper('url');
$this->load->library('tank_auth'); // we need to load tank auth library in example controller
$this->load->library('grocery_CRUD');
Step 5) In index function of examples controller, write code to check whether user is logged in or not:
public function index()
{
if (!$this->tank_auth->is_logged_in()) {
redirect('/auth/login/');
} else {
$data['user_id'] = $this->tank_auth->get_user_id();
$data['username'] = $this->tank_auth->get_username();
$this->_example_output((object)array('output' => '' , 'js_files' => array() , 'css_files' => array(),'data' => $data));
}
}
Step 6) Now in example views create a log out link:
Hi, ! You are logged in now.

How to use OR and AND conditions in a single query?

Today when I was coding for one of my projects, I required to use OR and AND queries together. And with having good experience in Mysql and PHP Programming, I thought I can do it easily. But when I did use the query, It was not working as expected. After analyzing my query, I got to know that it was checking AND and OR conditions separately and giving result even when AND condition is false because I forgot to use Bracket with OR conditions.

My Requirement: Select employee_id of all employers with common name 'John' , age>24 or city London

Name should be John
Age or City condition (One of them should be true)

Problematic Query: 
$sql = "Select employee_id from employers where employers.employee_name = '$name' AND employee_age > '$age' OR employee_city = '$city'";

It gives me result set with names other than John

Solution Query:
$sql = "Select employee_id from employers where employers.employee_name = '$name' AND (employee_age > '$age' OR employee_city = '$city')";

Updating all values using Single Query

Table which needs modification: TableA
Column whose value we need to change: ColA
Present Value: 2
New Value : 3

Query will be

UPDATE TableA 
SET ColA = '3' 
WHERE ColA = '2' 

How to improve page load speed of your php website?

1. Use include at the place of include_once for including files

2.Remove Blank Spaces from your php code.You can use this function:

function removeBlankLines($string)
{
return preg_replace("/(^[\r\n]*|[\r\n]+)[\s\t]*[\r\n]+/", "\n", $string);
}


3. Use , (comma) at the place of . (period) and single quote(') at the place of double quote(") wherever possible in echo statements.
For ex.
Before:
echo "my name is". $this->name."and your is".$this->your_name;
After:
echo 'my name is',$this->name,'and your is',$this->your_name;

4. Provide width and height of image tags. For eg width="10" height="10"

5. Use CDN for external Javascript files.

6. If you are using jquery or any other popular js technologies, then use google library service

7.Compress your css with css compressor.

8. Use minified version of javascript libraries, if not available then create one for yourself using sites like javascript compressor.

9.If possible write css files on page itself i.e inline, if file is too large then write css for visible content first on webpage and import css file in footer.

10. Import Javascript files in footer and not in header.

11. Load Javascript and other content Asynchronously or with the help of Ajax.

12. For image which are not above the fold, you can use lazyload javascipt to delay their loading.Read more about Lazy Load Plugin.

13. Compress your images using Yahoo Smush it

14. Use Gzip for compressing webpage.
Use this code before doctype to enable gzip compression on linux servers
 if (substr_count($_SERVER['HTTP_ACCEPT_ENCODING'], 'gzip')) ob_start("ob_gzhandler"); else ob_start();

15. Remove errors and warning from your code.

16. Use only required variables, if possible use values instead of variable.
For ex.
Before:
$j=10;
for($i=1;$i<$j;$i++) {   } // do something
After
for($i=1;$i<10 do="" font="" i="" nbsp="" something="">

17. Use microtime function of php to measure time taken by php to execute any specific function. For eg. if I want to measure time taken by first_function, I can measure it with the following code:
 
$time_start = microtime(true);
// Call your function
first_function();
$time_end = microtime(true);
$time = $time_end - $time_start;

echo "Total time to execute first function is $time seconds\n";

18. Use HTTP Caching : For PHP Website you can use header function or mod_expires

19. Avoid using SQL queries in loops(While or For), instead of that use array inside the loop and use implode function to append that array in SQL query.

20. Optimize CSS by using common declarations: 
Before
h1 { padding-top: 10px; }
p { padding-top: 10px; }
After
h1, p { padding-top: 10px; }


21. Use complex query instead of multiple queries to reduce number of requests.
Before:
Select * from products where pid=1;
Select * from text where pid=1; 
After:
Select * from products,text where products.pid = text.pid

22. You can use mysql procedure's at the place of php function. It will enhance your website security and performance.