Need help with building URLs

Getting really frustrated with determining how to build URL’s in my website. :frowning:

Here is my problem(s)…

I have a top menu bar (with dropdown sub-menus) that I want to link up to different pages in my website. Top menu items represent “categories” and so they will always link to some section landing page (e.g. Business, Sports, Politics). Sub-menu items may link to a single page (e.g. Job Listings) or to another landing page which contains a listing of things (e.g. Current News Articles).

I am looking for a stream-lined way to put all of these file paths and filenames in my HTML and make it so that it is easy to read and easy to maintain.

Below is some mock-up code which isn’t too bad, but if I give my filenames real names (e.g. “OCCoupleAccusedOfStealing$130MillionFromBanks.html”) and have some long filepath, then things could get nasty quickly?!

I could defintely use some suggestion here!


<?php
		$title=$content='';
		if (isset($_GET['article'])) {
			$articleFile = preg_replace('#[^A-z0-9_\\-]#', '', $_GET['article']).'.php';

			if(file_exists($articleFile)) {
				include(articles/$articleFile);
			}else{
				$title = 'Article Not Found';
				$content = '';
			}
		}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<head>
	<title>Dynamic Content Example</title>
	<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
	<link type="text/css" rel="stylesheet" href="css/pagelayout.css">
	<link type="text/css" rel="stylesheet" href="css/dropdown.css">
</head>

<body>
	<div id="wrapper" class="clearfix">
		<div id="inner">
			<div id="header">
				<!-- DROP-DOWN MENU -->
				<ul id="topMenu">
					<li class="current"><a href="/articles/?article=article1">Article 1</a></li>
					<li><a href="/articles/?article=article2">Article 2</a></li>
					<li><a href="/articles/?article=article3">Article 3</a></li>
					<!-- and so on... -->
				</ul><!-- End of TOPMENU -->

			</div>
			<div id="left">
				<p>
					Other content goes here : Other content goes here : Other content goes here :
				</p>
			</div>
			<div id="middle">
				<div id="content">
					<h2>MAIN CONTENT</h2>
					<p>
					<!-- Dynamically insert Article here using PHP include!! -->
					<?php echo $content; ?>
					</p>
				</div>
			</div>
			<div id="right">
				<p>
					Adverting goes here : Adverting goes here : Adverting goes here :
				</p>
			</div>
		</div>
		<div id="l"></div>
		<div id="r"></div>
	</div>
	<div id="footer">
		<p>footer</p>
	</div>
</body>

</html>

Also, when I put my articles in a sub-directory called “articles” it broke my code above?! I think the problem is that I am working in an IDE and so the path is funky, but maybe my modified code above wouldn’t evenwork if I have this on the internet?

Again, I think I need to get a better handle on how I build my URLs…

Thanks,

Debbie


<?php 
        $title = $content = ''; 
        if ( ! empty($_GET['article'])) {
          
            $articleFile = sprintf(
              'articles/%s.php',
              preg_replace('#[^a-z0-9_\\-]#i', '', $_GET['article'])
            );

            if(file_exists($articleFile)) { 
                $content = file_get_contents($articleFile); 
            }else{
                $title = 'Article Not Found'; 
            } 
        } 
?> 

I have no clue what you are trying to recommend…

Debbie

Hi Debbie,

I’m sure you do, compare the code I posted with what you posted. If there’s anything you’re unsure of, you can look up the function at the manual.

Anthony.

Can you show me a sample of one the article files? If you’re declaring the $title and $content variables in there, my example won’t work and may just confuse things.

Anthony.

All the issues your having span from the way the data is being stored. Using a database would just about eliminate this issue. Either way, if what you say is true in regards to the large collection of content than things are sure to get out of hand very quickly from a maintainability aspect. Your focusing to much on the micro issues (URL format) when what you should be looking at is the application architecture and a proper means storing the data. The conversion of a title to a “SEO friendly” URL is a very minute detail once everything else is properly in place. In most cases its best to include a unique key a stuff the URL with the SEO garbage: 3/My-Uberawesome-URL-For-SEO-Purposes – best of both worlds. You will also probably want a date in there also. Its worth anticipating the possibility of articles sharing the same title over time so: 2/12-3-2033/My-Uberawesome-URL-For-SEO-Purposes. using that method the content can easily be found and using the unique id and all the other crap there for SEO can be completely ignored from a programmatic stand-point. Oh… and no I have no problem with SEO, just the stupid, minute things people tend to focus on rather than writing good, unique content – the best SEO available. Most average users don’t even know what a URL is anyway. When your say URL they follow with Google? – yeah… Google… (shakes head).

Well, not to sound ungrateful, Anthony, but I’m posting here because I’m a newbie with PHP and could use a little more hand-holding than “go look it up in the manual if you’re unsure”! :wink:

This issue is frustrating because not only am I not a PHP guru like a lot of you, but I am uncertain how to tackle this problem which only acerbates being newer to PHP. :frowning:

The good news is that millions of websites do what I want to do.

I just need some help figuring out how to get there.

Thanks,

Debbie

Please be patient with me, because this problem is just part of a larger confusing puzzle, so it is hard to know where to start, or how one thing affects another. :frowning:

Before answering your question, some background…

I have a template that I created for my website. The plan is that the template(s) stay in place on every page, and that the content is what changes.

So, if you are reading in the Business>>Small Business sub-category, and then go to Sports>>World Cup sub-category, the website looks the same, it is just that you would see different articles.

(If you give my a little time in between responses, I can post something online so you have a better idea of what I mean.)

Back to your question…

So the code I posted currently “includes” a PHP file that has $title and $content. And when you navigate to a certain menu item, it pulls up the related article and “includes” it in to my template.


<?php
	$title = 'Ford Idles Plant to Conserve Parts';
	$content = '
		<h2>Ford Idles Plant to Conserve Parts</h2>
		<p>DETROIT -- Ford Motor Co. is planning to shut down its auto plant in Genk, Belgium, for five days starting April 4, in an effort to conserve auto parts in the wake of the earthquake in Japan, a company spokesman said.

The Genk plant had been scheduled to be idled for five days in May, but ...</p>';
?>

This is where things turn into a Matryoshka doll problem?! (I’ve got lots of issues here, and I’m trying to focus on one at a time!)

I am open to an Article being a .TXT or .HTML or .PHP (or down the road in a database).

The advantage of a .PHP is that it can store multiple things (e.g. $Title, $Author, $Date, $Content).

The disadvantage of using a .PHP include is that it can’t be easily read by others outside of PHP and my website (e.g. a content writer)

What do you think of this sub-issue?

Debbie

I am open to putting my articles in database, but I chose to include PHP/HTML files because I figured it would be easier to do in the beginning.

My goal is to have a website up in the next few days. (And this “article problem” is really the last kink I need to work out.)

Since my website might only have a few dozen articles in the beginning, I felt using a database was overkill.

If you feel differently, then maybe you can help me do things a better way?!

Your focusing to much on the micro issues (URL format) when what you should be looking at is the application architecture and a proper means storing the data.

So how should I approach this, then?

The conversion of a title to a “SEO friendly” URL is a very minute detail once everything else is properly in place. In most cases its best to include a unique key a stuff the URL with the SEO garbage: 3/My-Uberawesome-URL-For-SEO-Purposes – best of both worlds. You will also probably want a date in there also. Its worth anticipating the possibility of articles sharing the same title over time so: 2/12-3-2033/My-Uberawesome-URL-For-SEO-Purposes. using that method the content can easily be found and using the unique id and all the other crap there for SEO can be completely ignored from a programmatic stand-point. Oh… and no I have no problem with SEO, just the stupid, minute things people tend to focus on rather than writing good, unique content – the best SEO available. Most average users don’t even know what a URL is anyway. When your say URL they follow with Google? – yeah… Google… (shakes head).

Hey, I’m all about what you just said, but I have also learned the hard way that building an oceanliner when you just want to take your son fishing at the local pond is overkill!

The goal here is to “go fishing” (for customers) - not to spend the next 10 years building the world’s most technically eloquent and sexy website on the backend! :wink:

If you guys can help me out, then maybe a database is a better way to go?

Thanks,

Debbie

I don’t think there is anything really wrong with the approach your taking so long as you understand that from a maintainability and long-term aspect it isn’t going to be adequate. Hopefully when you are ready to make a transition over to a more adequate database or even XML driven storage means there won’t be be to many articles to migrate over. As the number of articles increases the migration to a new data storage mechanism is going to become increasingly painful and by painful I mean tedious. For what your trying to accomplish AnthonySterling solution seems to be adequate given the architecture. AnthonySterling’s solution just eliminates all characters besides: A-z,0-9,_,\,-. So an article URL: My Uber Awesome Article would translate to a file named: MyUberAwesomeArticle.php.

oddz, how hard would it be to do this with MySQL?

Anthony, here is a link to a test page…

Debbie’s Test Page

Can you give me some feedback on it?

Also, can you please explain the code you included above?

And what do you both think about naming an article file with the article name?

Thanks,

Debbie

I’m not going to harp on Drupal because it definitely has its down-falls. However, from what I have read of your other threads you would be able to accomplish everything and more without ever touching a line of PHP using it. In fact I’m a developer at a company that manages several newspaper sites around the US and Drupal is what we use. Of course several contributed and in-house custom modules have been created to accomplish specific business needs but for what you are doing the core D7 installation with the Views module should work well and have you up and running in no time. Just something to think about and I am by no means a Drupal “fanboy”. I much rather build things from the ground up but its not always practical.

oddz, how hard would it be to do this with MySQL?

Its not all that difficult to pull the articles out of the database. The difficult part will be creating a means to manage the articles from the admin. Now, if all you need for now is a way to way to pull articles without an admin system to create them than its not all that difficult. However, until you get an admin system in place w/ proper user registration, authentication, roles, etc you would need to enter them manually into the db. That said, all it looks like you need for now would be a single table: articles.

These: $Title, $Author, $Date, $Content

Would translate to columns: title, author, published_on_timestamp, content.

The only thing to becomes a bite of a problem is author. For now you “could” hard-code the author name. However, ideally you would want the author to be a reference to another table that contains the user/authors info via a foreign key.

Oddz,

I just updated my Test Space and included a second example that shows more of what my finished Top Menu might look like. If you can, please check it out here…

Debbie’s Test Page

As far as your Drupal suggestion…

Once I get my website up and start making some $$$ - assuming I ever do - I would be willing to go buy a book(s) on Drupal and play around with it.

But for now, that is easily another month or two out of my life that won’t help reach my business goals.

Like I said, I think I’m pretty close to getting my website done. I just could use some help getting my articles wired up! :slight_smile:

Thanks,

Debbie

Some comments…

  • I will be the only person managing this website, so that solves a lot of problems there.

  • IF this website takes off, I will probably want to add a couple of articles per week to have fresh content, but other than an initial load, it’s not like this is the NY Times where I need to add 100 new articles every day.

  • As far as data modeling, I’m familiar with it and 3NF and all that. Again, probably not really needed in the first 60-90 days. (Yes, in an ideal world I would model everything first, but doing it later won’t be a big deal based on the amount of articles I anticipate.)

  • Maybe it is still easier to just include either HTML files or PHP files? :-/

I think the place where I’m freaking out is…

1.) How should I organize Articles on the backend if they are files?

Maybe for now it is easiest to just have an “Articles” directory?

Or do I want…

Articles
Articles/News
Articles/Business
Articles/Business/Small Business

2.) Is it bad to just call files by the article title?

I guess you alluded to the fact that I’d be better with a truly unique name using an ID. (Which basically leds towards using a database, right?)

3.) How am I building the URL?

Is everything just plain-vanilla, one-level deep…

www. MyWebsite.com?article=000001.php
www. MyWebsite.com?article=000002.php
www. MyWebsite.com?article=000003.php
www. MyWebsite.com?article=000004.php

Or do I want to create a heirarchy like most news sites do?

www. MyWebsite.com?cat=business? article=EconomyStrugglesIn1stQuarter.php
www. MyWebsite.com?cat=business? article=StocksUpOnFriday.php
www. MyWebsite.com?cat=sports? article=Sweet16HoopsApproach.php
www. MyWebsite.com?cat=news? article=JapanStrugglesToContainReactors.php

Thanks,

Debbie