本文介绍了使用PHP创建主从页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用PHP创建一个主从页面.我不想从MYSQL数据库中获取数据,而是想从关联数组中获取数据.这可能吗?

I want to create a master-detail page using PHP. Rather than getting data from a MYSQL database, I want to get the data from an associative array. Is this possible?

首先将从mysql数据库表中获取数据,并将其存储在关联数组中以进行某些处理.现在,我想仅基于关联数组中的数据创建一个主详细信息页面.任何有想法的人吗?

The data will first be obtained from the mysql database table and stored inside an associative array for some processing. Now I want to create a master detail page based on the data inside the associative array alone. Anyone with Ideas?

推荐答案

由于PHP的性质,这是不可能的.
PHP脚本要运行,然后.所有这些都是变量,关联数组和其他内容.

It's just impossible, due to PHP nature.
PHP script being run for a fraction of second and then dies. With all it's variables and associative arrays and other stuff.

这就是为什么数据库打算在不同的HTTP调用之间进行数据存储.

That's why a database intended to be data storage between distinct HTTP calls.

因此,不要假装是一个聪明人,让事情自然发展:

Thus, don't pretend to be a smartmass, let the things go natural way:

  • 一个用于查询数据库的数据列表的页面,其类型链接指向详细信息页面,并通过HTTP GET查询字符串传递唯一的记录ID
  • 一个详细信息页面,该页面根据传递的ID向数据库查询详细信息.

这是使用模板的此类应用程序的一个非常基本的示例,旨在为您提供一个想法:

here is a very basic example of such application, using templates, to give you an idea:

<?  
mysql_connect(); 
mysql_select_db("new"); 
$table = "test"; 
if($_SERVER['REQUEST_METHOD']=='POST') { //form handler part: 
  $name = mysql_real_escape_string($_POST['name']); 
  if ($id = intval($_POST['id'])) { 
    $query="UPDATE $table SET name='$name' WHERE id=$id"; 
  } else { 
    $query="INSERT INTO $table SET name='$name'"; 
  } 
  mysql_query($query) or trigger_error(mysql_error()." in ".$query); 
  header("Location: http://".$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF']);  
  exit;  
}  
if (!isset($_GET['id'])) { //listing part: 
  $LIST=array(); 
  $query="SELECT * FROM $table";  
  $res=mysql_query($query); 
  while($row=mysql_fetch_assoc($res)) $LIST[]=$row; 
  include 'list.php'; 
} else { // form displaying part: 
  if ($id=intval($_GET['id'])) { 
    $query="SELECT * FROM $table WHERE id=$id";  
    $res=mysql_query($query); 
    $row=mysql_fetch_assoc($res); 
    foreach ($row as $k => $v) $row[$k]=htmlspecialchars($v); 
  } else { 
    $row['name']=''; 
    $row['id']=0; 
  } 
  include 'form.php'; 
}  
?>

详细信息页面模板,称为form.php

details page template called form.php

<form method="POST">
<input type="text" name="name" value="<?=$row['name']?>"><br>
<input type="hidden" name="id" value="<?=$row['id']?>">
<input type="submit"><br>
<a href="?">Return to the list</a>
</form>

和名为list.php的主页模板

and main page template called list.php

<a href="?id=0">Add item</a>
<? foreach ($LIST as $row): ?>
<li><a href="?id=<?=$row['id']?>"><?=$row['name']?></a>
<? endforeach ?>

这是管理页面的示例,可让您添加和编辑记录.
但是,页面tat仅显示数据将几乎相同.

this is example of admin page, letting you add and edit records.
however, the page tat just shows the data will be pretty much the same.

这篇关于使用PHP创建主从页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-24 09:46