I should preface this with "I am kind of new to web development." So here it is..
I am working on a page that shows all the results in the database along with who posted the data if a user is an admin ,otherwise, it will show only data that the user has posted without the "who posted the data" row, if the user is not an admin.
To check if the user is an admin or not I have the following code:
$drepturi = $conn->query("SELECT *
FROM rights
WHERE ID = '$loggedinuser'")
->fetch_object()
->drepturi;
This looks at a separate database table where I have 2 rows, one with the loggedinuser's name and one stating whether they are an "admin" or "user".
I then have and IF statement followed by an else depending on whether $drepturi (the user rights) are = admin/user:
if (($drepturi) == ("admin")) {
$query = "SELECT * FROM formular ORDER BY $order $sort ";
$result = mysqli_query($conn, $query);
}elseif (($drepturi) == ("user")) {
$query = "SELECT *
FROM formular
WHERE agent ='$loggedinuser'
ORDER BY $order $sort ";
$result = mysqli_query($conn, $query);
} ;
I then show the data in a table that I want to sort differently depending on if the user accessing the data is a "user" or "admin".
The problem I am having is my function won't change depending on the condition.
This is the script i wrote for the function:
if (($drepturi) = ("admin")) {
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
table = document.getElementById('myTable');
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td')[3];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
}else{
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
table = document.getElementById('myTable');
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td')[2];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
}
For some reason the filter only works as if $drepturi is = "admin" even if the user logged in is a normal "user".
EDIT: I will post the entire page in case anyone sees something I do not:
<?php
include_once'header.php';
include_once'includes/dbh.inc.php';
if (isset($_SESSION["useruid"])){
echo "<h1 class='welcomemsg'>Rapoarte pentru: " . $_SESSION["useruid"] . "</h1>";
}
?>
<?php
if(isset($_GET['order'])){
$order = $_GET['order'];
}
else {
$order = 'client';
}
if(isset($_GET['sort'])){
$sort = $_GET['sort'];
}
else {
$sort = 'ASC';
}
$drepturi = $conn->query("SELECT * FROM rights WHERE ID = '$loggedinuser'")->fetch_object()->drepturi;
if (($drepturi) == ("admin")) {
$query = "SELECT * FROM formular ORDER BY $order $sort ";
$result = mysqli_query($conn, $query);
}else {
$query = "SELECT * FROM formular WHERE agent ='$loggedinuser' ORDER BY $order $sort ";
$result = mysqli_query($conn, $query);
}
if($result = mysqli_query($conn, $query)){
if(mysqli_num_rows($result) > 0){
$sort == 'DESC' ? $sort = 'ASC' : $sort = 'DESC';
echo "<input type='text' id='myInput' onkeyup='myFunction()' placeholder='Search for names..'>";
echo "<br><table id='myTable' class='table table-responsive-sm table-hover table-dark'>";
echo "<tbody>";
echo "<tr>";
echo "<th><a href='?order=id&&sort=$sort'>ID</a></th>";
if(($drepturi) == ("admin")){
echo "<th><a href='?order=id&&sort=$sort'>Agent</a></th>";
echo "<th><a href='?order=dataZilei&&sort=$sort'>Data</a></th>";
echo "<th><a href='?order=client&&sort=$sort'>Client</a></th>";
echo "<th><a href='?order=persoanaIntalnita&&sort=$sort'>Persoana Intalnita</a></th>";
echo "<th>Tema Principala</a></th>";
echo "<th><a href='?order=durata&&sort=$sort'>Durata</a></th>";
echo "<th>Mostre Noi</th>";
echo "<th>Aspecte Pro</th>";
echo "<th>Aspecte Contra</th>";
echo "</tr>";
} else{
echo "<th><a href='?order=dataZilei&&sort=$sort'>Data</a></th>";
echo "<th><a href='?order=client&&sort=$sort'>Client</a></th>";
echo "<th><a href='?order=persoanaIntalnita&&sort=$sort'>Persoana Intalnita</a></th>";
echo "<th>Tema Principala</a></th>";
echo "<th><a href='?order=durata&&sort=$sort'>Durata</a></th>";
echo "<th>Mostre Noi</th>";
echo "<th>Aspecte Pro</th>";
echo "<th>Aspecte Contra</th>";
echo "</tr>";}
while($row = mysqli_fetch_array($result)){
echo "<tr>";
echo "<td><a class='idFormular' href='raport.php?id=".$row["id"]."'>". $row["id"]. "</a></td>";
if(($drepturi) == ("admin")){
echo "<td>" . $row["agent"]. "</td>";
echo "<td>" . $row["dataZilei"]. "</td>";
echo "<td>" . $row["client"]. "</td>";
echo "<td>" .$row["persoanaIntalnita"]. "</td>";
echo "<td>" . $row["temaPrincipala"]. "</td>";
echo "<td>" . $row["durata"]. "</td>";
echo "<td>" . $row["mostreNoi"]. "</td>";
echo "<td>" . $row["aspectePro"]. "</td>";
echo "<td>" . $row["aspecteContra"]. "</td>";
echo "</tr>";
}else{
echo "<td>" . $row["dataZilei"]. "</td>";
echo "<td>" . $row["client"]. "</td>";
echo "<td>" .$row["persoanaIntalnita"]. "</td>";
echo "<td>" . $row["temaPrincipala"]. "</td>";
echo "<td>" . $row["durata"]. "</td>";
echo "<td>" . $row["mostreNoi"]. "</td>";
echo "<td>" . $row["aspectePro"]. "</td>";
echo "<td>" . $row["aspecteContra"]. "</td>";
echo "</tr>";
}
}
echo "</tbody>";
echo "</table>";
echo "<br>";
// Free result set
mysqli_free_result($result);
} else{
echo "0 rezultate pentru userul logat";
}
} else{
echo "Unable to establish database connection!" . mysqli_error($conn);
};
echo "<h1>$drepturi<h1>";
?>
<script>
if (($drepturi) == ("admin")) {
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
table = document.getElementById('myTable');
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td')[3];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
}else{
function myFunction() {
var input, filter, table, tr, td, i, txtValue;
input = document.getElementById('myInput');
filter = input.value.toUpperCase();
table = document.getElementById('myTable');
tr = table.getElementsByTagName('tr');
for (i = 0; i < tr.length; i++) {
td = tr[i].getElementsByTagName('td')[1];
if (td) {
txtValue = td.textContent || td.innerText;
if (txtValue.toUpperCase().indexOf(filter) > -1) {
tr[i].style.display = '';
} else {
tr[i].style.display = 'none';
}
}
}
}
}</script>
<?php
include_once'footer.php';
?>