Skip to content

Commit a0b4dd1

Browse files
authored
Merge pull request #2 from firoouzeh/refactor-helper
Refactor helper
2 parents 73a6474 + 2ac9d0c commit a0b4dd1

File tree

4 files changed

+65
-64
lines changed

4 files changed

+65
-64
lines changed

helper.php

Lines changed: 0 additions & 28 deletions
This file was deleted.

index.php

Lines changed: 4 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php
2-
include("helper.php");
2+
include("postHelper.php");
3+
include("menuHelper.php");
34
?>
45
<!DOCTYPE html>
56
<html lang="en">
@@ -63,16 +64,7 @@
6364
</button>
6465
<div class="collapse navbar-collapse" id="navbarResponsive">
6566
<ul class="navbar-nav ml-auto nav-pills">
66-
<?php
67-
$menus = menus();
68-
foreach($menus as $menu) {
69-
echo "<li class='nav-item'>";
70-
echo "<a class='nav-link' href='" . $menu['url'] . "'>";
71-
echo $menu['title'];
72-
echo "</a>";
73-
echo "</li>";
74-
}
75-
?>
67+
<?php echo menus(); ?>
7668
</ul>
7769
</div>
7870
</div>
@@ -83,31 +75,7 @@
8375
<div class="container">
8476
<div class="row">
8577
<div class="col-lg-8 col-md-10 mx-auto">
86-
<?php
87-
$posts = posts();
88-
foreach($posts as $post) {
89-
?>
90-
<div class="post-preview">
91-
<a href="./post.php">
92-
<h2>
93-
<?php
94-
echo $post['title'];
95-
?>
96-
</h2>
97-
</a>
98-
<div class="intro">
99-
<div class="clearfix">
100-
<p>
101-
<?php
102-
echo $post['intro'];
103-
?>
104-
</p>
105-
</div>
106-
</div>
107-
</div>
108-
<?php
109-
}
110-
?>
78+
<?php echo posts()?>
11179
<hr>
11280

11381
<div class="clearfix"></div>

menuHelper.php

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?php
2+
include("data.php");
3+
4+
/**
5+
* Get all menus
6+
*/
7+
function menus() {
8+
global $menus;
9+
10+
$allMenus = null;
11+
if (isset($menus) && count($menus) > 0) {
12+
foreach ($menus as $menu) {
13+
$allMenus .= createEachMenuItem($menu);
14+
}
15+
}
16+
return $allMenus;
17+
}
18+
19+
function createEachMenuItem($menu){
20+
$menuBody = "<li class='nav-item'>\n";
21+
$menuBody .= "<a class='nav-link' href='" . $menu['url'] . "'>\n";
22+
$menuBody .= $menu['title'];
23+
$menuBody .= "</a>\n";
24+
$menuBody .= "</li>\n";
25+
return $menuBody;
26+
27+
}

postHelper.php

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
<?php
2+
include("data.php");
3+
4+
/**
5+
* Get All Posts
6+
*/
7+
function posts()
8+
{
9+
global $posts;
10+
11+
$allPosts = null;
12+
if (isset($posts) && count($posts) > 0) {
13+
foreach ($posts as $post) {
14+
$allPosts .= createEachPost($post);
15+
}
16+
}
17+
return $allPosts;
18+
}
19+
20+
21+
function createEachPost($post){
22+
23+
$postBody = '<div class="post-preview">';
24+
$postBody .= '<a href="./post.php">';
25+
$postBody .= '<h2>'.$post['title'].'</h2>';
26+
$postBody .= '</a>';
27+
$postBody .= '<div class="intro">';
28+
$postBody .= '<div class="clearfix">';
29+
$postBody .= '<p>'.$post['intro'].'</p>';
30+
$postBody .= '</div>';
31+
$postBody .= '</div>';
32+
$postBody .= '</div>';
33+
return $postBody;
34+
}

0 commit comments

Comments
 (0)