scriptygoddess RSS Feed
 
 
 
 

Another way to manage links

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. 1
    Bryan:

    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. 2
    [ S K A I H I G H ]:
    PHP blogsystem
    It’s Monday and I have caught the second cold this month…….poor me. Anyway, I am collecting information on ping, trackbacksystems,

  3. 3
    Jennifer J.:

    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. :)

  4. 4
    Jennifer:

    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…

  5. 5
    Jennifer J.:

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

  6. 6
    Megalion:

    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.

  7. 7
    [ S K A I H I G H ]:
    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. 8
    Tom Weber:

    "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.

  9. 9
    Kat:

    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. 10
    Jennifer:

    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. 11
    Kat:

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

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

Bookmarks

WordPress Resources

Meta

Random Stuff