Jump to content

Automatically make a site


Anders Moen

Recommended Posts

I've made my own CMS system in PHP and MySQL, but when I add a news I need to go into my FTP account to make the new site where it gets out the news. Does anyone here know how I do this?One more thing: when I add a news, it gets an ID, so the third news I add will get the ID 3. How can I now automatically make a site called 3.php where the third news gets gathered out?

Link to comment
Share on other sites

Yeah, like a new page. Just like you have a page called index.php, maybe one called contact.php and on and on.But I want for example 1.php if the news a person just added was the first news into the database, so it'd get the ID 1.

Link to comment
Share on other sites

It's not the best practice to create a new page for each news item. It would be best to just pass the news ID through the URL, but if you want each news page to have its own URL for search engine purposes, then you would need to look into URL rewriting with mod_rewrite.There is also a full set is FTP functions that you can check out on the php.net reference.

Link to comment
Share on other sites

well i assume you arent interested in using a databse to store all of this, so maybe it would be better if you had a news.php file and in there is all of your news in variables like$news01 = "blah blah html blah blah news bold one";$news02 = "blah blah html blah blah news bold two";$news03 = "blah blah html blah blah news bold three";i dont think you start variables off with a number either.but what i would do is make a form on my server that i type my news entry in html format into and when i submit it, it till convert the html characters and it will append to the news file. then when i want to load it i would have to do the html entities decode whatever function....just an idea tho, but i would also password protect the page too.

Link to comment
Share on other sites

well i assume you arent interested in using a databse to store all of this, so maybe it would be better if you had a news.php file and in there is all of your news in variables like$news01 = "blah blah html blah blah news bold one";$news02 = "blah blah html blah blah news bold two";$news03 = "blah blah html blah blah news bold three";i dont think you start variables off with a number either.but what i would do is make a form on my server that i type my news entry in html format into and when i submit it, it till convert the html characters and it will append to the news file. then when i want to load it i would have to do the html entities decode whatever function....just an idea tho, but i would also password protect the page too.
I have the admin page password protected...But I mean, when I make websites for other people, they probably don't know how to use an FTP program, so it has to be automatically made.Let me give an example: 1. I add a thing to the database 2. It's get picked out on the frontpage with a maximum character limit and then a link to the same ID 3. And then I want a page to be made like this: fopen = "" . $row['id'] . ".php" (I don't remember the fopen code lol) And in the new page, I want a code to appear but I guess I can fix that myself...no, wait, I've already tried that
Link to comment
Share on other sites

I don't think you can open something that doesn't exist, but it's been a while since I fiddled with PHP, so get confirmation on that from someone else.

Link to comment
Share on other sites

1. I add a thing to the database 2. It's get picked out on the frontpage with a maximum character limit and then a link to the same ID 3. And then I want a page to be made like this: fopen = "" . $row['id'] . ".php" (I don't remember the fopen code lol) And in the new page, I want a code to appear but I guess I can fix that myself...no, wait, I've already tried that
Do you need a new page to be created? Because it's about 10 times easier and less of a hassle not to do that, to just use a page that gets the news ID and displays the text. But if you are hard up to use fopen, then I guess that's what you would do.
Link to comment
Share on other sites

Hmm...not sure what you mean there, but what I mean is that....I simply add a news and it gets an ID (for example 4). Then I want a site called 4.php (4 because of the ID it'll get), and in that file I want it automatically to be placed in the text that news number 4 had. Hope you understand

Link to comment
Share on other sites

Hmm...not sure what you mean there, but what I mean is that....I simply add a news and it gets an ID (for example 4). Then I want a site called 4.php (4 because of the ID it'll get), and in that file I want it automatically to be placed in the text that news number 4 had. Hope you understand
I believe what justsomeguy is getting at is that it is much easier to have a single page (called something like news.php) to which you pass a particular id and that page displays the appropriate news article. The URL, rather than being "4.php" would be "news.php?id=4". Or, using mod_rewrite (as justsomeguy also mentioned), the URL could be something like "news/4". However, in both of these cases, there would only need to be one file that would get its contents from the database depending on which news ID you passed to it.I hope this helps.
Link to comment
Share on other sites

Yes, it's gonna be index.php?id=4 if it's the ID number 4, but that's not the question!The question is how I make the page called 4.php without opening an FTP program...and also insert the code that gets out news number 4 from the database...

Link to comment
Share on other sites

Either you use mod_rewrite or you use fopen.How you use mod_rewrite i don't now,but the use of fopen isn too difficult the only problem is that you get a lot of files:

..	$file = fopen( $row['id'].'.php', "w");	$content = "<?php\n";	$content .= '  include("newsgetter.php"); '."\n";	// It's alot easier to have the code in a sepperate file and just include it,													//  than the same (long) code in all files.	$content .= "?>\n";	fwrite($content);	fclose($file);...

It's not harder than that, I just don't see the reason

Link to comment
Share on other sites

That's what I've been trying to make, but I've never tried with fopen and fwrite etc before so I never knew how I do it. Just one more thing (if I make it work): fwrite($content); // This line has an error:Warning: Wrong parameter count for fwrite() in /var/www/hotserv.dk/users/andersmoen/ on line 24

Link to comment
Share on other sites

It may be a problem with the id if you create the file "directly" after you've inserted the news (and if you're using MySQL) you can use mysql_insert_id():

..	$file = fopen( mysql_insert_id().'.php', "w");	$content = "<?php\n";	$content .= '  include("newsgetter.php"); '."\n";	// It's alot easier to have the code in a sepperate file and just include it,													//  than the same (long) code in all files.	$content .= "?>\n";	fwrite( $file, $content );	fclose( $file );...

Link to comment
Share on other sites

It may be a problem with the id if you create the file "directly" after you've inserted the news (and if you're using MySQL) you can use mysql_insert_id():
..	$file = fopen( mysql_insert_id().'.php', "w");	$content = "<?php\n";	$content .= '  include("newsgetter.php"); '."\n";	// It's alot easier to have the code in a sepperate file and just include it,													//  than the same (long) code in all files.	$content .= "?>\n";	fwrite( $file, $content );	fclose( $file );...

