CodeIgniter – Multiple Views

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.

PHP Redirect Index to Root

Recently I ran into a problem where I needed http://www.domain.com/index.php 301 redirected to http://www.domain.com/ and if you redirect index to ./ you will run into an infinite loop situation. Since root is index.php, 301 redirecting using header() will just call index.php again, and on reload it’ll do the same thing. Using REQUEST_URI in PHP you can determine what the exact URL instead of looking up the absolute path which will always end up with index.php. My solution is below.

if($_SERVER['REQUEST_URI'] == "/index.php") {
	header("HTTP/1.1 301 Moved Permanently");
	header("location: http://YOUR-DOMAIN-NAME.COM/"); 
}

Very easy alternative if you don’t have .htaccess (like on Windows Servers).

4 Ways to use an IP Address

1) Tracking – In addition with cookies, IP addresses can be useful for tracking individual computers or networks. Take example one of the worlds greatest tracking analysis websites—Google Analytics (aka Urchin). Using IP Based information, Google can track, look up past visits, know what pages you visited, and what pages you came from (using http refer). IPs can give the anonymous visitor a name and collect data on that name.

2) User Interactivity – Display a user online script or even develop an online chatting program based on IP availability. With Session Handling, you could have people interact with each other by classifying IPs and logged in users that match those IP Addresses. Majority of online games are based using IPs. Though Session Handling could also provide user interactivity alone..

3) Ban Hammer – The three most effective way of banning someone off your website is Email, Username, and IP Address. Email and username are only available if users have the option to register their name. IP Addresses can be recorded without the user knowing. The only backdraw to this is if you ban an IP thats networking 10 computers, all 10 computers will be banned. That is the only downside when using IP as a ban hammer (though used commonly across the web).

4) Exceptions – Want to make a private viewing site specific towards a network? IP Exception (along with Cookies) are excellent. Give people cookies, record their IP Address, show them a sneak peak of what you’re working on. Great for showing a specific client how much work has been completed (if your a web developer) or if your just showing a small crowd of people a video and not want to program a login script.

Generation Twitter 2

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.

Parsing RSS Feeds using cURL and SimpleXML

For a while I’ve been using PHP’s extension cURL and SimpleXML to obtain API XML info from sites like Twitter and Bizzare Creations. Since the format for RSS feeds and XML are the same, I don’t see why I can’t use the same method so I’ve decided to try it out.

Normally I would make a class and all but for display purposes I’m going to keep it simple. Remember, you need to have cURL and SimpleXML extension enabled in your php.ini

$rss_feed = "http://www.bungie.net/News/NewsRss.ashx"; // halo 3 news eh?
 
$ch = curl_init($rss_feed);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
$data = curl_exec($ch); // put data from rss url into variable
curl_close($ch);

All thats happening on top is simply going to the URL provided and getting the rss feed source code (aka the info).

// handle the xml data parsing
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
 
// Do some testing with the data using print_r();
// We gotta see what info we need
 
 
print_r($xml);
 
// it'll show all the data from the RSS feed but we
// only need the news so lets shrink our test
 
print_r($xml->channel);
 
// item is an array so we can put a loop to process it
// NOTE: channel and item are the variables used in the source feed
// Other RSS Feeds may not have all this, though it should be standard
foreach($xml->channel->item) {
 
$item->description; // will show each description for each item
// other options that can be included (by doing tests,
// you can find out what options are available)
// title, link, pubDate, guid, description
 
$item->link; // would give you the url to the news article
$item->title; // title of the article, obviously
 
// AGAIN: variables names may be different, use print_r($xml)
// to initially find out your variables
}

Thats it! The Step Process is as simple as 1, 2, and 3.
1) Get info via cURL
2) Parse info via SimpleXML
3) Select what you need by viewing the whole variable, and pick vars out

When the loop was occurring, you notice that I didn’t start the url using $xml. That’s because the [ITEM] tag was already converted into a SimpleXML format (all the variables were) so using $item->description is definitely valid. $xml->channel->item[0]->description will get the same result if you want to see a different perspective in whats going on.

If you want to play around with the data, instead of echoing the variables, just have other variables equal them for organization.

Full working source below

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<?php
$ch = curl_init('http://www.bungie.net/News/NewsRss.ashx');
 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 4);
 
$data = curl_exec($ch);
curl_close($ch);
 
$xml = new SimpleXmlElement($data, LIBXML_NOCDATA);
 
foreach($xml->channel->item as $item) {
	foreach($item as $key=>$details) {
		// XML is all the way down to
		// the core array, no need
		// to do $xml->... or 
		// $item->...
		// the loop did the work for us
		echo '<strong>'.ucwords($key).
		':</strong> '.$details.'<br />';
	}
	echo '<br /><br />';
}
?>