Skip to content

Instantly share code, notes, and snippets.

View abdullahbutt's full-sized avatar

Abdullah Butt abdullahbutt

  • Petersberg, Deutschland
View GitHub Profile
@abdullahbutt
abdullahbutt / CI_Active_Record_Create_&_Update_Queries
Created November 18, 2013 09:16
CI_Active_Record_Create_&_Update_Queries
Create and Update Queries
Active Record has three functions that help you create new entries in your database. They are $this->db->insert(), $this->db->update(), and $this->db->set().
The difference between a 'create' and an 'update' query is that when you create a new record, there is no reference to any existing record, you are writing a new one. When you update, there is an existing record, and you are changing it. So in the second case, you have to specify which record you are changing. In both cases, you have to set the values you want to leave in the database after your query. Values you don't set will be left unaltered—or, if they didn't exist before, they will still be 'null' after your query.
CI allows you to set your values either with an array, or with $this->db-set(); the difference is only one of syntax.
So, let's add a line to our sites table in the websites database. We've already connected to this database in our controller. The controller's constructor function included the line:
$this->load-
@abdullahbutt
abdullahbutt / CI_Active_Record_Delete Queries
Created November 18, 2013 09:16
CI_Active_Record_Delete Queries
Delete Queries
Delete queries are perhaps the simplest to describe. All you need is the name of the table and the ID number of the record to delete. Let's say I want to delete record in my sites table with the ID number 2:
$this->db->where('id', '2');
$this->db->delete('sites');
I get slightly nervous around 'delete' queries because they are so powerful. Please remember to make sure that there is a valid value in the 'where' clause, or you may delete your whole table! Neither the author nor Packt Publishing will accept any liability if….
@abdullahbutt
abdullahbutt / CI_Mixing_Active_Record_&_Classic_Styles
Created November 18, 2013 09:18
CI_Mixing_Active_Record_&_Classic_Styles
Mixing Active Record and 'Classic' Styles
CI doesn't insist that you use Active Record. You can also use CI to issue straight SQL queries. For instance, assuming you loaded the database in your constructor, you can still write queries like this:
$this->db->query("SELECT id, name, url FROM sites WHERE 'type' = 'dynamic'");
Personally, I find Active Record easier to use. Conceptually, setting out my query in an array makes it easier to see and manipulate as an entity than writing it in SQL syntax. It's slightly more verbose, but clearly structured; it automatically escapes data; and it may be more portable. It also minimizes typing errors with commas
and quotes.
There are a few cases, however, where you may have to resort to the original SQL. You might want to do complex joins, or another example is if you need to use multiple 'where' conditions. If you want to find the websites associated with client 3, but only those of two specific types, you may need to put brackets around the SQL to make sure the query is
@abdullahbutt
abdullahbutt / CI_Active_Record_Class_Summary
Created November 18, 2013 09:19
CI_Active_Record_Class_Summary
Summary
We've looked at CI's Active Record class and seen how easy it is to:
1) Set up connections to one or more databases
2) Do standard SQL read, update, create, and delete queries
3) Perform other functions that we need, to use a database properly
CI's Active Record function is clean and easy to use, and makes coding much clearer to read. It automates database connections, allowing you to abstract the connection information to one config file.
It can do pretty well anything that you can do with 'classic' SQL—more than I have space to explain here. See the online User Guide for fuller details.
@abdullahbutt
abdullahbutt / websites
Created November 18, 2013 09:20
ci mysql query to set up database of a website
DROP TABLE IF EXISTS `ci_sessions`;
CREATE TABLE IF NOT EXISTS `ci_sessions` (
`session_id` varchar(40) NOT NULL default '0',
`peopleid` int(11) NOT NULL,
`ip_address` varchar(16) NOT NULL default '0',
`user_agent` varchar(50) NOT NULL,
`last_activity` int(10) unsigned NOT NULL default '0',
`left` int(11) NOT NULL,
`name` varchar(25) NOT NULL,
`status` tinyint(4) NOT NULL default '0'
@abdullahbutt
abdullahbutt / mysql_connect_function
Created November 19, 2013 07:40
mysql_connect_function
<?php
// USING the OLD MySQL_connect API is an Anti-Pattern .... for reference ONLY! (Anti-Pattern means Bad-Practice)
//echo phpinfo();
/* -------------------METHOD 1
$conn=mysql_connect('localhost','root','') or die ('Could NOT Connect');
mysql_select_db('practice',$conn);
@abdullahbutt
abdullahbutt / em_dl_dt_dd_tags
Created November 22, 2013 11:16
<em> <dl> <dt> <dd> & "Alt + F3" in sublime text to edit multiple entries
<!DOCTYPE html>
<html>
<head>
<title>Test :)</title>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
</head>
<body>
<!-- <em> </em> tag is used for 'emphasis' .... i-e turns italic !-->
<!-- Press "Alt + F3" to select edit same tag in sublime text !-->
@abdullahbutt
abdullahbutt / span_html
Last active December 29, 2015 02:09
HTML <span> Tag
<!DOCTYPE html>
<html>
<body>
<p>My mother has <span style="color:blue;font-weight:bold">blue</span> eyes and my father has <span style="color:darkolivegreen;font-weight:bold">dark green</span> eyes.</p>
</body>
</html>
<!--
Definition and Usage
@abdullahbutt
abdullahbutt / blockquote_html
Last active December 29, 2015 02:18
HTML <blockquote> Tag
<!DOCTYPE html>
<html>
<body>
<h1>About WWF</h1>
<p>Here is a quote from WWF's website:</p>
<blockquote cite="http://www.worldwildlife.org/who/index.html">
For 50 years, WWF has been protecting the future of nature. The world’s leading conservation organization, WWF works in 100 countries and is supported by 1.2 million members in the United States and close to 5 million globally.
</blockquote>
@abdullahbutt
abdullahbutt / doctype_html
Created November 22, 2013 12:50
HTML <!DOCTYPE> Declaration
<!DOCTYPE html>
<html>
<head>
<title>Title of the document</title>
</head>
<body>
The content of the document......
</body>