diff options
author | cflip <36554078+cflip@users.noreply.github.com> | 2021-03-21 17:37:13 -0600 |
---|---|---|
committer | cflip <36554078+cflip@users.noreply.github.com> | 2021-03-21 17:37:13 -0600 |
commit | c84215091e914c81937c3aad2f1fd1775f556aa6 (patch) | |
tree | e8397714dc55dbdfe0ae7ec427c783b7cac5c92b | |
parent | f881d482e2c04e1bc622a5c53e4176f30ca8a337 (diff) |
Changes from procedural to OOP
-rw-r--r-- | category.php | 38 | ||||
-rw-r--r-- | index.php | 45 | ||||
-rw-r--r-- | thread.php | 17 | ||||
-rw-r--r-- | user.php | 28 |
4 files changed, 41 insertions, 87 deletions
diff --git a/category.php b/category.php index e559f22..a71029e 100644 --- a/category.php +++ b/category.php @@ -16,7 +16,7 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { <head> <title><?php echo $current->name; ?> - cflip.net forum</title> </head> -<body style="width: 720px;margin: auto;"> +<body> <?php include_once 'templates/header.php';?> <h1><?php echo $current->name; ?></h1> <p><?php echo $current->description; ?></p> @@ -28,36 +28,14 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { <th>Latest Post</th> </tr> <?php - include_once 'includes/db_inc.php'; + $threads = $current->get_threads($dbc); - $threads = current->get_threads(); - for each thread { - $thread->get_latest_post(); - } - - $sql = " - SELECT thread_id, thread_subject, thread_date_created, thread_date_lastpost, thread_category, thread_author, user_id, user_name - FROM threads - LEFT JOIN users - ON thread_author = user_id - WHERE thread_category = " . $_GET['id']; - - $result = mysqli_query($dbc, $sql); - - if (!$result) { - die('Error trying to display posts: ' . mysqli_error($dbc)); - } - - if (mysqli_num_rows($result) == 0) { - echo 'No categories found!'; - } else { - while ($row = mysqli_fetch_assoc($result)) { - echo '<tr>'; - echo '<td><b><a href="../thread/' . $row['thread_id'] . '">' . $row['thread_subject'] . '</a></b><br>'; - echo '<small>by ' . $row['user_name'] . ' on ' . date('M d, Y', strtotime($row['thread_date_created'])) . '</small></td>'; - echo '<td>' . date('M d, Y', strtotime($row['thread_date_lastpost'])) . '</td>'; - echo '</tr>'; - } + foreach ($threads as $thread) { + echo '<tr>'; + echo '<td><b><a href="../thread/' . $thread->id . '">' . $thread->subject . '</a></b><br>'; + echo '<small>by ' . $thread->author->name . ' on ' . date('M d, Y', strtotime($thread->date_created)) . '</small></td>'; + echo '<td>' . date('M d, Y', strtotime($thread->date_lastpost)) . '</td>'; + echo '</tr>'; } ?> </table> @@ -4,7 +4,7 @@ <head> <title>cflip.net forum</title> </head> -<body style="width: 720px;margin: auto;"> +<body> <?php include_once 'templates/header.php'; ?> <h2>Welcome to the cflip.net forum!</h2> <p> @@ -24,35 +24,34 @@ </tr> <?php include_once 'includes/db_inc.php'; + include_once 'model/Category.php'; - $sql = "SELECT * FROM categories"; - $result = mysqli_query($dbc, $sql); - - if (!$result) { - die('Error trying to display posts: ' . mysqli_error($dbc)); + // TODO: The get_all_categories function should return them in the right order + function cmp($a, $b) { + if ($a->id == $b->id) { + return 0; + } + return ($a->id < $b->id) ? -1 : 1; } - if (mysqli_num_rows($result) == 0) { - echo 'No categories found!'; - } else { - while ($row = mysqli_fetch_assoc($result)) { - echo ' <tr> - <td> - <b><a href="category/' . $row['cat_id'] . '">' . $row['cat_name'] . '</a></b> - <br> - ' . $row['cat_description'] . ' - </td> - <td>' . $row['cat_thread_count'] . '</td> - <td>' . $row['cat_post_count'] . '</td> - <td><b>my supercool thread</b><br><small>by <b>cflip</b>, 3 days ago</small></td> - </tr> -'; - } + $categories = get_all_categories($dbc); + usort($categories, "cmp"); + + foreach ($categories as $category) { + echo '<tr>'; + echo '<td>'; + echo '<b><a href="category/' . $category->id . '">' . $category->name . '</a></b>'; + echo '<br>' . $category->description; + echo '</td>'; + echo '<td>' . $category->thread_count . '</td>'; + echo '<td>' . $category->post_count . '</td>'; + echo '<td><b>my supercool thread</b><br><small>by <b>cflip</b>, 3 days ago</small></td>'; + echo '</tr>'; } ?> </table> <h2>More from the forum</h2> - <table width="100%"> + <table> <tr> <th>Recent Posts</th> <th>Recent Threads</th> @@ -19,24 +19,15 @@ if (!isset($_GET['id']) || !filter_var($_GET['id'], FILTER_VALIDATE_INT)) { <body> <?php include_once 'templates/header.php';?> <h1><?php echo $current->subject; ?></h1> - created by <b><?php echo '$current->user->name'; ?></b> + created by <b><?php echo $current->author->name; ?></b> in <b><?php echo $current->category->name; ?></b> <abbr title="<?php echo date('M d, Y g:ia', strtotime($current->date_created));?>">3 days ago</abbr> <hr> <?php - include_once 'includes/functions_display.php'; + $posts = $current->get_posts($dbc); - $sql = "SELECT post_id, post_content, post_date, post_author, user_id, user_name FROM posts LEFT JOIN users ON post_author = user_id WHERE post_thread = " . mysqli_real_escape_string($dbc, $_GET['id']); - $result = mysqli_query($dbc, $sql); - - if (!$result) { - die('Error trying to display posts: ' . mysqli_error($dbc)); - } - - if (mysqli_num_rows($result) == 0) { - echo '<section>This thread has no posts</section>'; - } else { - display_posts($dbc, $_GET['id'], $result); + foreach ($posts as $post) { + $post->display_content(); } ?> </body> @@ -1,38 +1,24 @@ <?php include_once 'includes/db_inc.php'; +include_once 'model/User.php'; session_start(); -$user_name = "Unknown"; -$user_date = 0; +$current = new User(); if (!isset($_GET['name'])) { } else { - $sql = "SELECT user_date FROM users WHERE user_name = '" . $_GET['name'] . "'"; - $result = mysqli_query($dbc, $sql); - - if (!$result) { - die('Error trying to display user page: ' . mysqli_error($dbc)); - } - - if (mysqli_num_rows($result) == 0) { - $user_name = "Unknown"; - } else { - while ($row = mysqli_fetch_assoc($result)) { - $user_name = $_GET['name']; - $user_date = $row['user_date']; - } - } + $current->get_by_name($_GET['name'], $dbc); } ?> <!DOCTYPE html> <html> <head> - <title><?php echo $user_name; ?>'s Profile - cflip.net forum</title> + <title><?php echo $current->name; ?>'s Profile - cflip.net forum</title> </head> -<body style="width: 720px;margin: auto;"> +<body> <?php include_once "templates/header.php" ?> - <h1><?php echo $user_name; ?></h1> - member since <?php echo $user_date; ?> + <h1><?php echo $current->name; ?></h1> + member since <?php echo date('M d, Y', strtotime($current->date)); ?> </body> </html>
\ No newline at end of file |