Hmmm.....well, what I did now, was to make a site where I take out the latest news from the table, and then I used the code you gave me.
Link to comment
Share on other sites

You're getting into a maintenance nightmare. If you have 100 news items, then you have 100 separate pages to display them. What if you want to change how all news items are displayed? Do you really want to go through each page and change the code? It seems like this would work better:

<?php$id = intval($_GET['id']); //news.php?id=xxx$result = mysql_query("SELECT * FROM news WHERE id={$id}");if (!($news = mysql_fetch_assoc($result)))  die("No news item found");mysql_free_result($result);echo "<div class=\"headline\">" . htmlentities($news['title']) . "</div>";echo "<div class=\"newsbody\">" . nl2br(htmlentities($news['body'])) . "</div>";?>

It will display any news item, and you only have to change this code if you want all news items to change.

Link to comment
Share on other sites

I'm not quite getting what you're saying. I thought that you wanted to create a new file (ex: 3.php) when you add a new news, or am I wrong?

Hmmm.....well, what I did now, was to make a site where I take out the latest news from the table, and then I used the code you gave me.
If you create the file when you add the news my last code should be better. And you said that you only found a file name '.php' not a file named '3.php' (or whatever the id should be).someguy:If he uses an include to a "template" (or whatever you want to call it) that handles the output/layout, he just wants the abbility to reach the news via host.com/news/id.php
Link to comment
Share on other sites

You're getting into a maintenance nightmare. If you have 100 news items, then you have 100 separate pages to display them. What if you want to change how all news items are displayed? Do you really want to go through each page and change the code? It seems like this would work better:
<?php$id = intval($_GET['id']); //news.php?id=xxx$result = mysql_query("SELECT * FROM news WHERE id={$id}");if (!($news = mysql_fetch_assoc($result)))  die("No news item found");mysql_free_result($result);echo "<div class=\"headline\">" . htmlentities($news['title']) . "</div>";echo "<div class=\"newsbody\">" . nl2br(htmlentities($news['body'])) . "</div>";?>

It will display any news item, and you only have to change this code if you want all news items to change.

Yes, but how do I make the site called the same as the ID?
Link to comment
Share on other sites

What's the problem with this code: http://w3schools.invisionzone.com/index.ph...10856&st=0#?As long that you make sure that you use the right id to name the file it should work fine.Ahh.. You probaly want to add something like this:

$code .= '$_GLOBALS["id"] = '.$id;

before the include. Then just get the news whith that id in the included file...edit:The included file could look something like this (The code from justsomegy, modified):

<?phpglobal $id; // The id from the file$result = mysql_query("SELECT * FROM news WHERE id={$id}");if (!($news = mysql_fetch_assoc($result)))  die("No news item found");mysql_free_result($result);echo "<div class=\"headline\">" . htmlentities($news['title']) . "</div>";echo "<div class=\"newsbody\">" . nl2br(htmlentities($news['body'])) . "</div>";?>

Link to comment
Share on other sites

I made it!But today when I read a topic about making a simple forum (which worked :)), he linked like <a href="view_topic.php?id=$rows[id]">text</a>, and if I i.e clicked the 4th topic, I came to view_topic.php?id=4 but on my server, 4.php wasn't there...it's like an invisble page. How did he do that? I just couldn't understand it :S

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
×
×
  • Create New...