开发者

Create master-detail page using PHP

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?

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?


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.

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:

  • one page that queries database for the list of data, with typerlinks to detail page, passing unique record id via HTTP GET query string
  • one details page that queries database for the details, based on passed 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'; 
}  
?>

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>

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 ?>

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.


If you are, for example, displaying a list of summary data and you want to dynamically show the details for a particular record, you can use javascript (jQuery is a nice library that makes dealing with javascript easier).

Depending on the number of records you display on your summary page, you can either

  1. pull all the data (master and detail) from the database, output it with PHP and then hide the detail with javascript
  2. pull only the master data from the database, output it with PHP, and then do an AJAX request for the detail data when the user asks for it.

In neither case will you maintain all your data in memory.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