I have two methods in a class. The first is
save_comment()
It basically just inserts comment into db and then after its successfully another method is called to load some comments and the method is named
load_comments()
This is the code for better understanding. Unnecessary part had been commented out.
function save_comment()
{
//$this->Functions->debug();
//request token
//$token = $this->Functions->clean($_GET["token"]);
//check for csrf
// $check = $this->Functions->csrf_check($token);
/*if ($check === false) {
$this->Functions->api_response("Invalid Request!!!", 0, "csrf");
}*/
$pid = $this->Functions->clean($_GET["pid"]);
$offset = $this->Functions->clean("", $_GET["offset"], "");
//comment
$comment = $this->Functions->clean($_GET["user_comment"]);
//check login status
/* if (!isset($_SESSION["user"])) {
$this->Functions->api_response(
"You must login to make comments !!!.",
0,
"csrf"
);
}*/
//check if comment is less than 5 charcaters.
/* if (strlen($comment) < 5) {
$this->Functions->api_response(
"Your Comment is too short !!!.",
0,
"csrf"
);
}*/
try {
$user = $_SESSION["user"];
$params = ["sss", "?,?,?", [$pid, $user, $comment]];
$save_comment = $this->CustomDB->insert(
"comments",
"pid,user,comment",
$params
);
if ($save_comment->affected_rows == 1) {
$offset = 0;
$data = $this->load_comments($pid, $offset);
echo $data;//this returns empty string
$this->Functions->api_response(
"Thanks!! Your Comment has been submitted 🤗 ",
1,
"csrf",
$data
);
} else {
throw new Exception("Something went wrong. Try again!!!");
}
} catch (Exception $e) {
//try
$msg = $e->getMessage();
$this->Functions->api_response($msg, 0, "csrf");
}
} //comment()
Now the second method is
function load_comments($pid, $offset)
{
$this->Functions->debug();
//get comments so we can update at frontend
$params = ["si", [$pid, $offset]];
//fetch comments
$fetch = $this->CustomDB->query_custom(
"SELECT * FROM comments WHERE pid=? ORDER BY id DESC LIMIT 10 OFFSET?",
$params
);
$fetch = $fetch["result"];
$html = "";
//$html;
while ($row = $fetch->fetch_assoc()) {
$name = $row["user"];
$comment = $row["comment"];
$html .=
'<div class="columns is-mobile display"><div style="border-radius:30px 30px 30px 30px;" class="column is-full box"><p class="subtitle is-6 mb-3"><em>' .
$comment .
'</em></p><span class="bold ml-2">' .
$name .
"</span></div></div><br>";
}
return $html;
}
I can't really figure why it shouldn't work as expected