私は自分が作成したダウンロードをいくつか紹介したい個人の Web サイトを持っています。各ダウンロードには説明とダウンロード リンクがありますが、形式がほとんど「標準. " そこで、すべてのテキストを XML ファイルに入れ、PHP を使用してそれを解析します。
私のXMLは次のようになります。
<txtdb>
<txt name="index">
<str key="title">Index</key>
<str key="metadescription">Personal site</key>
<str key="navigation">Navigation</key>
<str key="description"><!CDATA[[<h2>Description</h2>]]></key>
<str key="download"><!CDATA[[<h2>Download</h2>]]></key>
</txt>
<txt name="item1">
<str key="title">Item 1</key>
<str key="metadescription">Item 1 is awesome, get it now!</key>
<str key="description"><!CDATA[[<p>Item 1 is an incredible item that you must get right away!</p>]]></key>
<str key="download"><!CDATA[[<a href="http://dropbox.com">Here</a>]]></key>
</txt>
<!-- ... -->
</txtdb>
そして、ここに私のindex.phpがあります:
<?php
$current = 'index';
class TextDatabase()
{
private $_xdb;
private $_name;
public function __construct($xdt)
{
$this->_xdb = simplexml_load_file('./incl/txt.xml');
$this->_name = $xdt;
}
public function getString($key, $name = null)
{
if (empty($name))
{
$name = $this->_name;
}
$str = $this->_xdb->xpath(sprintf("//txt[@name='%s']/str[@key='%s']", $name, $key));
return empty($str[0]) ? null : (string) html_entity_decode($str[0]);
}
}
session_start();
if (isSet($_GET['name']))
{
$current = $_GET['name'];
$_SESSION['name'] = $current;
}
else if (isSet($_SESSION['name']))
{
$current = $_SESSION['name'];
}
else
{
$current = 'index';
}
$TxtDb = new TextDatabase($current);
include_once('incl/header.php');
include_once('incl/sidebar.left.php');
if ($current == 'index'):?>
<h2><?php echo $TxtDb->getString('navigation'); ?></h2>
<ul>
<li><a href="index.php?name=item1"><?php echo $TxtDb->getString('title','item1'); ?></a></li>
<li><a href="index.php?name=item2"><?php echo $TxtDb->getString('title','item2'); ?></a></li>
<!-- more items -->
</ul>
<?php else: ?>
<h2><?php echo $TxtDb->getString('description','index'); ?></h2>
<article><?php echo $TxtDb->getString('description'); ?></article>
<h2><?php echo $TxtDb->getString('download','index'); ?></h2>
<article><?php echo $TxtDb->getString('download'); ?></article>
<?php endif;
include_once('incl/sidebar.right.php');
include_once('incl/footer.php');
?>
現時点では、動作します。「index.php」に行くと、アイテムのリストが表示されます。次に、それらのいずれかをクリックすると、「index.php?name=item n 」に送信されます。ただし、ヘッダーに「index.php」を指すリンクが 1 つあり、それをクリックするとページがリロードされますが、インデックスに戻りません。インデックスに戻るには、リンクを 'index.php?name=index' を指すように変更する必要がありますが、これは気に入りません。「index.php」(パラメーターなし) を現在のアイテム (PHP セッションに保存されていると思われる) の代わりにインデックスに戻す方法はありますか?
PHP を扱うのはこれが初めてなので (C# の方が好きです)、ばかげた質問でしたら申し訳ありません。助けてくれてありがとう。