Posted on May 3, 2010. Filed under
Coding,
Concepts,
Frameworks.
No Comments »
If you want a main header and footer file (like WordPress) with variables passed from the controller then continue reading. The content below simply displays this exact layout.
The controller file holds an array with array so on the first view file, the second array can still be passed, thus keeping organization between the header, the main page, and the footer.
Some Controller File
<?php
// Create a 2 dimensional to pass variables.
class page extends Controller {
function index() {
$data['header']['title'] = 'Title Tag';
$this->_loader("home", $data); // Home is the main view page
}
}
?>
The $header and $footer variables are based on the array that was passed initially. Remember the array keys get converted to the actual variable name when its passed onto the viewer.
Main View Page
<?php $this->load->view("header", $header); ?>
MAIN CONTENT
<?php $this->load->view("footer"); ?>
Header File
....
<title><?php echo $title; ?></title>
....
Pay attention to $data['header']['title'] and see how the data travels through both views.
Posted on September 8, 2009. Filed under
Concepts,
Media.
No Comments »
How does a web developer have any use of Twitter? Well one you have the potential of finding people with similar coding styles, two you could gain knowledge on a daily basis by following thousands of people, or three you could market yourself to either obtain a job or find business for your freelance/independent work. Knowing how to use the Twitter API also helps in increasing your work. Take a look at (It’s Always Sunny in Philadelphia)’s website owned by FX.
http://www.fxnetworks.com/shows/originals/sunny/
They completely dominated their site with Twitter. Is it a good thing? Of course it is! Think about it, user feed back increases which means more people are going back to their site for videos, pictures, and exclusive content. Because Twitter is real-time they took the advantage of the example of a real-time stock ticker and transformed it into a real-time Twitter ticker. One of many ways to make Twitter unique.
To the point…Twitter is a business companion and also an advantage for web developers. Knowing how to utilize the Twitter API and also knowing the business advantages of using Twitter for marketing purposes is a enormous plus for your resume and a great way to attract clients (for you web freelancers out there). But remember, if your going into marketing, you need to know all the aspects of social media to really be “hip” in today’s trends. Customers want to relate to something, don’t be Twitter only because there’s a large statistic somewhere stating X amount of people have no idea what the hell Twitter is. Just take advantage of Twitter in most of its many corners.
Future
Everyone that’s computer savvy knows about Twitter now so what does that mean 5, 10, or 15 years down the road. Will there be a company that will buy out Twitter or will Twitter be the centralization of all real-time social data? My blog (http://mattloinfo.com/) already is using Twitter to bridge real-time contacting to my phone. CNN is using the Twitter to communicate with its viewers and at the same time informing the public. Iran recently used Twitter to voice their opinion in a dictatorship nation. So what’s the future of Twitter? In my perspective, its going to turn into what we saw happen with the .com and internet shopping. Stock up on ideas and names because down the road, everyone will know Twitter and it will be a custom to life. Facebook is already the best site to communicate between friends and family. Because Twitter is more public, it will lag in its popularity due to the factor of openness and lack of “pretty features.” Applications already tie Twitpick, Twitter, and other applications but it’s still easier using Facebook. So how will Twitter progress to the younger non tech savvy crowd? Time. Celebrities, news, weather, and friends will make Twitter be a foundation Web 3.0 and a reality for all communicative devices. So again, if you have ideas in how to use this flexible way of communicating, be curious and look into your ideas. One day some team will become millionaires because they invented a method of using Twitter with a non price related variable to attract customers in a positive way that can be dependable. It’s like the lotto for web developers. Gear up with your knowledge of PHP and start inventing already. Tell me your thoughts on Twitter @matt_lo.
P.S. the @ (at) sign is already mainstreaming into the web as a form of meaning (Twitter contact). When we see example@yahoo.com we know its an email, eventually @example would mean its a Twitter account.
Posted on July 11, 2009. Filed under
Coding,
Concepts,
Security.
1 Comment »
In PHP, $_GET is commonly used to retrieve values passed through the URL. The purpose is simply to pass variables when other means are not the option. For example, some sites use the $_GET method to output specific pages. We’re going to take a look in how Google uses $_GET in it’s search engine.
http://www.google.com/#hl=en&q=example&aq=f&oq=&aqi=g10&fp=Xmf0jJ9P_V0
I’m going to bring down part of this URL since I have no clue (or intention) what the other stuff is. Initially the get method works by having a variable equaling to the value you want it. Starts off with a question mark, then variables equals value, and if there is more than one value to declare an ampersand is put at the end of each value. Now this sample URL from Google.com is telling that the language is in English, the search query is example, and fp=XmF0jJ9P_V0 (what ever that means). I know that en means English because of general knowledge I have in setting language types. This is used so if somebody in U.S. is looking at Google in a Korean language (hl=kr), Google will know that somebody in the U.S. (based on IP address) is viewing the page in Korean. Q means query and it always equals to what the user wants to search. Try it out by going to google.com and typing out different search queries in the URL and notice the results change to what you typed in q.
Now that you know sorta how it’s set up in the URL, lets see how we can use that data in the URL.
$language = $_GET['hl'];
$search_query = $_GET['q'];
Simple as that! If you wanted to have a site where people viewed people’s profile by ID, you could utilize the $_GET method and do…
// if url = example.com/profile.php?id=5
// select from MySql Database
$q = mysql_query("SELECT * FROM `profiles`
WHERE `userId` = '".$_GET['id']."' LIMIT 0,1");
// check if id exists
if(mysql_num_rows($q) == 1) {
// profile output
} else {
// big fail 404
}
STOP
If it didn’t catch your attention, it is very possible to hack the entire site you own with that simple statement (MySql Injection). How? Since we can control the ID variable directly from the URL…
profile.php?id='; DELETE FROM `profiles` WHERE `id` > '
which then goes into…
$q = mysql_query("SELECT * FROM `profiles`
WHERE `userId` = '';
DELETE FROM `profiles`
WHERE `id` > '0' LIMIT 0,1");
Looks like an intruder just messed up your site. Fail. Ways to make sure stuff like this doesn’t happen is quite simple.
$q = mysql_query("SELECT * FROM `profiles`
WHERE `userId` = '".clean($_GET['id'])."' LIMIT 0,1");
The clean(); function would return with an altered string going through html_entites, some str_replaces, and other methods to make sure everything is processed correctly (especially no quotes, double or single). You could also declare a variable equaling to the $_GET with the clean parenthesis around it (A future post is going to be on clean();).
Happy Coding!
Posted on July 9, 2009. Filed under
Coding,
Concepts.
No Comments »
It has been a while since I typed something up, I’ve been really busy with work and such (more busy than when I was in school…) so I’ve decided today that I’ll talk about functions and a small concept about them.
For the past 10 months I have been so object-oriented that I’ve forgot my old methods of using functions normally. $foo->bar(); vs foobar();Obviously a class is much more sophisticated and organized ever since I’ve started OOP, I’ve just inherit other classes that use the function I need or have a class full of miscellaneous functions. That is truly unnecessary…
None the less, back to the basics!
function foo() {
// code goes here...
// example....
echo 'bar';
}
// process function
foo(); // prints out "bar"
Once you initiate the function, any coding within the function will start, like a one way switch. One of the many purposes of a function is simply to perform a particular task over and over without complication in the location it needs to be repeated.
Remember you could also pass variables and use the return statement to further utilize the functions. For a more detailed definition of a function (and more ways in how to use it) go to http://www.w3schools.com/PHP/php_functions.asp
Posted on May 15, 2009. Filed under
Coding,
Concepts.
No Comments »
Loops in programming can become very handy whether it involves incremental data, repetition of data, or calling multiple bits of data (procedural method). Using these repeating statements should be part of your knowledge but just in case you forgot, I’m here to provide details.
1
2
3
4
5
6
7
8
9
10
11
| <?php
/*
Okay lets work with array manipulation
and display multiple items.
*/
$array = array('hey' => 'world', 5 => "Five", "Ten" => 10);
foreach($array as $key=>$item) {
echo '<div>'.$key.' => '.$item.'</div>';
}
?> |
Foreach is designed to handle arrays so instead of using for() statement and add an incrementing $i to the key, you can use foreach() in which has the ability to call the keys and items of the array respectively.
1
2
3
4
5
6
7
8
9
10
11
| <?php
/*
Since I've mentioned for(), i'll show a brief example
for() specifically works with 3 separate expressions.
were going to echo out 1 to 10
*/
for($i=1;$i<=10;++$i) {
echo $i.'<br />';
}
?> |
Source: http://us3.php.net/manual/en/control-structures.for.php
for (expr1; expr2; expr3)
statement
The first expression (expr1) is evaluated (executed) once unconditionally at the beginning of the loop.
In the beginning of each iteration, expr2 is evaluated. If it evaluates to TRUE, the loop continues and the nested statement(s) are executed. If it evaluates to FALSE, the execution of the loop ends.
At the end of each iteration, expr3 is evaluated (executed).
Each of the expressions can be empty or contain multiple expressions separated by commas. In expr2, all expressions separated by a comma are evaluated but the result is taken from the last part. expr2 being empty means the loop should be run indefinitely (PHP implicitly considers it as TRUE, like C). This may not be as useless as you might think, since often you’d want to end the loop using a conditional break statement instead of using the for truth expression.
In short, for() loops are very useful when dealing with multiple data manipulation in multiple dimensions.
This next loop is for the people out there who have trouble outputting multiple MySQL rows (most people google it anyways… but I like this example).
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
| <?php
// MySQL / $result = mysql_query($str) goes somewhere above
// While a row of data exists, put that row in $row as an associative array
// Note: If you're expecting just one row, no need to use a loop
// Note: If you put extract($row); inside the following loop, you'll
// then create $userid, $fullname, and $userstatus
while ($row = mysql_fetch_assoc($result)) {
echo $row["userid"];
echo $row["fullname"];
echo $row["userstatus"];
}
#--above from php.net--#
/*
Now lets work with count using a while statement
*/
$i = 0;
while($i < 10) {
echo ++$i.'<br />';
}
?> |
Use loops when you need to and don’t accidentally make an infinite loop, it will have long loading time and then stop at the 30 second timeout (php.ini default). Also remember those curly brackets!