有哪些方法可以将帖子排序并加载到动态php模板中
我目前有两个项目正在进行中,一个小型flash游戏网站,用于实践和掌握一些概念,另一个是博客/游戏开发广告的混合。我想问你们的问题是,加载帖子的最有效方式是什么,这样我就可以生成包含指定数量条目的页面,并根据我稍后定义的类别对其进行排序。这可能只是我的无知,但将每个文件存储在一个文件中似乎效率很低,因为要存储的文件会很多。 一个来自flash小游戏站点的基本游戏“post”的例子,实际上是一个div容器,我可以在其中添加一些东西,比如标签系统,以便稍后进行分类有哪些方法可以将帖子排序并加载到动态php模板中,php,javascript,jquery,Php,Javascript,Jquery,我目前有两个项目正在进行中,一个小型flash游戏网站,用于实践和掌握一些概念,另一个是博客/游戏开发广告的混合。我想问你们的问题是,加载帖子的最有效方式是什么,这样我就可以生成包含指定数量条目的页面,并根据我稍后定义的类别对其进行排序。这可能只是我的无知,但将每个文件存储在一个文件中似乎效率很低,因为要存储的文件会很多。 一个来自flash小游戏站点的基本游戏“post”的例子,实际上是一个div容器,我可以在其中添加一些东西,比如标签系统,以便稍后进行分类 <div class
<div class="game">
<a href="games/game.php?game=somegame"><img src="images/somegame.png"></a>
<div id="content">
<a href="games/game.php?game=somegame"><header>Game name</header></a>
<p>
Lorem ipsum dolor sit amet, consectetur adipiscing elit.
</p>
</div>
</div>
Lorem ipsum dolor sit amet,是一位杰出的献身者。
看来你是发展中国家的新手(这不是问题)。
您可能想寻找一种开箱即用的解决方案
想到的事情:Tumblr、Wordpress等
如果你真的想自己破解它,我建议你使用一个“框架”,你可以把它看作是编程语言之上的一个额外的层,以填补空白/使东西更易于使用
我真的可以推荐使用laravel(),它使用起来非常简单,文档会让你加快速度
希望它能帮助您创建这样的XML文件。读取XML文件有不同的方法。我猜你想把它读入Flash。所以你会看到actionscript。 为此,您可以轻松找到信息并开始学习。 (即) 这取决于你想要什么。您可以使用XML文件中的信息动态生成div块
<games>
<game>
<name>Some name</name>
<link>"games/game.php?game=somegame</link>
<image>"images/somegame.png"</image>
<content>Lorem Ipsum</content>
</game>
<game>
<name>Second name</name>
<link>"games/game.php?game=secondgame</link>
<image>"images/secondgame.png"</image>
<content>Lorem Ipsum</content>
</game>
</games>
某个名字
“games/game.php?game=somegame
“images/somegame.png”
乱数假文
第二个名字
“games/game.php?game=secondgame
“images/secondgame.png”
乱数假文
将这3个文件放在(本地)主机上的一个文件夹中,然后运行index.php-这就是您想要的吗
/wwwroot/post.tpl
<article id="{$postId}" class="{$postType}">
<div class="content" style="width:450px;float:left;margin-right:20px;">
<h3><a href="single-post.php?id={$postId}">{$postTitle}</a></h3>
<p>{$postContent}</p>
</div>
<a href="single-post.php?id={$postId}"><img style="height:150px;" src="{$postPreviewImage}"></a>
</article><br clear="all"/>
<!doctype html>
<html>
<head>
<meta charset="UTF-8"/>
<title>{$pageTitle}</title>
<script type="text/javascript" src="//code.jquery.com/jquery-latest.min.js"></script>
</head>
<body>
<h1>{$pageH1}</h1>
<hr>
Filter posts:
<select id="filter">
<option id="all">Show all posts</option>
{$filterOptions}
</select>
<hr>
{$posts}
<script>
$().ready(function() {
$("#filter").change(function() {
for(var b = $(this).find(":selected").attr("id"), d = document.getElementsByTagName("article"), c = 0;c < d.length;c++) {
var a = d[c];
"all" === b || b === a.className ? $(a).fadeIn() : b !== a.className && $(a).fadeOut()
}
});
});
</script>
</body>
</html>
/wwwroot/index.php
<?php
// get categories from DB
$postCategoriesFromDb = array(
array(
'slug' => 'games',
'name' => 'Games'
),
array(
'slug' => 'dev',
'name' => 'Development'
)
);
// get posts from DB
$postsFromDb = array(
array(
'id' => 1,
'type' => 'games',
'image' => 'http://openmatt.org/wp-content/uploads/2012/12/Game-On-banner.png',
'title' => 'A post about games',
'content' => 'This is the content of my game post.. lorem ipsum..'
),
array(
'id' => 2,
'type' => 'dev',
'image' => 'http://gregrickaby.com/wp-content/uploads/2012/03/github-logo.png',
'title' => 'A post about development',
'content' => 'This is the content of my dev post.. lorem ipsum..'
)
);
// function to populate single post template with post data
function getPostHtml($postData) {
$html = file_get_contents(__DIR__ . '/post.tpl');
$vars = array(
'{$postId}' => $postData['id'],
'{$postType}' => $postData['type'],
'{$postPreviewImage}' => $postData['image'],
'{$postTitle}' => $postData['title'],
'{$postContent}' => $postData['content']
);
return strtr($html, $vars);
}
// create HTML for each post
$posts = '';
foreach ($postsFromDb as $post) {
$posts .= getPostHtml($post);
}
// create HTML for category filter
$options = '';
foreach ($postCategoriesFromDb as $cat) {
$options .= sprintf('<option id="%s">%s</option>', $cat['slug'], $cat['name']);
}
// get main page template
$html = file_get_contents(__DIR__ . '/main.tpl');
// template vars
$vars = array(
'{$pageTitle}' => 'Page title',
'{$pageH1}' => 'Hello World!',
'{$filterOptions}' => $options,
'{$posts}' => $posts
);
// output to client
echo strtr($html, $vars);
最好是使用数据库并分类存储代码片段。在此之后,您可以获得查询中定义的任意数量的记录。我必须补充的是,您也可以将它们存储在XML文件中。。正如你所说,我真的宁愿自己“砍掉”它。现在我宁愿没有一个框架,主要是因为我的学习方式。我设定了一个目标并努力去实现它,让自己学习它以备将来使用。实际上,我在寻找更好的方式来存储信息,而不是加载信息。也许这还不太清楚。然后我会建议你把东西存储在数据库中(检查PHP的PDO文档,看看支持什么DB以及应该如何使用它)。哇,我不需要任何人帮我做这件事,我不是那样的,谢谢你。事实上,我并没有那么糟糕,我自己也可以做到,但无论如何,谢谢你。很抱歉接受了你的挑战。我就是睡不着。只花了几分钟就把它写下来了。但我相信你可以自己解决这个问题!:)