Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
D
Development_of_Web_Applications
Manage
Activity
Members
Labels
Plan
Issues
Issue boards
Milestones
Wiki
Requirements
Code
Merge requests
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Locked files
Build
Pipelines
Jobs
Pipeline schedules
Test cases
Artifacts
Deploy
Releases
Package registry
Container registry
Model registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Code review analytics
Issue analytics
Insights
Model experiments
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Silas Meister
Development_of_Web_Applications
Commits
8e902533
Commit
8e902533
authored
1 year ago
by
Silas Meister
Browse files
Options
Downloads
Patches
Plain Diff
Create NewsArticle-Page to fetch articles by id
parent
55b72ad4
Branches
Branches containing commit
Tags
Tags containing commit
No related merge requests found
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
functions.js
+1
-1
1 addition, 1 deletion
functions.js
php/Index.php
+3
-1
3 additions, 1 deletion
php/Index.php
php/NewsArticle.php
+94
-0
94 additions, 0 deletions
php/NewsArticle.php
php/NewsFetch.php
+4
-2
4 additions, 2 deletions
php/NewsFetch.php
php/Page.php
+6
-2
6 additions, 2 deletions
php/Page.php
with
108 additions
and
6 deletions
functions.js
+
1
−
1
View file @
8e902533
...
@@ -77,7 +77,7 @@ function createNewsChild(news) {
...
@@ -77,7 +77,7 @@ function createNewsChild(news) {
a
.
appendChild
(
textNode
);
a
.
appendChild
(
textNode
);
const
hrefAttr
=
document
.
createAttribute
(
'
href
'
);
const
hrefAttr
=
document
.
createAttribute
(
'
href
'
);
hrefAttr
.
value
=
'
#
'
;
hrefAttr
.
value
=
'
NewsArticle.php?articleId=
'
+
encodeURIComponent
(
news
[
'
id
'
])
;
a
.
setAttributeNode
(
hrefAttr
);
a
.
setAttributeNode
(
hrefAttr
);
...
...
This diff is collapsed.
Click to expand it.
php/Index.php
+
3
−
1
View file @
8e902533
...
@@ -40,13 +40,15 @@ class Index extends Page {
...
@@ -40,13 +40,15 @@ class Index extends Page {
$news
[]
=
new
News
(
$title
,
$imgurl
,
$content
);
$news
[]
=
new
News
(
$title
,
$imgurl
,
$content
);
}
}
$resultSet
->
close
();
return
$news
;
return
$news
;
}
}
protected
function
generateView
():
void
protected
function
generateView
():
void
{
{
$this
->
news
=
$this
->
getViewData
();
$this
->
news
=
$this
->
getViewData
();
$this
->
generatePageHeader
(
'NASA'
);
$this
->
generatePageHeader
(
'NASA'
,
'../functions.js'
);
echo
<<<EOT
echo
<<<EOT
...
...
This diff is collapsed.
Click to expand it.
php/NewsArticle.php
0 → 100644
+
94
−
0
View file @
8e902533
<?php
declare
(
strict_types
=
1
);
require_once
'Page.php'
;
class
NewsArticle
extends
Page
{
private
const
ARTICLE_ID
=
'articleId'
;
private
const
TITLE
=
'title'
;
private
const
IMGURL
=
'imgurl'
;
private
const
CONTENT
=
'content'
;
private
int
$articleId
;
protected
function
__construct
()
{
parent
::
__construct
();
$this
->
articleId
=
-
1
;
session_start
();
}
public
function
__destruct
()
{
parent
::
__destruct
();
}
protected
function
getViewData
():
array
{
$stmt
=
$this
->
database
->
prepare
(
"SELECT n.title, n.imgurl, n.content FROM news n WHERE n.id = ?"
);
$stmt
->
bind_param
(
"i"
,
$this
->
articleId
);
$stmt
->
execute
();
$resultSet
=
$stmt
->
get_result
();
if
(
$resultSet
->
num_rows
==
0
)
return
array
();
$article
=
$resultSet
->
fetch_assoc
();
$array
=
array
(
self
::
TITLE
=>
$article
[
self
::
TITLE
],
self
::
IMGURL
=>
$article
[
self
::
IMGURL
],
self
::
CONTENT
=>
$article
[
self
::
CONTENT
]
);
$resultSet
->
free
();
$stmt
->
close
();
return
$array
;
}
protected
function
generateView
():
void
{
$data
=
$this
->
getViewData
();
$this
->
generatePageHeader
(
'News Article'
);
var_dump
(
$data
);
echo
<<<EOT
<body>
<header></header>
</body>
EOT;
$this
->
generatePageFooter
();
}
protected
function
processReceivedData
():
void
{
parent
::
processReceivedData
();
if
(
$_SERVER
[
'REQUEST_METHOD'
]
===
'GET'
)
{
if
(
isset
(
$_GET
[
self
::
ARTICLE_ID
])
&&
is_numeric
(
$_GET
[
self
::
ARTICLE_ID
]))
{
$this
->
articleId
=
intval
(
$_GET
[
self
::
ARTICLE_ID
]);
}
}
}
public
static
function
main
():
void
{
try
{
$page
=
new
NewsArticle
();
$page
->
processReceivedData
();
$page
->
generateView
();
}
catch
(
Exception
$e
)
{
header
(
"Content-type: text/html; charset=UTF-8"
);
echo
$e
->
getMessage
();
}
}
}
NewsArticle
::
main
();
\ No newline at end of file
This diff is collapsed.
Click to expand it.
php/NewsFetch.php
+
4
−
2
View file @
8e902533
...
@@ -5,6 +5,7 @@ require_once 'util/constants.php';
...
@@ -5,6 +5,7 @@ require_once 'util/constants.php';
class
NewsFetch
extends
Page
class
NewsFetch
extends
Page
{
{
private
const
ID
=
"id"
;
private
const
TITLE
=
"title"
;
private
const
TITLE
=
"title"
;
private
const
SEARCH_WORD
=
"searchWord"
;
private
const
SEARCH_WORD
=
"searchWord"
;
...
@@ -31,15 +32,16 @@ class NewsFetch extends Page
...
@@ -31,15 +32,16 @@ class NewsFetch extends Page
$likeSearchWord
=
"%"
.
$this
->
searchWord
.
"%"
;
$likeSearchWord
=
"%"
.
$this
->
searchWord
.
"%"
;
$stmt
=
$this
->
database
->
prepare
(
"SELECT n.title FROM news n WHERE LOWER(n.title) LIKE LOWER(?) LIMIT 5"
);
$stmt
=
$this
->
database
->
prepare
(
"SELECT
n.id,
n.title FROM news n WHERE LOWER(n.title) LIKE LOWER(?) LIMIT 5"
);
$stmt
->
bind_param
(
"s"
,
$likeSearchWord
);
$stmt
->
bind_param
(
"s"
,
$likeSearchWord
);
$stmt
->
execute
();
$stmt
->
execute
();
$resultSet
=
$stmt
->
get_result
();
$resultSet
=
$stmt
->
get_result
();
while
(
$row
=
$resultSet
->
fetch_assoc
())
{
while
(
$row
=
$resultSet
->
fetch_assoc
())
{
$id
=
$row
[
self
::
ID
];
$title
=
$row
[
self
::
TITLE
];
$title
=
$row
[
self
::
TITLE
];
$data
[]
=
array
(
self
::
TITLE
=>
$title
);
$data
[]
=
array
(
self
::
ID
=>
$id
,
self
::
TITLE
=>
$title
);
}
}
return
$data
;
return
$data
;
...
...
This diff is collapsed.
Click to expand it.
php/Page.php
+
6
−
2
View file @
8e902533
...
@@ -25,7 +25,7 @@ abstract class Page {
...
@@ -25,7 +25,7 @@ abstract class Page {
/*$this->database->close();*/
/*$this->database->close();*/
}
}
protected
function
generatePageHeader
(
string
$title
=
""
):
void
protected
function
generatePageHeader
(
string
$title
=
""
,
string
$jsScript
=
""
):
void
{
{
$title
=
htmlspecialchars
(
$title
);
$title
=
htmlspecialchars
(
$title
);
header
(
"Content-type: text/html; charset=UTF-8"
);
header
(
"Content-type: text/html; charset=UTF-8"
);
...
@@ -37,7 +37,11 @@ abstract class Page {
...
@@ -37,7 +37,11 @@ abstract class Page {
<title>$title</title>
<title>$title</title>
<meta charset="UTF-8" />
<meta charset="UTF-8" />
<meta name="viewport" content="width=device-width, initial-scale=1">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="../functions.js"></script>
EOT;
if
(
!
empty
(
trim
(
$jsScript
)))
echo
"<script src=
\"
$jsScript
\"
></script>"
;
echo
<<<EOT
<link rel="stylesheet" style="text/css" href="../styles.css" />
<link rel="stylesheet" style="text/css" href="../styles.css" />
</head>
</head>
EOT;
EOT;
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Loading
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment