Archive

Archive for June, 2009

Understanding Arrays and How to Use Them

June 24th, 2009 admin 1 comment

So in a recent project I have just finished up (again, will let you guys know what it is when it’s officialy released), arrays had become my best friend along with some regex, but that’s a topic for another post. I needed to turn strings into arrays, which I would then put back into arrays, and finally back into strings. I did this in a few different ways, but let’s go into some examples of why one would need to do this, then we’ll begin the how; seeing as the how without the why makes very little sense.

Well, I mainly use all this to transfer large amounts of separated data from function to function. In my most recent endeavor it was to split a specific type of file, parse it through 3 different functions (each function returned an array, but dealt with the data in strings which I will explain) then finally took all the information in the provided array and successfully dumped the data to a database. This is only one of the numerous reasons a person could/would use an array, but they help like one couldn’t imagine.

Let’s begin with the basics. For sake of example, we’ll keep things in PHP. The first example below just explains an array. No more, no less.

<?php
// Ways to add data to arrays!
$array1 = array("Entry 0","Entry 1","Entry 2","Entry 3"); // Just put data in
$array2 = array(
0 => "Entry 0",
2 => "Entry 2",
1 => "Entry 1"
); // Define arrays by opening and stating index.
$array3[0] = "Entry 0";
$array3[1] = "Entry 1"; // Enter directly into index

?>

Another way to build an array, more or less indirectly, is to use php’s “explode();” function. This will take text with a defined delimiter and separate each set of data (at the delimiter) into its own index. For example, consider the following.

<?php
$string = "This is a string where we will, use commas, for delimiting the text and to, split this into an, array!";
$array = explode(",",$string);
print "Original: ".$string."<br /><pre>Array: ";
print_r($array);
print "</pre>";
?>

To call these defined arrays we would use $array[INDEX]. Now, there are many ways to access this data and filter it. Let’s begin by calling one value from an array.

<?php
$array = ("Entry 0","Use this text","Entry 2","Entry 3","This text isn't being called either");
print $array[1];
?>

The script above with print “Use this text” because it is in the array at the index value 1. It is important to remember that indexes start at 0 unless defined otherwise. Now, we’ll explain some more intricate ways of getting all your values. The first way is iteration using the “for();” loop. Here goes:

<?php
$array = ("Test","Test2","Word3","Keep 'em coming!","white space next :P ","","No need for that","We <3 trim");

// Iterate.
for($i=0;$i<count($array);$i++){
print $i.") ".$array[$i]."<br />";
}
?>

Now this will just result similarly to the foreach(); which I am about to explain. However, sometimes iteration is more effective than foreach(); because it gives you more liberty. Most of the time I would recommend iteration for multi-dimensional arrays (arrays within arrays and so on), but usually two different ones. For example:

<?php
$array = array(array("Array in index","number 0"),array("Array in index","number 2"));

for($i=0;$i<count($array);$i++){
// Now, let's get our multidimensional values! :D
for($ii=0;$ii<count($array);$ii++){
print "[".$i."][".$ii."]: ".$array[$i][$ii]."<br />";
}
}
?>

Now, that will grab all the data within the arrays within the arrays. Wow, sounds confusing, but once you get a better grasp, it’s really quite simple, but I cannot lie; it all seems very daunting at first. Now we’ll explain the foreach(); function. This function basically does what we just did manually, but sometimes is easier and more effective. A lot of times, you will use the two in conjunction (even if for different purposes), so become friendly with both of them!

<?php
$array = array(
0 => "Entry 0",
1 => "Another entry",
2 => "Again...",
3 => "And finally :P "
);

foreach($array as $key => $value){
print "Key/Index: ".$key.": ".$value."<br />";
}
?>

From the code above, you can see that the foreach(); function has accomplished the same feat we did earlier with iteration. On a simple array such as this, it would be best to use foreach, but like I said: it may be necessary at times to use iteration.

As I mentioned earlier, both can be used in conjunction. Just a quick example (we won’t go through the actual coding of it) is if you have a multi-dimensional array where you feel the need to use foreach, you can use foreach($var[$i] as $key => $val){} and work it that way.

So all in all, everything boils down to: love iteration, love foreach(); on the programming languages it’s available, and above all LOVE arrays! They are right next to regex in the developer’s “most useful tools” section of his or her tool belt!

