当前位置: 首页 > 工具软件 > Anchor CMS > 使用案例 >

asp.cms制定显示内容_如何建立CMS:显示内容

公冶智刚
2023-12-01

asp.cms制定显示内容

This article is part of a series of articles that intends to demonstrate ‘

本文是旨在说明“

How to build a content management system如何建立内容管理系统

In this article I intend to show how a content item can be identified directly (using type and id) or through the translation of a navigation type, then how this information can be used to create a menu for further navigation.

在本文中,我打算展示如何直接(使用

To begin with, we will look at the index.php file that was introduced in the previous article in a little more detail, then expand upon this to show how our content is selected and shown.

首先,我们将更详细地介绍上一篇文章中介绍的index.php文件,然后在此基础上进行扩展以显示如何选择和显示我们的内容。

Enable error reporting 启用错误报告

This simply enables PHP error reporting, which you will find very useful later down the line, when you come to make changes to the code and struggle to find out where the problems lie…

这只是启用了PHP错误报告,当您进行代码更改并努力找出问题出在哪里时,您将在以后的工作中找到非常有用的信息…

<?php
error_reporting(E_ALL);
ini_set('display_errors', 1);
Open Connection to database 打开与数据库的连接

Open and test the connection to your MySQL database using the details you created earlier.

使用先前创建的详细信息打开并测试与MySQL数据库的连接。

/* Open a connection */
$mysqli = new mysqli("localhost", "DATABASE_NAME", "DATABASE_PASSWORD", "DATABASE_USER");
/* check connection */
if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }
Process Input 流程输入

This line of code processes all POST and GET requests that have been sent to the page, then creates a new php variable by the same name for you to use throughout your code. 

这行代码处理已发送到页面的所有POST和GET请求,然后创建一个具有相同名称的新php变量,供您在整个代码中使用。

In the first instance, you will only be using the ‘id’ variable that we will place in the query string later on in the example. However, as you further develop your system, that same line of code will process all FORM data that has been sent to the page (for example when creating new pages).

在第一种情况下,您将仅使用在后面的示例中将放置在查询字符串中的'id'变量。 但是,当您进一步开发系统时,同一行代码将处理已发送到页面的所有FORM数据

/* process input */
foreach($_REQUEST as $key => $value){ $$key = $value;}
Find the home page 查找首页

This is important because if a user goes to the root of the site, or incorrectly enters information into the address bar, we need to be able to send them to the home page by default. For simplicity, in this example we simply tell the code to use ’id#1’, however later examples will find the correct id, based on the ordering of the main menu.

这很重要,因为如果用户转到网站的根目录,或者在地址栏中错误地输入了信息,则默认情况下,我们需要能够将其发送到主页。 为简单起见,在此示例中,我们仅告诉代码使用“ id#1”,但是后面的示例将根据主菜单的顺序找到正确的ID。

/* find homepage */
$homepageid=1;
Display content item to user 向用户显示内容项

In order to display a content item to the user, we need to know the type and id of the item. This information can be passed to us directly, or through the use of a navigation type.

为了向用户显示内容项目,我们需要知道该项目的 。 这些信息可以直接传递给我们,也可以通过使用导航类型传递给我们。

Both methods are equally important. As you can imagine, it is important to be able to access every single content item via one method or another. However, we wouldn’t necessarily want every single item added to the navigation. Using a navigation level enables us to map a route to selected items for our end users, whilst hiding non-essential information.

两种方法同等重要。 您可以想象,能够通过一种或另一种方法访问每个单个内容项非常重要。 但是,我们不一定希望将每个项目都添加到导航中。 使用导航级别使我们能够将路线映射到最终用户的选定项目,同时隐藏不必要的信息。

To the end user, the system is therefore navigated using the ‘id’ variable which will be passed from page to page as a parameter in the URL. This ‘id’ will then map directly to a row in the ‘nav’ table.  Each ‘nav’ instance has two defined fields type and typeid which are extracted everytime we process the navigation.

对于最终用户,系统使用“ id”变量进行导航,该变量将作为URL中的参数从一个页面传递到另一个页面。 然后,此“ id”将直接映射到“ nav”表中的一行。 每个“ nav”实例都有两个定义的字段 ,每次我们处理导航时都会提取这些字段。

Our system identifies when we are using the navigation level, if only the [id] parameter is detected as input. In which case it retrieves the type and typeid from the nav table for later use.

如果仅检测到 ,以供以后使用。

