Make page selector and HTML cleanup

This commit is contained in:
Pòsweg 2020-08-29 14:31:09 +02:00
parent 890a24dcd2
commit 41e2ffeee0
2 changed files with 136 additions and 43 deletions

View File

@ -36,7 +36,9 @@
} }
/* Form column */
/* Form column */
.form-column{ .form-column{
background-color: <?= $color_2 ?>; background-color: <?= $color_2 ?>;
padding: 10px; padding: 10px;
@ -55,7 +57,8 @@
background-color: <?= $color_main ?> background-color: <?= $color_main ?>
} }
/* Highlighted post (?p=id) */
/* Highlighted post (?p=p) */
.highlighted-post{ .highlighted-post{
background-color: <?= $color_2 ?>; background-color: <?= $color_2 ?>;
@ -88,7 +91,9 @@
color: <?= $color_4 ?>; color: <?= $color_4 ?>;
} }
/* Questions */
/* Questions */
.question-container{ .question-container{
padding: 10px; padding: 10px;
background-color: <?= $color_main ?>; background-color: <?= $color_main ?>;
@ -127,7 +132,25 @@
margin-top: 20px; margin-top: 20px;
} }
/* Footer */
/* Page selector */
.page-selector{
text-align: center;
color: <?= $color_4 ?>;
}
.ps_item{
display: inline-block;
}
.ps_item a{
padding: 5px;
font-weight: bold;
text-decoration: none;
}
/* Footer */
#footer{ #footer{
padding: 10px; padding: 10px;
/*background-color: <?= $color_3 ?>;*/ /*background-color: <?= $color_3 ?>;*/

148
user.php
View File

