php - adding a news article auto creates page for it

Joined
12 Feb 2006
Posts
17,631
Location
Surrey
so i got the news thing all working brilliantly, really impressed with it, the only thing i don't like about it is that the articles don't get their own page as the way i view them is to show articles with a specific category which means that i get all the articles and not one by one.

At the moment the tables for the articles in the database are, id, title, author, category, post and date.

Im thinking of something along the lines of when i submit php will copy a page and then rename it to the id as its unique, and then have the page do some php (this is where i have no clue on) and have it check the file name and get the id number from that, then do a mysql query and select the article with id="filename"

Is this possible and how would i go about doing it?

thanks
 
i think it's more to do with the query you use to fetch the articles? at the moment you must be selecting a whole category? you just need a new page/query that selects only by id? :)

you could call the page like this....
article.php?id=3

Code:
$id = intval($_GET['id']);
if($id < 1) {
    header("location: index.php") //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'")
if(mysql_num_rows($result) != 1) {
    header("location: index.php") //article id supplied doesn't exist in db
} else {
    //display article
}
....
 
First of all I need to understand what you are trying to do.

Tell me if this is correct:

What you have:
A page with all the articles on.

What you want:
Separate pages for each article?

It gets confusing when you start talking about filenames, are you looking to cache the pages or simply when you go to article.php?id=1 it will get the article with the ID = 1 from the database and display it on the page.

EDIT:

The last bit is referring to what is said above
 

I tried your code and made the changes but it didn't seem to work, it didn't like the } after the header part.

Conrad11 said:
What you have:
A page with all the articles on.

What you want:
Separate pages for each article?

It gets confusing when you start talking about filenames, are you looking to cache the pages or simply when you go to article.php?id=1 it will get the article with the ID = 1 from the database and display it on the page.

yeah sorry im finding it hard to put into words. What you think is correct, that at the moment i have pages that query the database for all articles that have a specific category and i want seperate pages for each article automatically made when the article is submitted.

So im thinking so say on the category page i selects all the articles with categroy, "house" and display the title which is a link to the article and the address is based on the id

for instance
Code:
include ('../services/mysql_connect.php');
$query = "SELECT * FROM table WHERE category = 'house' ORDER BY date DESC";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

echo " <h3><a href="$row[id].php">$row[title]</h3><br/ >";

then on the page that is linked to it will check what the name of the file is which will be the id of the article, and then display whatever article is with that id:
Code:
include ('../services/mysql_connect.php');
$query = "SELECT * FROM table WHERE id= 'name of file???'";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {
<h3>$row[title]</h3><br />

<i>Posted on</i> $row[sd] <i>by <a ../login/userinfo.php?user=$row[author]\">$row[author]</a></i>

<br /><br />

$row[post])

<br /><br />

<i>Categories Posted in:</i> <a href=\"blog-view-$row[category].php\">$row[category]</a>

<br /><br />

hope that made some sense and not more confusion of what i need help with? the part the im lost with is getting the name of the file where is say "name of file???".

thanks again
 
