[英]Run a different function if the first condition is not met
我應該以“我對 web 開發有點陌生”作為開頭。 所以在這里..
我正在處理一個頁面,該頁面顯示數據庫中的所有結果以及誰發布了數據(如果用戶是管理員),否則,它將僅顯示用戶發布的數據而沒有“誰發布數據”行,如果用戶不是管理員。
要檢查用戶是否是管理員,我有以下代碼:
$drepturi = $conn->query("SELECT *
FROM rights
WHERE ID = '$loggedinuser'")
->fetch_object()
->drepturi;
這會查看一個單獨的數據庫表,其中我有 2 行,其中一行帶有登錄用戶的名稱,另一行說明他們是“管理員”還是“用戶”。
然后我有和 IF 語句后跟一個 else 取決於 $drepturi (用戶權限)是否 = 管理員/用戶:
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);
} ;
然后,根據訪問數據的用戶是"user"還是"admin" ,我在表中顯示我想要以不同方式排序的數據。
我遇到的問題是我的 function 不會根據情況而改變。
這是我為 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';
}
}
}
}
}
出於某種原因,即使登錄的用戶是普通的“用戶”,過濾器也只能像 $drepturi is = "admin" 一樣工作。
編輯:如果有人看到我沒有看到的東西,我會發布整個頁面:
<?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';
?>
正如 str7 所說,答案是您不能在 javascript 中使用 php 變量。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.