@ -7,7 +7,7 @@ include("include/settings.php");
$db = new SQLite3('ask.db'); $db = new SQLite3('ask.db');
$p_user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC); $p_user = $db->query("SELECT * FROM users WHERE username = '" . $_GET["q"] . "';")->fetchArray(SQLITE3_ASSOC);
if(!$p_user || !$p_user["id"]){ if(!$p_user || $p_user["id"] == 0){ // Test if user exists or if it is the id 0, anonymous
include("404.php"); include("404.php");
die(); die();
} }
@ -27,10 +27,10 @@ if(isset($_SESSION["uid"])){
<body> <body>
<?php // print_profile($p_user); ?> <?php // print_profile($p_user); ?>
<?php include("include/header.php"); ?> <?php include("include/header.php"); ?>
<div id="parent-container"> <div id="parent-container">
<div id="user-container"> <div id="user-container">
<p id="user-name"><?= $p_user["username"] ?></p> <p id="user-name"><?= $p_user["username"] ?></p>
<p class="user-bio"><?php <p class="user-bio"><?php
// if($p_user["bio"]) echo($p_user["bio"]); // if($p_user["bio"]) echo($p_user["bio"]);
// else echo("This is a sample bio, please change me, this is just to test the layout."); // else echo("This is a sample bio, please change me, this is just to test the layout.");
?></p> ?></p>
@ -121,28 +121,28 @@ if(isset($_GET["p"])){
} }
?> ?>
<div class="columns-container"> <div class="columns-container">
<div class="column form-column"> <div class="column form-column">
<form name="input" action="/action/publish-question.php" method="post"> <form name="input" action="/action/publish-question.php" method="post">
<textarea placeholder="Ask me anything!" id="post-text" name="post-text"></textarea> <textarea placeholder="Ask me anything!" id="post-text" name="post-text"></textarea>
<br/> <br/>
<input type="hidden" name="uid" value="<?= $p_user["id"] ?>"/> <input type="hidden" name="uid" value="<?= $p_user["id"] ?>"/>
<p><input type="checkbox" <?php if(!isset($_SESSION["uid"])) echo("checked disabled"); ?> name="anon"/> Post anonymously</p> <p><input type="checkbox" <?php if(!isset($_SESSION["uid"])) echo("checked disabled"); ?> name="anon"/> Post anonymously</p>
<?php if(isset($errorMsg)) echo "<p>$errorMsg</p>\n"; ?> <?php if(isset($errorMsg)) echo "<p>$errorMsg</p>\n"; ?>
<input type="submit" value="Ask" name="post-submit"/> <input type="submit" value="Ask" name="post-submit"/>
</form> </form>
</div>
</div>
<div class="question-container column"> <div class="question-container column">
<?php <?php
if(!$_GET["page"]) $_GET["page"] = 1;
$p_limit = 10; // TODO: Make the limit variable $p_limit = 10; // TODO: Make the limit variable
$p_offset = $_GET['page'] * $p_limit; // SQLite3 unsertands offsets in units, not pages $p_offset = ($_GET['page'] - 1) * $p_limit; // SQLite3 unsertands offsets in units, not pages
if(!$p_offset || $p_offset <= 0) $p_offset = 0; if(!$p_offset || $p_offset <= 0) $p_offset = 0;
// TODO: Build a mechanism to make off-limits pages show as the last one // TODO: Build a mechanism to make off-limits pages show as the last one
$count = $db->query('SELECT COUNT(*) FROM questions WHERE user = "' . $p_user["id"] . '";')->fetchArray(SQLITE3_ASSOC)['COUNT(*)']; $q_count = $db->query('SELECT COUNT(*) FROM questions WHERE user = "' . $p_user["id"] . '";')->fetchArray(SQLITE3_ASSOC)['COUNT(*)'];
// echo $count;
$u_prep = $db->prepare("SELECT * FROM users WHERE id = :id"); $u_prep = $db->prepare("SELECT * FROM users WHERE id = :id");
$qs = $db->query("SELECT * FROM questions WHERE user = '". $p_user["id"] ."' ORDER BY id DESC LIMIT ". $p_limit ." OFFSET ". $p_offset .";"); $qs = $db->query("SELECT * FROM questions WHERE user = '". $p_user["id"] ."' ORDER BY id DESC LIMIT ". $p_limit ." OFFSET ". $p_offset .";");
@ -158,9 +158,11 @@ if(isset($_GET["p"])){
$u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER); $u_prep->bindValue(":id", $current["by"], SQLITE3_INTEGER);
$q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC); $q_user = $u_prep->execute()->fetchArray(SQLITE3_ASSOC);
echo("<div class='question'>"); echo("\t\t\t");
echo("<div class='question'>\n");
// Question username // Question username
echo("\t\t\t\t");
echo("<p class='question-username'>" ); echo("<p class='question-username'>" );
if($q_user["id"] == 0){ // no link if anonymous if($q_user["id"] == 0){ // no link if anonymous
echo($q_user["name"]); echo($q_user["name"]);
@ -172,45 +174,113 @@ if(isset($_GET["p"])){
echo ("/"); echo ("/");
else else
echo(".php?q="); echo(".php?q=");
echo($q_user["name"] . "'>" . $q_user["name"] ."</a>\n"); echo($q_user["name"] . "'>" . $q_user["name"] ."</a>");
} }
echo("</p>"); echo("</p>\n");
// Actual text body // Actual text body
echo("\t<p class='question-text'>". $current["question"] ."</p>\n"); echo("\t\t\t\t");
echo("<p class='question-text'>". $current["question"] ."</p>\n");
/* Question footer */ /* Question footer */
echo("\t\t\t\t");
echo('<div class="question-footer">'); echo('<div class="question-footer">');
echo("\n");
// Actions // Actions
if($is_current_user){ if($is_current_user){
echo("\t<a href='/action/reply.php?q=".$current["id"]."'>reply</a> "); echo("\t\t\t\t\t");
echo("\t<a href='/action/delete-question.php?q=".$current["id"]."'>delete</a> "); echo("<a href='/action/reply.php?q=".$current["id"]."'>reply</a>\n");
echo("fav ignore "); echo("\t\t\t\t\t");
echo("<a href='/action/delete-question.php?q=".$current["id"]."'>delete</a>\n");
echo("\t\t\t\t\t");
echo("fav\n");
} }
// Time // Time
$time->settimestamp($current["q_date"]); $time->settimestamp($current["q_date"]);
echo("\t\t\t\t\t");
echo('<a href="?p=' . $current["id"] . '">'); echo('<a href="?p=' . $current["id"] . '">');
echo("<p class='question-date'>" . $time->format("Y-m-d h:i:s") . "</p>"); echo("<p class='question-date'>" . $time->format("Y-m-d h:i:s") . "</p>");
echo("</a>"); echo("</a>\n");
echo('</div>'); // Footer echo("\t\t\t\t");
echo("</div>\n"); // Footer
/* Answer */ /* Answer */
if($current["answer"]){ if($current["answer"]){
echo("<div class='answer'>"); echo("\t\t\t\t");
echo("<p class='answer-text'>" . $current["answer"] . "</p>"); echo("<div class='answer'>\n");
$time->settimestamp($current["a_date"]);
echo("<p class='answer-date'>" . $time->format("Y-m-d h:i:s") . "</p>");
echo("</div>");
}
echo("\n\n");
echo("</div>"); // Question echo("\t\t\t\t\t");
echo(nl2br("<p class='answer-text'>" . $current["answer"] . "</p>\n"));
$time->settimestamp($current["a_date"]);
echo("\t\t\t\t\t");
echo("<p class='answer-date'>" . $time->format("Y-m-d h:i:s") . "</p>\n");
echo("\t\t\t\t");
echo("</div>\n");
}
echo("\t\t\t");
echo("</div>\n"); // Question
echo("\n");
} }
/* PAGE SELECTOR */
// ps_* means Page Selector
$p_total = ceil($q_count / $p_limit);
$ps_margin = 2; // Amount of page numbers to show after and before the actual one
// Limit displayed numbers to the existing ones (bound logic)
if($_GET["page"] - $ps_margin < 1) $ps_init = 1;
else if($_GET["page"] + $ps_margin > $p_total) $ps_init = $p_total - $ps_margin * 2;
else $ps_init = $_GET["page"] - $ps_margin;
$ps = "";
$ps .= "<div class='page-selector'>\n";
function ps_item($label, $link){
$ps_item = "";
if($link){
$link_init = "<a href='/user";
if($fancy_urls)
$link_init .= "/" . $_GET["q"] . "?";
else
$link_init .= ".php?q=" . $_GET["q"] . "&";
$link_init = $link_init . "page=" . $link . "'>";
$label = $link_init . $label . "</a>";
}
$ps_item = "\t<div class='ps_item'>" . $label . "</div>\n";
return $ps_item;
}
// "|<" and "<"
if($ps_init > 1) $ps .= ps_item("|<", 1);
if($_GET["page"] > 1) $ps .= ps_item("<", $_GET["page"] - 1);
// Numbers
for($i = $ps_init; $i <= $ps_init + $ps_margin * 2 && $i <= $p_total && $p_total != 1; $i++){
if($i == $_GET["page"]) $ps .= ps_item($i, "");
else $ps .= ps_item($i, $i);
}
// ">" and ">|"
if($_GET["page"] < $p_total) $ps .= ps_item(">", $_GET["page"] + 1);
if($ps_init + $ps_margin * 2 < $p_total) $ps .= ps_item(">|",$p_total);
$ps .= "</div>\n";
echo $ps;
?> ?>
</div> </div>
</div> </div>