Regards,
Dennis M.

Categories: Other, PHP Tags: , , , ,

Busy With Working… Taking More!

June 19th, 2009 admin 2 comments

Hey everyone,

As you can see, it has been a long time no post XD. It’s simply because I’ve been very busy with work and projects you guys have been giving me. Within the next week I should be done again and ready for you guys to continue contacting me for more projects. Also, in between projects I will be posting some more blog posts to help everyone. With every project I get I gain greater ideas on what you guys would like to learn.

As for next time, I have a big post planned. I’ve been working on a fairly large project for almost 2 weeks and am finally finishing up. I won’t say much about it until it’s released (and then I will make a whole post about it :) ), but it’s a great script and definitely will be worth your while to take a look at.

Thanks for checking!

Regards,
Dennis M.

Categories: News Tags: , , , , , ,

Importance of a Portfolio

June 10th, 2009 admin 1 comment

Portfolio. The word sometimes lingers in one’s mind, but usually is associated with artwork, or a collection of works from your high school English class. But really, an online portfolio (whether it’s only artwork/graphics or programming) is very important when trying to find interested clients.

Generally, most people can only establish their names by doing quality work at a great price (or in a few cases, great work and advertising at a high price pays off too – but those are the few giants of the web today). Word of mouth is good, but when you have a website, you should be showing off more than that to your potential clients. When a client visits your site (no matter which way – browsing, by accident, referal, etc.) their first opinion is your site itself. From there, they are looking for a giant link or button that says “Portfolio.”

When the user goes to this link, they don’t want to see just links, but they want a thumbnail shot of what to expect, a link to a working example, and a description of the purpose of the work. Now I know I do not have all of those things right now (just yet, anyway), but they are well worth it. Since most users see other works I’ve done (and after speaking to me on MSN) they are always satisfied with what I have to offer, but I am working on my designing skills and a portfolio script which I shall be launching very soon so I can practice what I preach.

So now that everyone understands that a portfolio should be more than just a collection of works you done, everyone can start really presenting themselves effectively. Like I said, soon I will be finishing my portfolio script and perhaps releasing it to the public, so if you are unsure about how to get yours started, that may be a great starting point for you!

Regards,
Dennis M.

PHP & Pagination

June 4th, 2009 admin No comments

Very often I see people asking questions about this on forums such as DP and NP. I have probably posted my code snippet on how to efficiently create a simple pagination document more times than I can count (which whether you believe it or not is pretty high ;) ). So I decided I’d go through it here.

First off, we’ll explain what pagination is. You may be reading this article not even certain if pagination is what you need. However, if you have multiple MySQL results you wish to organize and display “x” results per page, you’re on looking at the right thing. It is as simple as that. Pagination is really just web jargin used to shorten up “return results only displaying ‘x’ per page.”

So, I’m going to post the snippet and I’ll go into it a little bit, but I do believe that the comments explain pretty thoroughly. First the database structure we’re going to use ;)

CREATE TABLE `pagination` (
`id` BIGINT( 10 ) NOT NULL ,
`name` VARCHAR( 32 ) NOT NULL ,
`content` TEXT NOT NULL ,
PRIMARY KEY ( `id` )
) ENGINE = MYISAM ;

And now.. The moment we’ve been waiting for… The actual pagination script! :)

<?php
/**
* Pagination Script by Dennis M.
* (C) 2009 Dennis M. All Rights Reserved.
*
* Primary purpose of this script is to teach
* people the art and concept of pagination
* and how to effectively create/use it.
* Please give credit to Dennis M. if you
* take anything directly from this script.
*
*/

// Connect to DB
mysql_connect("localhost","USER","PASS");
mysql_select_db("USER_DATABASE");

// Define results per page
$perPage = 1;

// Define page number in URL
$num = $_GET['no'];

// Define link starting point.
$linkno = 1;

// If no number given, default it.
if(!isset($num) OR !is_numeric($num)){
$num = 1;
}

// Multiply to see where we're at to show results :)
$currentLimit = $perPage*($num-1);

// Get how many pages we'll have total (for links)
$numPages = mysql_num_rows(mysql_query("SELECT * FROM pagination"))/$perPage;

