|
Title: Some PHP stuff Post by: three_jump on March 19, 2007, 09:57:40 pm right, here is what I want / need to do:
I'm doing some simple kind of a news system atm and due having no sql at the webspace it has to be a bit "simple" (but also because of I know shit about sql databases and stuff :)). Anyway, my plan is to put all the news in different files like year.month.day.txt (070319.txt) and put them all in a directory. Now I want to read that directory, sort all files by date (therefore the filename) and echo them all on the newspage. The news files look like this: Code: 18.03.07<;> News Title<;> Description<;> Newstext<;> And my code I ended up is: Code: $tborder = 0; $twidth = "100%"; $cssheadline = "headline"; $cssdate = "date"; $csstext = "text"; $cnt = 0; $pfad = "content/news2/articles/"; function ls($dir) { $handle = opendir($dir); for(;(false !== ($readdir = readdir($handle)));) { if($readdir != '.' && $readdir != '..') { $path = $dir.'/'.$readdir; if(is_dir($path)) { $output[$readdir] = ls($path); } if(is_file($path) && substr($path,-4) == '.txt') { $output[] = $readdir; } } } return isset($output)?$output:false; closedir($handle); } $ary = ls($pfad); arsort ($ary); for ( $i = count($ary); $i >= 0; $i--) { $string = join("\n",file($pfad.$ary[$i])); // Description, Newstext can be over several lines and contain some html code $content[$cnt] = explode("<;>", $string); $len = count($content[$cnt]); for ($j=0; $j<$len-1; $j++) { $content[$cnt][$j]; } $cnt++; } // Augabe news.php $ncnt = $cnt-1; for ($i = 1; $i <= $ncnt; $i++) { echo "<table border=\"".$tborder."\" width=\"".$twidth."\">\n"; echo "<tr><td class=\"".$cssheadline."\">".$content[$i][1]."</td></tr>"; echo "<tr><td class=\"".$cssdate."\">".$content[$i][0]."</td></tr>"; echo "<tr><td class=\"".$csstext."\"><blockquote>".$content[$i][2]."</blockquote></td></tr>"; echo "</table>\n"; echo "<hr>"; } Ok, now me being a php noob asks you in hope that you know more than me if there is a better way to do that stuff? Title: Re: Some PHP stuff Post by: three_jump on March 19, 2007, 10:15:43 pm reason for this is also that I wanna do a rss feed at some point later and make me less work with publishing new news :)
atm it's just a simple html file :) Title: Re: Some PHP stuff Post by: Bismarck on March 20, 2007, 09:54:27 am Hi Jan,
maybe a cleaner solution, than this PHP one, using XSLT. So you have a news.xml, where you enter your news, like this: Code: <?xml version="1.0" encoding="utf-8"?> <?xml-stylesheet type="text/xsl" href="newssystem.xsl"?> <newssystem> <news date="2006-02-28"> <title>Old news</title> <description>This news is kind of old</description> <newstext>Hello dear readers, today i present you a news that will be old in a moment.</newstext> </news> <news date="2007-03-20"> <title>Tristan is wearing strange outfits</title> <description>This news is just undescribable</description> <newstext>Hello dear readers, this is a news text ...</newstext> </news> <news date="2007-03-14"> <title>News are for newsreaders</title> <description>This news is still undescribable</description> <newstext>Hello dear readers, no news today ...</newstext> </news> </newssystem> and you have a newssystem.xsl, where you can transform the newsitems to a document, like that: Code: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:template match="/"> <html> <head> <title>Jans news page</title> </head> <body> <xsl:apply-templates select="//news"> <xsl:sort select="@date"/> </xsl:apply-templates> </body> </html> </xsl:template> <xsl:template match="news"> <h2><xsl:value-of select="./title"/></h2> <p><xsl:value-of select="./newstext"/></p> </xsl:template> </xsl:stylesheet> You just need to call the news.xml in your browser. This should work in every not to outdated browser and is much more flexible, i think. Title: Re: Some PHP stuff Post by: three_jump on March 20, 2007, 11:05:42 am Would that work when I include that file in a .php file? (As it has to work with include as I'm using some kind of simple template system).
Also I would like to keep the single newsfile structure, as at some point I'll have to copy old news to some kind of archive which is easier by just copying files from one into another dir :) Title: Re: Some PHP stuff Post by: Bismarck on March 20, 2007, 11:42:53 am Would that work when I include that file in a .php file? (As it has to work with include as I'm using some kind of simple template system). No idea. If you mean whether it works, if you change the extension from .xml to .php, then yes it works.Also I would like to keep the single newsfile structure, as at some point I'll have to copy old news to some kind of archive which is easier by just copying files from one into another dir :) With "single newsfile" you think of one file per news?Else, if you only want to show actual news, you could for instance only show news, that are not older than an amount of specified days. So you can keep that news in one file. Title: Re: Some PHP stuff Post by: three_jump on March 20, 2007, 11:50:14 am Include like in I have one index.php which include the news.php or news.xml. Therefore I would have the html doctype and the xml doctype in the source code and I'm not sure if this would still be valid code or even worse would produce weird stuff...
about the 2nd: yes, one file per news. With you suggestion I would have a steady growing newsfile which gets loaded everytime but the user doesn't see most of the stuff that is / was loaded. (or am I wrong here?) Title: Re: Some PHP stuff Post by: three_jump on March 21, 2007, 11:35:06 pm So far I found not a real solution that is better than my crappy code (with the things I need) so I finished my news system to a stage that it works very well. :) Just needs some code clean up now...
I'm gonna post a link here (for those who are interested in how it looks now) in a few weeks when I have converted all news and the system goes online :) Title: Re: Some PHP stuff Post by: three_jump on March 24, 2007, 03:54:02 pm for those who care and don't mind the german texts... here is a short preview of how it will look when I put it online...
Title: Re: Some PHP stuff Post by: three_jump on March 26, 2007, 05:39:40 pm haha, can't belive it... rss feed works :)
Title: Re: Some PHP stuff Post by: three_jump on April 15, 2007, 01:13:22 am here it is....
http://www.radfest-buckow.de/index.php?topic=news Title: Re: Some PHP stuff Post by: nikimere on April 18, 2007, 12:34:16 pm looks well, i haven't looked at the code, did u use JavaScript & css to hide/show the news?
Title: Re: Some PHP stuff Post by: three_jump on April 18, 2007, 04:51:06 pm css / js only :) but I didn't code the js myself :p
Title: Re: Some PHP stuff Post by: nikimere on April 18, 2007, 05:23:59 pm cheater :p he he!! nah, only messing, good job ;)
Title: Re: Some PHP stuff Post by: three_jump on October 14, 2007, 04:11:58 pm /me digs out old thread...
ok, atm I'm working on some kind of a simple language switch, which is done via a cookie and working well so far. Now comes the problem: I'll have to do the site for at least 3 languages and therfore I though it would be cool to have all textstrings I used in just one file. The problem is now that I'm a bit helpless with loading the file so I have the strings (more than 5 in most cases) in all kind of functions without using function($string 1.... ) each time. I hope anyone has a cool idea to help me out :) |