Cleanup "/user.php"

This commit is contained in:
Pòsweg 2020-08-27 22:57:05 +02:00
parent 82c840ce4a
commit 890a24dcd2
1 changed files with 72 additions and 20 deletions

View File

@ -1,4 +1,5 @@
<?php <?php
session_start(); session_start();
include("include/settings.php"); include("include/settings.php");
@ -49,11 +50,16 @@ if(isset($_SESSION["uid"])){
</div> </div>
<?php <?php
/** QUESTION HIGHLIGHT **/
if(isset($_GET["p"])){ if(isset($_GET["p"])){
//Get info about the question
$h_question = $db->query("SELECT * FROM questions WHERE id = " . $_GET["p"] . ";")->fetchArray(SQLITE3_ASSOC); $h_question = $db->query("SELECT * FROM questions WHERE id = " . $_GET["p"] . ";")->fetchArray(SQLITE3_ASSOC);
$h_user = $db->query("SELECT * FROM users WHERE id = " . $h_question["by"] . ";")->fetchArray(SQLITE3_ASSOC); $h_user = $db->query("SELECT * FROM users WHERE id = " . $h_question["by"] . ";")->fetchArray(SQLITE3_ASSOC);
if($p_user["id"] == $h_question["user"]){ if($p_user["id"] == $h_question["user"]){ // Only show question that are for this user
echo('<div class="highlighted-post">'); echo('<div class="highlighted-post">');
@ -61,27 +67,33 @@ if(isset($_GET["p"])){
$time = new DateTime("@0"); $time = new DateTime("@0");
// Title
echo("<p class='question-username'>"); echo("<p class='question-username'>");
if($h_user["id"] == 0){ if($h_user["id"] == 0){ // no link if anonymous
echo($h_user["name"]); echo($h_user["name"]);
} }
else if($fancy_urls){
echo("<a href='/user/" . $h_user["name"] . "'>" . $h_user["name"] ."</a>");
}
else{ else{
echo("<a href='/user.php?q=" . $h_user["name"] . "'>" . $h_user["name"] ."</a>"); echo("<a href='/user");
if($fancy_urls)
echo ("/");
else
echo(".php?q=");
echo($h_user["name"] . "'>" . $h_user["name"] ."</a>\n");
} }
echo(" asked:</p>"); echo(" asked:</p>");
// Actual question text
echo("\t<p class='question-text'>". $h_question["question"] ."</p>\n"); echo("\t<p class='question-text'>". $h_question["question"] ."</p>\n");
// Footer
if($is_h_question_user){ if($is_h_question_user){
echo("\t<a href='/action/reply.php?q=".$h_question["id"]."'>reply</a> "); echo("\t<a href='/action/reply.php?q=".$h_question["id"]."'>reply</a> ");
echo("\t<a href='/action/delete-question.php?q=".$h_question["id"]."'>delete</a> "); echo("\t<a href='/action/delete-question.php?q=".$h_question["id"]."'>delete</a> ");
echo("fav ignore "); echo("fav ignore ");
} }
// Time // Time
$time->settimestamp($h_question["q_date"]); $time->settimestamp($h_question["q_date"]);
echo('<p class="question-date">'); echo('<p class="question-date">');
echo('<a href="?p=' . $h_question["id"] . '">' . $time->format('Y-m-d h:i:s') . '</a>'); echo('<a href="?p=' . $h_question["id"] . '">' . $time->format('Y-m-d h:i:s') . '</a>');
@ -89,17 +101,22 @@ if(isset($_GET["p"])){
echo('</div>'); // question-body echo('</div>'); // question-body
/* Answer */
if($h_question["answer"]){ if($h_question["answer"]){
echo("<div class='answer'>"); echo("<div class='answer'>");
echo('<p class="title">' . $p_user["name"] . ' answered:'); echo('<p class="title">' . $p_user["name"] . ' answered:');
echo("<p class='answer-text'>" . $h_question["answer"] . "</p>"); echo("<p class='answer-text'>" . $h_question["answer"] . "</p>");
$time->settimestamp($h_question["a_date"]); $time->settimestamp($h_question["a_date"]);
echo("<p class='answer-date'>" . $time->format("Y-m-d h:i:s") . "</p>"); echo("<p class='answer-date'>" . $time->format("Y-m-d h:i:s") . "</p>");
echo("</div>"); echo("</div>");
} }
echo("\n\n");
echo('</div>'); echo('</div>'); // highlighted-post
} }
} }
?> ?>
@ -118,35 +135,70 @@ if(isset($_GET["p"])){
</div> </div>
<div class="question-container column"> <div class="question-container column">
<?php <?php
$p_limit = 10; // TODO: Make the limit variable
$p_offset = $_GET['page'] * $p_limit; // SQLite3 unsertands offsets in units, not pages
if(!$p_offset || $p_offset <= 0) $p_offset = 0;
// 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(*)'];
// 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 0, 10;"); $qs = $db->query("SELECT * FROM questions WHERE user = '". $p_user["id"] ."' ORDER BY id DESC LIMIT ". $p_limit ." OFFSET ". $p_offset .";");
/** QUESTIONS MAIN LOOP **/
$time = new DateTime("@0"); $time = new DateTime("@0");
while($current = $qs->fetchArray(SQLITE3_ASSOC)){ while($current = $qs->fetchArray(SQLITE3_ASSOC)){
// Execute prepared statement
// Execute prepared statement
$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("<div class='question'>");
if($q_user["id"] == 0) echo("<p class='question-username'>" . $q_user["name"] ."</p>"); // Question username
else if($fancy_urls) echo("<p class='question-username'><a href='/user/" . $q_user["name"] . "'>" . $q_user["name"] ."</a></p>\n"); echo("<p class='question-username'>" );
else echo("<p class='question-username'><a href='/user.php?q=" . $q_user["name"] . "'>" . $q_user["name"] ."</a></p>\n"); if($q_user["id"] == 0){ // no link if anonymous
echo($q_user["name"]);
}
else{
echo("<a href='/user");
if($fancy_urls)
echo ("/");
else
echo(".php?q=");
echo($q_user["name"] . "'>" . $q_user["name"] ."</a>\n");
}
echo("</p>");
// Actual text body
echo("\t<p class='question-text'>". $current["question"] ."</p>\n"); echo("\t<p class='question-text'>". $current["question"] ."</p>\n");
/* Question footer */
echo('<div class="question-footer">'); echo('<div class="question-footer">');
// Actions
if($is_current_user){ if($is_current_user){
echo("\t<a href='/action/reply.php?q=".$current["id"]."'>reply</a> "); echo("\t<a href='/action/reply.php?q=".$current["id"]."'>reply</a> ");
echo("\t<a href='/action/delete-question.php?q=".$current["id"]."'>delete</a> "); echo("\t<a href='/action/delete-question.php?q=".$current["id"]."'>delete</a> ");
echo("fav ignore "); echo("fav ignore ");
} }
// Time
// Time
$time->settimestamp($current["q_date"]); $time->settimestamp($current["q_date"]);
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>");
echo('</div>');
echo('</div>'); // Footer
/* Answer */
if($current["answer"]){ if($current["answer"]){
echo("<div class='answer'>"); echo("<div class='answer'>");
@ -157,7 +209,7 @@ if(isset($_GET["p"])){
} }
echo("\n\n"); echo("\n\n");
echo("</div>"); echo("</div>"); // Question
} }
?> ?>
</div> </div>