Topic: Admin generated news box on website

Hi everybody! This is a html type question, so I guess this is the best spot for it.

I have a procedural question. Odds are that there are already a dozen tutorials out there for exactly what I want, but I'm not asking the right questions. I figure asking humans will hopefully get me pointed in the right direction rather than relying on Google's algorythms.

Challenge: I have responsibilityfor a site which has on its front page two news spots - one for a one-line "quick" news update and one for a longer, multi-paragraph news block (by "news" I mean whatever the admin wants to put on there).

The site is all hand coded with no CMS. I am comfortable with php and have MySql available (but not currently using it).

Current Workaround: The page currently uses php include to import an external text file which has both news items plus sundries (like footer information). Editing this file by hand and uploading via FTP is what is currently being done by me after receiving an email with the new news from the admin.

Desired Solution: I want to set it up so that the admin can update the news themselves. They are not web savvy nor know their way around an FTP program.

So far, I think what can be done is this:

  • Set up a folder "admin" and seal that off via .htaccess. This will allow the admin to log in via their browser with no third programs needed.

  • In that section, have some forms (or a single form) that will allow the admin to input the new data. Presumably the target for said form(s) would need to be "update file".

  • To my mind, either the form(s) would need to edit a text file which would then be read by the index, or the form(s) would need to update a database (which would then be read by the index).

It is this third part that I am stuck on. I think I have the theory right, but how to actually go about coding it I am not so sure of.

Any tips or links to tutorials?

Smile and say hello to people. It costs you nothing and can brighten their day.

Re: Admin generated news box on website

So if I understand correctly, you want a GUI for the admins to update news, sans having to editing actual pages.

http://www.brickshelf.com/gallery/fib12345/Lolz/lulz.png
"actuallly this involves spiderman too, not batman. but im also taking a new approach, more comedy, less action. i dont see to many movies like that with more comedy than action" --SteveStarfyTV on an Indiana Jones meets Star Wars idea.

Re: Admin generated news box on website

That's a nifty way of summing it up, yes.

Admin visits site via browser, logs in, types new news and clicks "update". New news now visible.

Smile and say hello to people. It costs you nothing and can brighten their day.

Re: Admin generated news box on website

So you don't want to use an FTP file to update the text file.

You can go about it by using a PHP function fwrite(); to actually write the postdata to the file, something like this:

input.php

$myFile = "admin.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
fwrite($fh, stripslashes($_POST['yayinput']));
fclose($fh);

post.php

<html>
<head>
<title>Post</title>
</head>
<body>
<form action="input.php" method="post">
<textarea name="yayinput"><?php echo file_get_contents('admin.txt'); ?></textarea><br />
<input type="submit" value="Go!" name="submit">
</form>
</body>
</html>

I don't know if that'll work and it's highly basic but basically what it does is what you want, you can probably insert some nice jazz into it but for a 2 minute job at lunchtime at school it's alright.

Re: Admin generated news box on website

mini/twitch !@#$ awesome beans Aled mini/tongue

mini/cat

Re: Admin generated news box on website

Thanks Aled, that's awesome!

It works great for the first part (working sample here) but I can't figure out how to get it to do the second part (multiple paragraphs).

The main issue is that the admin using this script is not code savvy and won't know to use \n for new lines (or <br> or other codes).

Is there a way to get php to echo a block of text the way it is written in the text file (as it does in a forum, for example) or does that need more complex database functions (I downloaded phpBB for a quick peek at the php code and couldn't make sense of it mini/sad ).

Smile and say hello to people. It costs you nothing and can brighten their day.

Re: Admin generated news box on website

Yes! Someone on another forum pointed out how to do this - there is a php command that will do it - nl2br.

Usage:

<?php
$comment = file_get_contents('admin.txt'); 
echo nl2br ($comment);
?>

So going on from Aled's code, the first line will read whatever is in the file admin.txt and the second line will display it on the screen. Adding the "nl2br" bit means that any line breaks in admin.txt will be displayed.

Thanks everyone!

Smile and say hello to people. It costs you nothing and can brighten their day.

Re: Admin generated news box on website

Does that box support HTML/PHP code, or whatever you may be using? It would be nice to edit most of the page (think images and fonts/colors/sizes, etc) online.

"[It] was the theme song for the movie 2010 first contact." ~ A YouTuber on Also Sprach Zarathustra
CGI LEGO! Updated occasionally...

Re: Admin generated news box on website

I like it, the only problem is, ditch the <marque> tag. That's old school HTML

Re: Admin generated news box on website

@Littlebrick - It does. If you feed through basic html (such as links) they get parsed correctly, not too sure about php. Feeding through simple php (such as a single argument) ought to work but the first ; would kill the data feed. I don't think it would work very well for major page entities (such as the layout and stylesheets).

@lil'jj - I agree with you on that one in principle, but it's not for my site and the person requesting this has their dear little heart set on scrolling text. I'd do it better with javascript or something but the <marquee> tag is there, it works and I can't be bothered putting in the extra effort to make it nicer in the code to have the same result on the outside.

At any rate, I got the nut cracked, so I'm happy with the end result. Thanks to everyone and especially thanks to Aled.

Smile and say hello to people. It costs you nothing and can brighten their day.

Re: Admin generated news box on website

If you wanted to execute PHP via the textbox (highly insecure, I do not recommend it) you put it like eval(nl2br(file_get_contents('admin.txt')));

Are you a web developer now then Leo?

Re: Admin generated news box on website

Yes and no.

I've been dabling with websites since 2001 but I've never really "developed" anything. I mainly just hand-code ultra-basic html and now getting into php since it is so useful. Javascript? No idea on that one. I can do rudimentary flash, but nothing really exciting or interesting. I've got a smattering of sites around the web that I've built either for myself or for others, but nothing to get excited over.

Smile and say hello to people. It costs you nothing and can brighten their day.