A Simple Implementation of Arrays With Php.

I have actually been starting to use arrays a lot lately with some of the projects I have been working on.

The most recent application for an array in one of my projects is as follows:

I am designing a template system for one of the projects I am working on and I needed a simple way to output some rows from a db without all the clutter of a db connection, query, and while loop.

The cool thing about this is that I can call it as many times as I want because I create a function for it.

The Problem: Reduce db query/connection clutter by calling a function that in turn will populate an array with the desired information from the db.

Here’s all the crap you would want to put somewhere else, possibly in another file like functions.php:

name;

		// define the url of our link from the current row
		$url = $row->url;

		// create a new element in our array
		$links[$name] = $url;
	}

	return $links;
}

?>

Next we get to call the function and create our links!

 $url) {
	echo "" . $name . "";
}

?>

Notice how much less crap we need. This method is very useful if you plan on showing information multiple times on a specific page because all you have to do is loop through them again!

Hope this helps!

Regards,
Nick

This entry was posted in Tutorials. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>