Log in

View Full Version : Non-database 'CMS' written in PHP



Spinethetic
05-03-2008, 10:38 PM
I've been toying with the idea of a sort-of-cms website that uses a single universal newsarticle.php page that is blank if you solely open that file; but has a php script (where you want the news article to appear) that retrieves a variable from the window.location.href passed on from the previous page



<?php
######################################
# This is where the problem is, somehow retrieving the number "23"
# from the address bar so that it knows which XML file to parse.
# I know that the below function is completely wrong though in regards
# to variable retrieval, but I'am still new to PHP :P
######################################
if (!$myxml=simplexml_load_file('window.location.href')){

echo 'Error reading the XML file';

}

foreach($myxml as $article){

echo '<h3> ' . $article->title . '</h3>';
echo '<a href=','' . $article->sourcelink . '','>' . $article->sourcelink . ' | ' . $article->date . '</a><br />';
echo '' . $article->story . '';

}

?>

ie:
http://mysite.com/index.php has a link that goes to
http://mysite.com/newsarticle.php?id=23

the variable being 23 in which the aforementioned PHP coded into newsarticle.php then parses and displays, say, 23.xml, which could be coded something like this:


<?xml version="1.0" encoding="utf-8" ?>
<index>
<article>
<title>Alien from mars speaks his/her mind at UN conference</title>
<source>BBC World News</source>
<date>12/25/08</date>
<sourcelink>http://bbc.co.uk/world/5674alien.aspx</sourcelink>
<story>blah blah blah blah</story>
</article>
</index>

Likewise if the variable had been http://mysite.com/newsarticle.php?id=24, then 24.xml would have been parsed instead.

Thoughts?

Master_script_maker
05-04-2008, 02:15 PM
it seems like to many xml files, what if you had 100 stories? 100 xml files seems unreasonable.

Spinethetic
05-04-2008, 06:12 PM
PROBLEM SOLVED!

Man I'am really starting to love PHP! I don't know why I diden't switch before!



<?php
$variable = $_GET['id'];

if (!$myxml=simplexml_load_file(''.$variable.'.xml')){

echo 'Error reading the XML file';

}
### Rest of code irrelevant ###

mysite.com/view.php?id=abcdefg will load and parse abcdefg.xml, just as easily as mysite.com/view.php?id=h65h3456h56jkedj will load and parse h65h3456h56jkedj.xml, as long as the files exist anyway.

Best Regards
~Ross :)

Nile
05-04-2008, 06:42 PM
Just a tip.
You can change:


if (!$myxml=simplexml_load_file(''.$variable.'.xml')){

To this:


if (!$myxml=simplexml_load_file($variable.'.xml')){

Spinethetic
05-04-2008, 07:02 PM
Just a tip.
You can change:


if (!$myxml=simplexml_load_file(''.$variable.'.xml')){

To this:


if (!$myxml=simplexml_load_file($variable.'.xml')){


I am new to php, so I must ask, what is the functional difference? I've gotten to chapter 5 in a book that I purchased and we havent really gotten tat far, (at least I think)

Nile
05-04-2008, 07:06 PM
Its just cleaner.
When you start your code with a variable, lets call it $var, instead of:


echo ''.$var.' is the value of the variable.';

You might wanna just do this:


echo $var.' is the value of the variable.';

Then say, you wanna end with a variable, instead of doing this:

echo 'This is the value of the variable: '.$var;
But then say your using double quotes("). Then you don't even need to include the concatenation, and double quotes.
So, it'd be something like this:


echo "$var is the value of the variable";

Spinethetic
05-04-2008, 07:52 PM
Ahh, I see.

I still have a couple specific, but small problems with using this whole setup on my site. How do I return the article title itself to parse between <title></title> in the basic HTML; In the script I have ' . $article->title . ' which displays the article title from XML <title>The Last Question</title>. I really don't want a generic page title that is the same across every news article.

And I also have <subtitle>seconday title text</subtitle> that I will only be using on a few articles. There must be a way that I could go like so: <subtitle active="false"></subtitle> and the PHP will then ignore the line I have in bold so that I don't have an unnecessary line of whitespace:


<?php
$variable = $_GET['id'];

if (!$myxml=simplexml_load_file($variable.'.xml')){

echo 'Error reading the XML file';

}

foreach($myxml as $article){

echo '<h2> ' . $article->title . '<span></span></h2>';
echo '<b>' . $article->subtitle . '</b><br />';
echo '<a href=','' . $article->link . '','>' . $article->source . ' | ' . $article->publishdate . '</a><br />';
echo '' . $article->content . '';
echo '<p class=','meta','> <span class=','posted','>Posted by <a href=','mailto:admin@integralbuddha.net','>Ross Vaughn</a> on ' . $article->publishdate . '</span> <a href=','' . $article->permalink . '',' class=','permalink','>Permalink</a></p>';

}

?>

This is the link I'am testing the script on http://www.integralbuddha.net/n/v.htm?id=2

Nile
05-04-2008, 08:15 PM
I didn't read your 2nd question, but I'm guessing that it also has to do with combining HTML and PHP like the first, so all you'd do is replace the:


<title>The Last Question</title>

With this:


<?php
echo "<title>The Last Question - $article->title</title>";
?>

Spinethetic
05-04-2008, 08:22 PM
Hmm, I should've been able to figure that one out on my own, I guess I was just getting caught up in the moment while I'am brooding over this technical scripting, (at least to me =P) and dident think.

Nile
05-04-2008, 08:47 PM
Alright, glad to help you. :)