👥Step 2: Build web pages with PHP
Home || News || Stories || Installer ||
Function.php under private folder
<?php
function url_for($script_path) {
// add the leading '/' if not present
if($script_path[0] != '/') {
$script_path = "/" . $script_path;
}
return WWW_ROOT . $script_path;
}
?>
Staff_header.php under shared folder of private folder
<?php
if(!isset($page_title)) {$page_title = 'Staff Area';}
?>
<!doctype html>
<html lang="en">
<head>
<title> RMT TECH - <?php echo $page_title; ?> </title>
<!-- change the code to php -->
<link rel="stylesheet" media="all" href="<?php echo url_for('/stylesheet/staff.css'); ?>" />
</head>
<body>
<header>
<h1> RMT TECH Staff Area </h1>
</header>
<navigation>
<ul>
<!-- change the code to url for -->
<li><a href="<?php echo url_for (('/staff/index.php')); ?>">Menu</a></li>
</lu>
</navigation>
Index.php under staff folder
<?php require_once('../../private/initialize.php'); ?>
<?php $page_title = 'Staff Menu'; ?>
<?php include(SHARED_PATH . '/staff_header.php'); ?>
<div id="content">
<!-- add this or change -->
<div id="main-menu">
<h2>Main Menu</h2>
<a href="<?php echo url_for('/staff/subjects/index.php'); ?>">Subjects</a>
</div>
</div>
<?php include('../../private/shared/staff_footer.php'); ?>
</body>
</html>
Index.php under subjects folder
<?php require_once('../../../private/initialize.php'); ?>
<!-- add this -->
<?php
$subjects =
[
['id' => '1', 'position' => '1', 'visible' => '1', 'menu_name' => 'about RMT TECH'],
['id' => '2', 'position' => '2', 'visible' => '1', 'menu_name' => 'Customer'],
['id' => '3', 'position' => '3', 'visible' => '1', 'menu_name' => 'Small Business'],
['id' => '4', 'position' => '4', 'visible' => '1', 'menu_name' => 'Comercial'],
];
?>
<!-- END -->
<?php $page_title = 'Subjects'; ?>
<?php include('../../../private/shared/staff_header.php'); ?>
<div id="content">
<!-- add this -->
<div class="subjects listing">
<h1> Subjects </h1>
<div class="actions">
<a class="action" href="">Create New Subjects</a>
</div>
<table class="list">
<tr>
<th>ID</th>
<th>Position</th>
<th>Visible</th>
<th>Name</th>
<th> </th>
<th> </th>
<th> </th>
</tr>
<?php foreach($subjects as $subject)
{ ?>
<tr>
<td><?php echo $subject['id']; ?></td>
<td><?php echo $subject['position']; ?></td>
<td><?php echo $subject['visible'] == 1 ? 'true' : 'false'; ?></td>
<td><?php echo $subject['menu_name']; ?></td>
<td><a class="action" href="">View</a></td>
<td><a class="action" href="">Edit</a></td>
<td><a class="action" href="">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
<!-- end -->
</div>
<?php include('../../../private/shared/staff_footer.php'); ?>
</body>
</html>
Create extra php file, name show.php for our
view under subject folder
Index.php under subjects folder
<?php require_once('../../../private/initialize.php'); ?>
<?php
$subjects =
[
['id' => '1', 'position' => '1', 'visible' => '1', 'menu_name' => 'about RMT TECH'],
['id' => '2', 'position' => '2', 'visible' => '1', 'menu_name' => 'Customer'],
['id' => '3', 'position' => '3', 'visible' => '1', 'menu_name' => 'Small Business'],
['id' => '4', 'position' => '4', 'visible' => '1', 'menu_name' => 'Comercial'],
];
?>
<?php $page_title = 'Subjects'; ?>
<?php include('../../../private/shared/staff_header.php'); ?>
<div id="content">
<div class="subjects listing">
<h1> Subjects </h1>
<div class="actions">
<a class="action" href="">Create New Subjects</a>
</div>
<table class="list">
<tr>
<th>ID</th>
<th>Position</th>
<th>Visible</th>
<th>Name</th>
<th> </th>
<th> </th>
<th> </th>
</tr>
<?php foreach($subjects as $subject)
{ ?>
<tr>
<td><?php echo $subject['id']; ?></td>
<td><?php echo $subject['position']; ?></td>
<td><?php echo $subject['visible'] == 1 ? 'true' : 'false'; ?></td>
<td><?php echo $subject['menu_name']; ?></td>
<!-- add url to view -->
<td><a class="action" href="<?php echo url_for('/staff/subjects/show.php?id=' . $subject['id']); ?> ">View</a></td>
<td><a class="action" href="">Edit</a></td>
<td><a class="action" href="">Delete</a></td>
</tr>
<?php } ?>
</table>
</div>
</div>
<?php include('../../../private/shared/staff_footer.php'); ?>
</body>
</html>
Show.php under subjects folder
<?php
$id = $_GET['id'];
echo $id
?>
Show.php under subjects folder if the url dint find any
ID
<?php
// $id = isset($_GET['id']) ? $_GET['id'] : '1';
$id = $_GET['id'] ?? '1'; // PHP > 7.0 VERSION
echo $id
?>
Show.php under subject folder
<?php
// $id = isset($_GET['id']) ? $_GET['id'] : '1';
$id = $_GET['id'] ?? '1'; // PHP > 7.0 VERSION
echo $id
?>
<!-- ADD THIS -->
<a href="show.php?name=<?php echo urlencode ('Jam Campol'); ?>">Link</a><br>
<a href="show.php?company=<?php echo urlencode ('Widget&More'); ?>">Link</a><br>
<a href="show.php?name=<?php echo urlencode ('!#*?'); ?>">Link</a><br>
Function.php
<?php
function url_for($script_path) {
if($script_path[0] != '/') {
$script_path = "/" . $script_path;
}
return WWW_ROOT . $script_path;
}
// add this
function u($string="")
{
return urlencode($string);
}
function raw_u($string="")
{
return rawurlencode($string);
}
?>
Show.php
<!-- add this -->
<?php require_once('../../../private/initialize.php'); ?>
<?php
$id = $_GET['id'] ?? '1';
echo $id
?>
<!-- change utlencode to u only to shorten it -->
<a href="show.php?name=<?php echo u('Jam Campol'); ?>">Link</a><br>
<a href="show.php?company=<?php echo u('Widget&More'); ?>">Link</a><br>
<a href="show.php?name=<?php echo u('!#*?'); ?>">Link</a><br>
Go to browser
Add to url <strong> tag
Change the url <strong> tag to <script>
tag
Goto page source
Show.php
<?php require_once('../../../private/initialize.php'); ?>
<?php
$id = $_GET['id'] ?? '1';
// change code
echo htmlspecialchars($id);
?>
<a href="show.php?name=<?php echo u('Jam Campol'); ?>">Link</a><br>
<a href="show.php?company=<?php echo u('Widget&More'); ?>">Link</a><br>
<a href="show.php?name=<?php echo u('!#*?'); ?>">Link</a><br>
Function.php
<?php
function url_for($script_path) {
if($script_path[0] != '/') {
$script_path = "/" . $script_path;
}
return WWW_ROOT . $script_path;
}
function u($string="")
{
return urlencode($string);
}
function raw_u($string="")
{
return rawurlencode($string);
}
// add this
function h($string="")
{
return htmlspecialchars($string);
}
?>
Show.php
<?php require_once('../../../private/initialize.php'); ?>
<?php
$id = $_GET['id'] ?? '1';
// change code
echo h($id);
?>
<a href="show.php?name=<?php echo u('Jam Campol'); ?>">Link</a><br>
<a href="show.php?company=<?php echo u('Widget&More'); ?>">Link</a><br>
<a href="show.php?name=<?php echo u('!#*?'); ?>">Link</a><br>
1. Links and URL's
2. Use URL parameters
3. Default values for URL parameters
4. Encode URL parameters
5. Encode for HTML
Mga Komento
Mag-post ng isang Komento