如何创建从数据库驱动的动态菜单和页面? [英] How can I create dynamic menus and pages driven from database?
问题描述
我正在创建一个简单的报告系统,我想要从数据库生成menues和页面。
我看到 YouTube上的这部影片< a>并设法创建一个包含以下代码的菜单。
我有一个名为 Reports
的数据库表和名为 rep_id
, rep_date
, rep_ledit_date
, rep_by
,部门
,位置
,报告
和 rep_to
。
所以基于上面的方法,我创建了一个使用这个代码的菜单。
<?php
require(includes / db.php);
mysqli_select_db($ con,'$ db_name');
$ sql =SELECT * FROM reports;
$ result = mysqli_query($ con,$ sql)或者死(mysqli_error($ con));
$ represult = mysqli_fetch_assoc($ result);
$ rep_by = $ represult ['rep_by'];
$ report = $ represult ['report'];
?>
< li>菜单
< ul>
<?php do
{
?>
< li>< a href =reports.php?rep_id =<?php echo $ represult ['rep_id'];?>> <?php echo $ represult ['rep_by']。 (。$ represult ['rep_date']。); ?>< / A>< /锂>
<?php
} while($ represult = mysqli_fetch_assoc($ result));
// $ represult ['rep_by'];
// $ represult ['report'];
// $ represult ['report'];
?>
< / ul>
< / li>
所以我创建了一个名为 reports.php
查看数据库中的内容的详细信息。我想要的是将以下 rep_by(rep_date)作为标题和内容报告。
我可能想要在内容中使用其他列。那么什么样的代码菜单和 reports.php
应该实现我想要的。我所做的是以下内容,只有当所有的菜单链接被点击时才输出第一行。
require(includes / db.php);
mysqli_select_db($ con,'$ db_name');
$ sql =SELECT * FROM reports;
$ result = mysqli_query($ con,$ sql)或者死(mysqli_error($ con));
$ represult = mysqli_fetch_assoc($ result);
< h1> <?php echo $ represult ['rep_by']。 (。$ represult ['rep_date']。); ?>< / A>< / H1>
<?php echo $ represult ['report']; ?>
report.php:
<?php
require('includes / db.php');
mysqli_select_db($ con,$ db_name);
$ sql ='SELECT * FROM Reports WHERE rep_id =?';
$ stmt = $ CON> prepare($ sql);
$ id = $ _GET ['rep_id'];
$ stmt-> bind_param('i',$ id);
$ stmt-> execute();
$ stmt-> bind_result($ rep_id,$ rep_date,$ rep_ledit_date,$ rep_by,$ department,$ position,$ report,$ rep_to);
$ stmt-> fetch();
?>
< h1><?php echo$ rep_by($ rep_date); ?>< / H1>
<?php echo $ report; ?>
reports.php(调整一下)
<?php
require('includes / db.php');
mysqli_select_db($ con,$ db_name);
$ sql ='SELECT * FROM Reports';
$ stmt = $ CON> prepare($ sql);
$ stmt-> execute();
$ stmt-> bind_result($ rep_id,$ rep_date,$ rep_ledit_date,$ rep_by,$ department,$ position,$ report,$ rep_to);
?>
菜单
< ul>
<?php while($ stmt-> fetch()){?>
< li>< a href =report.php?rep_id =<?php echo $ rep_id;?>> <?php echo$ rep_by($ rep_date); ?>< / A>< /锂>
<?php}?>
< / ul>
I am a newbie here and for php too. I am not new to stackoverflow though :)
I am creating a simple reporting system and I want the menues and pages to be generated from the database.
I saw this video on YouTube and managed to create a menu with the following code.
I have a database table called Reports
and columns called rep_id
, rep_date
, rep_ledit_date
, rep_by
, department
, position
, report
, and rep_to
.
So based on the above method, I created a menu using this code.
<?php
require ("includes/db.php");
mysqli_select_db($con, '$db_name');
$sql = "SELECT * FROM reports";
$result = mysqli_query($con, $sql) or die (mysqli_error($con));
$represult=mysqli_fetch_assoc($result);
$rep_by=$represult['rep_by'];
$report=$represult['report'];
?>
<li> Menu
<ul>
<?php do
{
?>
<li><a href="reports.php?rep_id=<?php echo $represult['rep_id']; ?>"> <?php echo $represult['rep_by'] . " (" . $represult['rep_date'] . ")"; ?></a></li>
<?php
} while ($represult=mysqli_fetch_assoc($result));
//$represult['rep_by'];
//$represult['report'];
//$represult['report'] ;
?>
</ul>
</li>
So I created a page called reports.php
to see the details of the content in the database. What I wanted was to see the following rep_by (rep_date) as a heading and report as a content.
I might want to use other columns in the content too. So what kind of code the menu and reports.php
should have to achieve what I want. What I did was the following and it only outputs the first row when all the menu links are clicked.
require ("includes/db.php");
mysqli_select_db($con, '$db_name');
$sql= "SELECT * FROM reports";
$result = mysqli_query($con, $sql) or die (mysqli_error($con));
$represult=mysqli_fetch_assoc($result);
<h1> <?php echo $represult['rep_by'] . " (" . $represult['rep_date'] . ")"; ?></a></h1>
<?php echo $represult['report']; ?>
report.php:
<?php
require ('includes/db.php');
mysqli_select_db($con, $db_name);
$sql= 'SELECT * FROM Reports WHERE rep_id = ?';
$stmt = $con->prepare($sql);
$id = $_GET['rep_id'];
$stmt->bind_param('i', $id);
$stmt->execute();
$stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $department, $position, $report, $rep_to);
$stmt->fetch();
?>
<h1><?php echo "$rep_by ($rep_date)"; ?></h1>
<?php echo $report; ?>
reports.php (tweaked a bit)
<?php
require ('includes/db.php');
mysqli_select_db($con, $db_name);
$sql = 'SELECT * FROM Reports';
$stmt = $con->prepare($sql);
$stmt->execute();
$stmt->bind_result($rep_id, $rep_date, $rep_ledit_date, $rep_by, $department, $position, $report, $rep_to);
?>
Menu
<ul>
<?php while ($stmt->fetch()) { ?>
<li><a href="report.php?rep_id=<?php echo $rep_id; ?>"> <?php echo "$rep_by ($rep_date)"; ?></a></li>
<?php } ?>
</ul>
这篇关于如何创建从数据库驱动的动态菜单和页面?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!