/* process nav to determine type & typeid */
if(isset($id)){
$sql = "SELECT type, typeid FROM nav WHERE id = $id";
	if ($result = $mysqli->query($sql)) {
		$row = $result->fetch_assoc();
		foreach($row as $key => $value) {
			$$key = html_entity_decode($value, ENT_QUOTES);
		}
		$result->free();
	}
}

If instead our system detects both the [type] and [typeid] parameters have been passed as input, then it knows to skip the above step and use the inputted variables instead.

相反,如果我们的系统检测到[type]和[typeid]参数都已作为输入传递,那么它知道跳过上述步骤,而是使用输入的变量。

Process output 过程输出

This next section retrieves information from the database to display to the user at a later stage.

下一部分将从数据库中检索信息,以在以后显示给用户。

Using the two variables we found in the previous section, the code below retrieves the row from the ‘page’ table (stored in variable $type), where the id is equal to the variable $typeid. Each field from the retrieved row is then stored as a variable by the same name, for future use.

使用在上一节中找到的两个变量,下面的代码从“页面”表(存储在变量$ type中)中检索行,其中id等于变量$ typeid。 然后将检索到的行中的每个字段存储为相同名称的变量,以备将来使用。

For example, if you are on the home page, the variables would store:  $page_id=’1’, $page_title=’welcome to our website’, $page_content=’Content for the welcome page’.

例如,如果您在主页上,则变量将存储:$ page_id ='1',$ page_title ='欢迎来到我们的网站',$ page_content ='欢迎页面的内容'。

/* process output */
$sql = "SELECT * FROM $type WHERE `id` = '$typeid'";
if ($result = $mysqli->query($sql)) {
	$row = $result->fetch_assoc();
	foreach($row as $key => $value) {
		$newvarible =$type."_".$key;
		$$newvarible = html_entity_decode($value, ENT_QUOTES);
	}
	$result->free();
}
Output to the user 输出给用户

In this example, we will only output the raw data without any consideration to layout / style etc.

在此示例中,我们将仅输出原始数据,而无需考虑布局/样式等。

In order to do this, we simply provide a basic HTML template and use the PHP echo command to output the two variables $page_title and $page_content.

为此,我们仅提供一个基本HTML模板,并使用PHP echo命令输出两个变量$ page_title和$ page_content。

Note: the ‘content’ field for each page will include HTML formatting, therefore we don’t need to worry about adding this to the page template. Future articles will show you how to incorporate a WSYWIG editor into the system, which will send formatted data to the database.

注意:每个页面的“内容”字段将包含HTML格式,因此我们不必担心将其添加到页面模板中。 以后的文章将向您展示如何将WSYWIG编辑器合并到系统中,该系统会将格式化的数据发送到数据库。

echo "<h3>$page_title</h3>
      $page_content";
Process navigation 流程导航

