scriptygoddess

11 May, 2003

Another way to manage links

Posted by: Jennifer In: Script snippet

Back when I first changed the format of my page so that I had the same list of links down the side of every page, I quickly discovered that it was a pain to have to go through 5 or 6 pages of HTML/PHP adding new links here and there.

I eventually came up with a system that makes it much easier to add/remove links from all my pages in a single go. On every page that has a list of links, I put this at the top:

include "path/to/linkage.inc";

The file linkage.inc contains a series of multidimensional arrays, like so:

$blogsLinkage = array(
0 => array("http://www.snazzykat.com/", "snazzykat"),
1 => array("http://www.scriptygoddess.com/", "Scriptygoddess"),
2 => array("http://www.technoerotica.net/", "technoerotica"),
4 => array("http://www.neuroticfishbowl.com/", "neurotic fishbowl"),
5 => array("http://www.tampatantrum.com/", "aint too proud to blog"),
7 => array("http://troll54.blogspot.com/", "Troll54"),
8 => array("http://timatollah.blogspot.com/", "Timatollah"),
9 => array("http://www.greasypants.org/", "greasypants"),
10 => array("http://www.randomramblings.com/", "Random Ramblings"),
);

$techLinkage = array(
0 => array("http://www.thinkgeek.com/", "ThinkGeek"),
1 => array("http://www.phpbuilder.com/", "PHP Builder"),
2 => array("http://www.scriptygoddess.com/", "scriptygoddess"),
3 => array("http://www.dynamicdrive.com/", "Dynamic Drive"),
);

Then, in each file where the links are to be listed, it's a simple matter of a nice little loop to render the code:

<?php
foreach ($blogsLinkage as $val) {
print "<a href=\"$val[0]\">$val[1]</a><br />";
}
?>
[more HTML code…]
<?php
foreach ($techLinkage as $val) {
print "<a href=\"$val[0]\">$val[1]</a><br />";
}
?>

Then, whenever I need to add a new link, I simply put a new entry into linkage.inc. If I need to remove a link, simply take it out of linkage.inc. This way, all the pages are updated automatically, and it's much easier!

Guest authored by:
Will – silverfisch.net

12 Responses to "Another way to manage links"

1 | Bryan

May 12th, 2003 at 8:23 am

Avatar

Unless you specify otherwise, .inc files are rendered in the browser as clear text. Therefore, if you don't want people to see the code (if they know or guess the path) then change the extension to .php (or .asp if you're in that camp).

2 | Jennifer J.

May 12th, 2003 at 10:48 pm

Avatar

Pardon me, here. I'm wanting to learn, so I'm questioning everything I see. :)

I have always used an include file containing just the raw HTML for the links list.

What advantages are there to using the method shown here?

The only thing I can see is it would be easy to have one rather large file, with a number of arrays defined, and just use the include code to call out one array, or part of one array, depending on the page. That would be very useful: a long blogroll, for example, could be displayed just a few at a time. But if a list was long, and I always displayed the entire thing, then wouldn't there technically be more server load involved in looping through a long array, as opposed to just including a file?

Is there another advantage or use for this that I'm not considering? That's the downside to seeing interesting code samples like this: wrapping my mind around uses for them. :)

3 | Jennifer

May 14th, 2003 at 6:29 am

Avatar

A few advantages:

1) If you ever want to change the style you're printing out the links (ie. lets say you want to define a "class" in your a href tag) it's only one line to change for all the links

2) The links are easier to see (rather than surrounded by a bunch of code) – so adding/deleting is easier

Unless you have a CRAZY number of links – the loop won't bee too much of a drain on the server…

4 | Jennifer J.

May 14th, 2003 at 6:37 am

Avatar

Jennifer, that makes perfect sense! :) I'm going to have to do this with my links now!

5 | Megalion

May 15th, 2003 at 4:49 am

Avatar

Thanks to Jennifer J for asking the question and to Jennifer for clarifying… I had the same question cause my links are already in separate template modules in MT and while I don't have that many, I was interested in the reasoning in case it becomes an issue.

6 | Tom Weber

January 15th, 2004 at 3:52 pm

Avatar

"Script snippet: Another way to manage links"

I like the idea of this code. How could you incorporate that same concept to swap out descriptions from say a car to a truck which has additional descriptive items you could pull from a db.

7 | [ S K A I H I G H ]

May 12th, 2003 at 9:59 am

Avatar

PHP blogsystem
It’s Monday and I have caught the second cold this month…….poor me. Anyway, I am collecting information on ping, trackbacksystems,

8 | [ S K A I H I G H ]

May 15th, 2003 at 10:57 am

Avatar

PHP blogsystem
It’s Monday and I have caught the second cold this month…….poor me. Anyway, I am collecting information on ping, trackbacksystems,

9 | Kat

July 29th, 2004 at 10:49 am

Avatar

I'm brand new to PHP, and thus far all of the scripts I've found here have been fairly easy for me to customize and learn. This one, though, stumps me.

Is there a way to show a single link from this array? The idea is to have a static link page that, on one page, shows the entire array, and then on another page, shows only the first link. I've looked at alot of the PHP sites suggested, but they don't seem to have what it is I'm looking for. I'd appreciate any help anyone could give me. Thanks!

10 | Jennifer

July 29th, 2004 at 9:22 pm

Avatar

Kat – I'm not using this script – so I have no way to test it – but what the script is doing is making a "two-dimensional" array. So to display a single link I think you would do this:
<?php
print "<a href="$blogsLinkage[0][0]">$blogsLinkage[0][1]</a><br />";
?>
The first bracket says which person's site/name we're going to display, the second bracket will display which part we want to display. (ie URL, vs. name)

Just a quick google on two-dimensional arrays and I found this page which looks good if you want to learn more about them…

11 | Kat

July 29th, 2004 at 11:10 pm

Avatar

Thank you very much Jennifer! I don't know why I didn't see that page before.Thanks again.

12 | javakittie

November 23rd, 2004 at 12:52 am

Avatar

Ye gods..
What have I gotten myself into? I.. don't even know where to begin with this one script. Why is it…

Featured Sponsors

Genesis Framework for WordPress

Advertise Here


  • Scott: Just moved changed the site URL as WP's installed in a subfolder. Cookie clearance worked for me. Thanks!
  • Stephen Lareau: Hi great blog thanks. Just thought I would add that it helps to put target = like this:1-800-555-1212 and
  • Cord Blomquist: Jennifer, you may want to check out tp2wp.com, a new service my company just launched that converts TypePad and Movable Type export files into WordPre

About


Advertisements