Last edited:
marc2003 said:
Code:
$id = intval($_GET['id']);
if($id < 1) {
    header("location: index.php") //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'")
if(mysql_num_rows($result) != 1) {
    header("location: index.php") //article id supplied doesn't exist in db
} else {
    //display article
}
....

You forget many ;'s :eek:

Code:
$id = intval($_GET['id']);
if($id < 1) {
    header("location: index.php"); //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'");
if(mysql_num_rows($result) != 1); {
    header("location: index.php") //article id supplied doesn't exist in db
} else {
    //display article
}
....

fixed ^ :)
 

still not working for me, telling me that exact same problem as the previous code.

ok i have tried rewording again what i want just incase its not clear.

So when i create an article, php will find a file, copy and then rename it to the id number, so for instance first article i add will create a file called 1.php.

1.php will then check what's its name, and then search for any article that has an id with the same name as the 1.php and display it.
 
Why are you messing with creating new files?

Just make a 'show_article.php' or some such named file, pass it the news item id and then display the single article using that file every time?
 
sirlemonhead said:
Why are you messing with creating new files?

Just make a 'show_article.php' or some such named file, pass it the news item id and then display the single article using that file every time?

pass it the news item? you care to explain? i understand what your saying but no clue how to which is what this thread is for iirc.
 
Use a GET variable to determine the article id, then pass it to your SQL SELECT statement; to simplify - a page wrapper which loads articles depending on the query string e.g. page.php?id=3 would load database article with id of 3.
 
Mammalian said:
still not working for me, telling me that exact same problem as the previous code.

ok i have tried rewording again what i want just incase its not clear.

So when i create an article, php will find a file, copy and then rename it to the id number, so for instance first article i add will create a file called 1.php.

1.php will then check what's its name, and then search for any article that has an id with the same name as the 1.php and display it.

They are basically telling you what to do already to do this.

You need to create a file which does this.

For instance, create a file called cache.php and have the code something to the similar:

Code:
<?

$id = intval($_GET['id']);
if($id < 1) {
    header("location: index.php"); //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'");
if(mysql_num_rows($result) != 1); {
    header("location: index.php") //article id supplied doesn't exist in db
} else {
    //here you would create the html page in a the string $html_text.

    //This code is saving the article as a static page
    $file = fopen("articles/".$id.".html", 'w') or die("can't open file");
    fwrite($file, $html_text);
    fclose($file);
}

?>


Then if you went to the page: cache.php?id=1. It will create a page called 1.html for the article with the ID = 1.
 

hmm ok i see that would be much easier for update purposes but the code provided is still not working and i have no clue where i'd put the part to echo out; guessing after the following?:
//here you would create the html page in a the string $html_text.
 
Lol...well you do actually have to put the code for creating the $html_txt string as that is project specific (different for every project), assuming you haven't already done that?

If you have then post what error message you are getting?
 
Conrad11 said:
Lol...well you do actually have to put the code for creating the $html_txt string as that is project specific (different for every project), assuming you haven't already done that?

If you have then post what error message you are getting?

Parse error: syntax error, unexpected '}' in /home/mammal00/public_html/admammal/blog/cache.php on line 10

thats the error message i get. What no earth is the $html_txt string? would i something like the following?
Code:
<?
include ('../services/mysql_connect.php');
$query = "SELECT ' FROM table where id='?what woudl go in here?' ";
$result = @mysql_query($query);

if ($result) {
while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {


echo "
</div>

<h3>$row[title]</h3><br />

<i>Posted on</i> $row[sd] <i>by <a href=\"..//login/userinfo.php?user=$row[author]\">$row[author]</a></i>

<br /><br />

$row[post])

<br /><br />

<i>Categories Posted in:</i> <a href=\"$row[category].php\">$row[category]</a>
 
try this?

Code:
$id = intval($_GET['id']);
if($id < 1) {
    //header("location: index.php"); //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'");
if(@mysql_num_rows($result) != 1) {
    //header("location: index.php"); //article id supplied doesn't exist in db
} else {
    $row = mysql_fetch_assoc($result);
    echo '<h3>'.$row['title'].'</h3><br />';
    echo '<i>Posted on</i>'.$row[sd].'<i>by <a href="../login/userinfo.php?user='.$row[author].'">'.$row[author].'</a></i><br /><br />';
    echo $row['post'];
    echo '<br /><br />';
    echo '<i>Categories Posted in:</i> <a href="'.$row['category'].'.php">'.$row['category'].'</a>';
}

probably full of typos... :p

save that as article.php and you would then access the article by going to

article.php?id=1

now of course you don't want to have to type that in. you'll have an index page with article links generated from the db. :)
 
Last edited:
marc2003 said:
try this?

Code:
$id = intval($_GET['id']);
if($id < 1) {
    //header("location: index.php"); //redirect if id is not a valid number
}
$result = mysql_query("SELECT * FROM table WHERE id = '$id'");
if(@mysql_num_rows($result) != 1) {
    //header("location: index.php"); //article id supplied doesn't exist in db
} else {
    echo '<h3>'.$row['title'].'</h3><br />';
    echo '<i>Posted on</i>'.$row[sd].'<i>by <a href="../login/userinfo.php?user='.$row[author].'">'.$row[author].'</a></i><br /><br />';
    echo $row['post'];
    echo '<br /><br />';
    echo '<i>Categories Posted in:</i> <a href="'.$row['category'].'.php">'.$row['category'].'</a>';
}

probably full of typos... :p

save that as article.php and you would then access the article by going to

article.php?id=1

now of course you don't want to have to type that in. you'll have an index page with article links generated from the db. :)

That won't work to my knowledge....where are you getting the $row from, there is no declaration of it.
 
Try this, it should work fully, provided all the variables and names of things are correct (you may have to change them)

Code:
<?

include ('../services/mysql_connect.php');

$id = intval($_GET['id']);

if($id < 1) {
    header("location: index.php"); //redirect if id is not a valid number
}

$result = mysql_query("SELECT * FROM table WHERE id = '$id' LIMIT 0, 1");

if(mysql_num_rows($result) != 1); {
    header("location: index.php"); //article id supplied doesn't exist in db
} else {

    while ($row = mysql_fetch_array($result, MYSQL_ASSOC)) {

	    $html_text "<h3>$row['title']</h3><br /><i>Posted on</i> $row['sd'] <i>by <a href=\"..//login/userinfo.php?user=$row['author']\">$row['author']</a></i><br /><br />$row['post']<br /><br /><i>Categories Posted in:</i> <a href=\"$row['category'].php\">$row['category']</a>";

        $file = fopen(.$id.".html", 'w') or die("can't open file");
        fwrite($file, $html_text);
        fclose($file);
    }

	echo "Page Created called: ".$id.".html";

}

?>


EDIT:

Should you still need help, you can add me on MSN: ruudbarron [at] hotmail [dot] com
 
Last edited:
Conrad11 said:
That won't work to my knowledge....where are you getting the $row from, there is no declaration of it.

when i said it probably has typos, i meant i was going to leave out whole lines of code... :p

it's corrected now. :)
 
marc2003 said:
try this?

you sexy monster, at first didn't work as the ids i was trying didn't have an article with them, so i got straight on here typed out a whole post, and bam i realised why it didn't work, tried and yippee out popped an article.

thanks so much for everyone who helped me with this problem.

Only last wish of mine is if someone could explain how the $id = intval($_GET['id']); works?

I understand that what going on but this intval bit is a lil confusing as im not entirely sure on how it does what it does. Like is it just a it of code that says ok now look at what page we are at and read the part after the ? and then the bit that says get id looks for id= and gets the number or seomthing?

thanks again.
 
it just validates the id to make sure it's whole number. you need to clean all user input before using it to query your db. have a read of this....

http://php.robm.me.uk/#toc-SQLInjection

even if you're not malicious and you try and access article.php?id=b , instead of the page throwing an error, intval will just return a value of 0. this is then picked up by the next line of code and the page is re-directed back to the index (or wherever you want). :)
 
Back
Top Bottom