This last bit of code retrieves all ‘nav’ items that have been related to the ‘menu’ (id#1) in the relations table. We then use an INNER JOIN to combine the data from the ‘nav’ table for future use. We then cycle through each record using a WHILE statement and create a list of links.

最后的代码检索与关系表中的“菜单”(id#1)相关的所有“导航”项目。 然后,我们使用INNER JOIN合并来自“ nav”表的数据,以备将来使用。 然后,我们使用WHILE语句循环遍历每条记录,并创建链接列表。

The ‘title’ attribute is used for the anchor text, and the ‘id’ attribute is used appended to the HREF string as follows: index.php?id=XX. Upon a new page being loaded, this ‘id’ is then processed and instructs the rest of the system what information to display.

'title'属性用于锚文本,并且'id'属性用于附加到HREF字符串,如下所示:index.php?id = XX。 加载新页面后,将处理此“ id”,并指示系统其余部分显示哪些信息。

/* process navigation */
$sql = "SELECT nav.id, nav.title FROM relations INNER JOIN nav ON relations.childtypeid=nav.id WHERE parenttype = 'menu' AND parenttypeid = '1'";
if ($result = $mysqli->query($sql)) {
	echo "<ul>";
	while ($row = $result->fetch_assoc()) {
		echo "<li><a href='index.php?id={$row['id']}'>{$row['title']}</a></li>";
	}
	echo "</ul>";
}
Close database connection 关闭数据库连接

This final statement closes the MySQLi connection.

最后一条语句关闭了MySQLi连接。

<?php $mysqli->close(); ?>

The code below shows how we incorporate these additional features into the working example from the previous article:

下面的代码显示了我们如何将这些附加功能整合到上一篇文章的工作示例中:

<html>
	<body>
		<h1><u>CMS Example #2</u></h1>
<?php
/* Enable error reporting */
error_reporting(E_ALL);
ini_set('display_errors', 1);

/* Open a connection */
$mysqli = new mysqli("localhost", "DATABASE_NAME", "DATABASE_PASSWORD", "DATABASE_USER");
/* check connection */
if (mysqli_connect_errno()) { printf("Connect failed: %s\n", mysqli_connect_error()); exit(); }

/* process input - create a variable for each input and assign the value*/
foreach($_REQUEST as $key => $value){ $$key = $value;}

/* process actions */
if(isset($action)){

	echo "<p>PROCESSING ACTION...</p>";

	if($action=="login"){

		echo "<p>PROCESSING LOGIN...</p>";
		
		unset($action);

		echo "<p>LOGIN SUCCESS... REDIRECTING TO DASHBOARD</p>";

		$display="dashboard";

	}
}

/* find homepage */
$homepageid=1;

if(isset($display)){
	if($display=="login") {

		echo 	"<p>LOGIN VIEW<p>
			 <p>To login, please <a href='index.php?action=login'>click here</a>.";

	}elseif($display=="dashboard") {

		echo 	"<p>DASHBOARD VIEW</p>
			 <ul>
				<li>To view all pages <a href='index.php?display=view&type=page'>click here</a>.</li>
				<li>To add a new page <a href='index.php?display=add&type=page'>click here</a>.</li>
			</ul>";

	}elseif($display=="add") {

		echo "<p>ADD VIEW</p>";

	}elseif($display=="view") {

		echo "<p>DISPLAY ALL VIEW</p>";

	}
}elseif(isset($id)||(isset($type)&&isset($typeid))){

	/* process nav to determine type & typeid */

	if(isset($id)){
		$sql = "SELECT type, typeid FROM nav WHERE id = $id";
		if ($result = $mysqli->query($sql)) {
			$row = $result->fetch_assoc();
			foreach($row as $key => $value) {
				$$key = html_entity_decode($value, ENT_QUOTES);
			}
			$result->free();
		}
	}

	/* process output */

	$sql = "SELECT * FROM $type WHERE `id` = '$typeid'";
	if ($result = $mysqli->query($sql)) {
		$row = $result->fetch_assoc();
		foreach($row as $key => $value) {
			$newvarible =$type."_".$key;
			$$newvarible = html_entity_decode($value, ENT_QUOTES);
		}
		$result->free();
	}

	echo 	"<h3>$page_title</h3>
		$page_content";
	
}else{

	/* display home page */
	echo "<p>HOME PAGE</p>";

}
?>
		<h2>Menu</h2>
		<?php
		/* process navigation */
		$sql = "SELECT nav.id, nav.title FROM relations INNER JOIN nav ON relations.childtypeid=nav.id WHERE parenttype = 'menu' AND parenttypeid = '1'";
		if ($result = $mysqli->query($sql)) {
			echo "<ul>";
			while ($row = $result->fetch_assoc()) {
				echo "<li><a href='index.php?id={$row['id']}'>{$row['title']}</a></li>";
			}
			echo "</ul>";
            $result->free();
		}
$mysqli->close();
		?>
	</body>
</html>

You can find a working example of code above at the following url:

您可以在以下网址找到上述代码的有效示例:

http://www.suremedia.co.uk/cms-example-2/index.php http://www.suremedia.co.uk/cms-example-2/index.php

You can also download the SQL for the example using the link below:

您还可以使用以下链接下载示例SQL:

example1.sql example1.sql

In summary, we have shown the two different methods for identifying which content item to display and the process of outputting the information. We have also looked at how a 'navigation' layer can be added to control exactly which content items are navigable to the user.

总而言之,我们展示了两种不同的方法来标识要显示的内容项以及输出信息的过程。 我们还研究了如何添加“导航”层以精确控制用户可以浏览哪些内容项。

In the next article Building a CMS: User authentication and security we look at the login process in a lot more detail and also introduce a number of small measures to protect our system against malicious use.

在下一篇文章“ 构建CMS:用户身份验证和安全性”中,我们将详细介绍登录过程,并介绍一些小的措施来保护我们的系统免遭恶意使用。

翻译自: https://www.experts-exchange.com/articles/19740/How-to-build-a-CMS-Displaying-the-content.html

asp.cms制定显示内容

 类似资料: