html - PHP approach to creating article list -
i plan write function dynamically create article , news list, don't know proper approach.
i have /articles
, /news
folders containing files article1.php
, article2.php
etc.
these files contain variables $date
(publishing date), $type
(news/article), $h1, $h2
(title, subtitle), $short
(short paraghaph display on list).
i want create list of these files displaying on 1 page.
html:
<div> $content <!--here goes list of articles/news--> </div>
would better to:
1.
- write while loop in
articlelist.php
:
[pseudocode] $content = ""; while (get filename /articles) include filename $content .= (variables filename.php parsed html) display $content
- and second same loop
newslist.php
.
(with approach e.g. sorting articles date difficult.)
or maybe:
2.
- create
articlearray.php
,newsarray.php
files storing data each article , news file in array in formkey : value = $date : [$type, $h1, $h2, $short]
- create
parsearray
function parsing whole given array html (containing data files) - call
$content = parsearray(...)
inarticlelist.php
,newslist.php
- display $content.
or there other better solution?
edit:
i don't have database because of small amount articles/news. i'll use 1 if it'll necessary, @ moment please assume should done pure php. (i asked question learning purposes, not practical.)
first of all: managing content , or code in different files recommended (for better understandability , maintainability), not obligatory. chose following approach. separate content 3 files:
index.php
(contains "main" functions)data.php
(contains data)functions.php
(contains callable functions)
index.php
// index.php require_once 'data.php'; require_once 'functions.php'; $allowedmodules = array('articles', 'news'); if(empty($_get['m']) || null === $_get['m']) { die('module required'); } else { $module = $_get['m']; } if(!in_array($module, $allowedmodules)) { die('invalid module'); } echo execute($module);
data.php
// data.php $data = array( 'articles' => array( array( 'date' => '2014-06-10', 'type' => 'article', 'h1' => 'my headline #1', 'h2' => 'subheadline #1', 'short' => 'my teaser' ), array( 'date' => '2014-06-09', 'type' => 'article', 'h1' => 'my headline #2', 'h2' => 'subheadline #2', 'short' => 'my teaser' ) ), 'news' => array( array( 'date' => '2014-06-08', 'type' => 'news', 'h1' => 'my news headline #3', 'h2' => 'subheadline #3', 'short' => 'my teaser' ), ) );
functions.php
// functions.php function execute($module) { global $data; $content .= '<div>'; foreach($data[$module] $item) { $content .= '<span>' . $item['date'] . '</span>'; $content .= '<h1>'. $item['h1'] . '</h1>'; // $content .= ... } $content .= "</div>"; return $content; }
now can call page via index.php?m=articles
or index.php?m=news
show articles or news.
sidenote: approach makes somehow easy switch database later.
Comments
Post a Comment