我有一个多维数组,我使用foreach循环在表中显示所有值。在该表中,我有一个视图配置文件(a href =)。我想要做的就是获取数组并将其传递到另一个页面,并根据id显示数组的值
<?php include_once('fake_db.php'); ?>
<--index.php-->
<div class="widget-body">
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<th>Name</th><th>Position</th><th>Action</th>
</tr>
</thead>
<?php foreach($employees as $key => $value){ ?>
<tr>
<td><?= $value['name']?></td>
<td><?= $value['position']; ?></td>
<td><a href="profile.php?id=<?= $key; ?>">View Profile</a></td>
</tr>
<?php } ?>
</table>
</div>
<-- profile.php -->
<h3>Employee Profile : Employee Name</h3>
<a href="index.php">Back to Employee List</a>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<td>Name </td>
<td>Employee Position</td>
</tr>
</thead>
<tr>
<td></td>
<td></td>
</tr>
</table>
<--fake_db.php-->
<?php
$employees = array
(
1 => array("name" => 'Jason Alipala', "employee_id" => 'G1001-05', 'position' => 1),
2 => array("name" => 'Bryann Revina', "employee_id" => 'G1009-03', 'position' => 2),
3 => array("name" => 'Jeniel Mangahis', "employee_id" => 'G1009-04', 'position' => 2),
4 => array("name" => 'Arjay Bussala', "employee_id" => 'G1009-05', 'position' => 3),
5 => array("name" => 'Ronnel Ines', "employee_id" => 'G1002-06', 'position' => 3)
);
$emp_positions = array_count_values(array_column($employees, 'position'));
$positions = array(1 => 'TL', 2 => 'Programmer', 3 => 'Converter');
?>
答案 0 :(得分:1)
您可以使用$ _GET [•id']从您的ID中获取ID 您可以通过isset检查您的id是否通过... if(isset($ _ GET ['id'])
然后将该ID传递给数据库,或者如果您将数组..的键传递给数组
答案 1 :(得分:1)
请将profile.php
替换为以下内容。
<?php include_once('fake_db.php');
$id = $_GET['id'];
$employee = $employees[$id]
?>
<h3>Employee Profile : Employee Name</h3>
<a href="index.php">Back to Employee List</a>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<td>Name </td>
<td>Employee Position</td>
</tr>
</thead>
<tr>
<td><?php echo $employee['name'] ?></td>
<td><?php echo $employee['position'] ?></td>
</tr>
</table>
答案 2 :(得分:1)
在profile.php中,如下所示,为了方便起见,我将选定的员工信息数组放到selected_employees
数组中。
<?php if(isset($_GET["id"])):
$selected_employee=$employees[$_GET["id"]];
?>
<h1>Is Set</h1>
<h3>Employee Profile : <?= $selected_employee["name"]; ?></h3>
<a href="index.php">Back to Employee List</a>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<td>Name <?= $selected_employee["name"]; ?></td>
<td>Employee Position: <?= $selected_employee["position"]; ?></td>
</tr>
</thead>
</table>
<?php endif; ?>
答案 3 :(得分:0)
您可以接受@Khiev的回答。但是,如果您不想两次包含数据文件,则可以按照以下步骤操作-
<?php include_once('fake_db.php'); ?>
<--index.php-->
<div class="widget-body">
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<th>Name</th><th>Position</th><th>Action</th>
</tr>
</thead>
<?php foreach($employees as $key => $value){ ?>
<tr>
<td><?= $value['name']?></td>
<td><?= $value['position']; ?></td>
<td><a href="profile.php?id=<?= rawurlencode(json_encode($value)); ?>">View Profile</a></td>
</tr>
<?php } ?>
</table>
</div>
And in the `profile.php` file grab the `id` variable via `$_GET` variable.
<?php
$id = $_GET['id'];
$employee = json_decode(rawurldecode($id), true);
?>
<h3>Employee Profile : Employee Name</h3>
<a href="index.php">Back to Employee List</a>
<table class="table table-condensed table-striped table-bordered table-hover no-margin">
<thead>
<tr>
<td>Name </td>
<td>Employee Position</td>
</tr>
</thead>
<tbody>
<tr>
<td><?php echo $employee['name'] ?></td>
<td><?php echo $employee['position'] ?></td>
</tr>
</tbody>
</table>