$query = mysql_query("SELECT * FROM pagination ORDER BY id ASC LIMIT ".mysql_escape_string($currentLimit).",".mysql_escape_string($perPage).";");

// Fetch all the info
while($row = mysql_fetch_array($query)){
$title = stripslashes($row['name']);
$article = stripslashes($row['content']);
?>
<html>
<head>
<title>Pagination Test</title>
</head>
<body>
<h1><?php echo($title); ?></h1><br />
<p><?php echo($article); ?></p><br /><hr>
<?php
}
print "<p align=\"right\">";
if($perPage <= 2){
while($links < round($numPages)){
?>

<?php
$links++;
$linkno++;
}
} else {
while($links <= round($numPages)){
?>

<?php
$links++;
$linkno++;
}
}
?></p>
</body>
</html>

Now that’s all there really is to it. Basically how it works is it updates the SQL query depending on the page number (received from $_GET['no']). If none is defined, it reverts to the default and all goes accordingly!

Pagination Tutorial

The download link is above as usual! :) Enjoy

Regards,
Dennis M.

PHP vs Ruby on Rails (RoR)

June 2nd, 2009 admin 4 comments

Well people have been asking me lately about this Ruby on Rails (RoR). I often receive questions such as “What is RoR?” or “Is it better than PHP?”. Well, Ruby on Rails is another web programming language, but is not widely compatible across all web servers, but is increasingly becoming so. Personally, I am more of an advocate to PHP, but we’ll try to get the facts straightened out.

To begin, I’m going to assume everyone knows the basics of PHP. It’s fast, efficient, integrated with the webserver, etc. But for those of you completely unfamiliar with Ruby on Rails it is made by the “Ruby Core Team” (of the Ruby Programming Language). The format of the language is similar to Python if any are familiar with that. Ruby generally runs on its own server, separate from the general webserver-to my experience anyway.

So now that we have the basics down about each let’s go over the weak points in each and strong points. We will begin with the weak points to Ruby on Rails. Since it runs on it’s own server, it makes it more difficult to readily integrate with a site: obviously. Also, for those of us who prefer the nice C/C++ style programming, you can forget it with RoR. You will definitely end up using “end” much more than you would care to admit with this language. Compatibility across servers is more or less low. Most servers do not provide the RoR service, so that is also a downfall of RoR.

Likewise, PHP also has its faults. Since the language is so widely used and installed on practically every serer on the web, it allows for more holes to be more easily and quickly found. This means more frequent updates (which could be good if it does not become a nuisance) and since it is integrated with the web server, that also means long patch times. Depending on how you have setup PHP, you may even have to recompile the webserver. Normally it doesn’t cause a great problem for most servers because the old version doesn’t remove until the new version is finished compiling, but it greatly increases server load. For webhosts, this could cause a problem because this means that they have to go down for more server maintenance time. Also, unfortunately for PHP, every time one wants to add a new feature (example add ziplink support or something) they would have to recompile PHP.

Although both have their faults, both also have very strong points. Again, we’ll begin with RoR. RoR, again, since it’s not integrated means little downtime if it needs to be updated. Also, it is similar to Perl in the respect that it can download modules quickly and easily. Ruby, growing more popular, also is easier for users who are entering programming. It definitely scores an A+ on the entry level programming language level and pretty powerful with simple code.

PHP, on the otherhand, though a little more complex has its key aspects. There is mass compatibility with PHP. As mentioned earlier, pretty much every server on the web is PHP enabled. It has become the web standard over the years. Another benefit, since most open source software is programmed in PHP, is one who knows it can make their own program modifications and customizations to most open source programs. Above all, the support over the web is great because users all over the web have a deep understanding and knowledge of the language and are willing to offer free help on forums all over the web.

So all in all, both languages have their ups and downs. Ultimately, it is your choice to decide which you would rather learn/install on your server. Peronsally, I would go with PHP simply because it’s more available and there is a greater user base to help you. If you are looking for custom projects to be made, it may also be much easier to find a programmer to do work for you if you’re looking for PHP. But if you have never looked at programming before and cannot seem to grasp the concepts of PHP, Ruby may be a good alternative to start learning how programming should look and feel.

Regards,
Dennis M.

Categories: Other, PHP, Software Reviews Tags: , , , ,