gpt4 book ai didi

如果变量不等于 'yes',PHP 函数不显示行

转载 作者:行者123 更新时间:2023-11-29 13:17:14 25 4
gpt4 key购买 nike

我创建了一个 PHP 函数,它从数据库中选择记录并将其显示在表中:

function DashboardTickets($status, $colour, $header_text_colour, $text_colour) {
global $pdo_conn;
global $conn;

global $usertype_user;
global $usertype_admin;
global $usertype_accounts;
global $usertype_support;

$stmt = $pdo_conn->prepare("SELECT * from tickets where deleted = :deleted and status = :status ");
$stmt->execute(array(':deleted' => '', ':status' => $status));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
if(count($records) > 0) {
//set the tickets_found to true
$tickets_found=true;

echo '<tr>
<td colspan="8" bgcolor="#'.$colour.'"><strong><font color="#'.$header_text_colour.'">'.$status.' ('.count($records).')</font></strong></td>
</tr>';
$counter=0;
foreach($records as $result) {
$counter++;

$stmt = $pdo_conn->prepare("SELECT * from ticket_updates where ticket_seq = :ticket_seq ");
$stmt->execute(array(':ticket_seq' => $result["ticketnumber"]));
$records = $stmt->fetchAll(PDO::FETCH_ASSOC);
$ticketupdates=count($records);

if($result["locked_by"] != '') {
$stmt = $pdo_conn->prepare("SELECT * from admin where sequence = :sequence ");
$stmt->execute(array(':sequence' => $result["locked_by"]));
$user = $stmt->fetch();

$padlock = '<a href="/index.php?unlockticket='.$result["ticketnumber"].'"><img src="/includes/images/padlock_closed.png" width="25px" /></a><br>'.substr($user["forename"],0,1).' '.substr($user["surname"],0,1);
} else {
$padlock = '';
}

$stmt = $pdo_conn->prepare("SELECT * from admin where sequence = :sequence ");
$stmt->execute(array(':sequence' => $result["assigned_to"]));
$at_result = $stmt->fetch();

echo '<tr class="notfirst" style="cursor:pointer;" onclick="document.location=\'/helpdesk/reviewtickets.php?seq='.$result["ticketnumber"].'\'">
<td align="center">'.$padlock.'</td>
<td><font color="#'.$text_colour.'"><strong>'.$result["ticketnumber"].'</strong></font></td>
<td><font color="#'.$text_colour.'">'.ContactNameLookup($result["contact"], "Forename Surname").'<br><strong>'.CompanyNameLookup($result["company"]).'</strong></font></td>
<td width="400px"><font color="#'.$text_colour.'"><strong>'.stripslashes(strip_tags($result["subject"])).'</strong><br>'.stripslashes(strip_tags(substr($result["summary"],0,200))).'</font></td>
<td><font color="#'.$text_colour.'"><strong>'.$result["department"].'</strong></font></td>
<td><font color="#'.$text_colour.'">'.$result["opened_by"].'<br>'.$result["datetime"].'</font<</td>
<td><font color="#'.$text_colour.'">'.$result["last_updated_by"].'<br>'.$result["last_modified"].'</font></td>
<td><font color="#'.$text_colour.'">'.$at_result["forename"].' '.$at_result["surname"].'</font></td>
</tr>';
}
}
}

然后在调用该函数时我执行以下操作:

<?php DashboardTickets("Customer Reply", "F36F25", "FFFFFF", "000000") ?>
<?php DashboardTickets("Needs Action", "FF0000", "FFFFFF", "000000") ?>
<?php DashboardTickets("Open", "666666", "FFFFFF", "000000") ?>

我在某些变量中设置了权限

$usertype_user = $_SESSION["usertype_user"];
$usertype_admin = $_SESSION["usertype_admin"];
$usertype_accounts = $_SESSION["usertype_accounts"];
$usertype_support = $_SESSION["usertype_support"];

上述变量为'yes'''

我只想向某些用户显示门票表中的行

例如,如果$usertype_accounts不等于'yes',则不要显示工单中的行其中department = 'Accounts'

最佳答案

有几种方法可以解决这个问题。

1) 在工单结果循环中添加一个条件,当 $usertype_accounts 的值不为“yes”并且记录的部门为“Accounts”时,该条件会跳过记录。

foreach($records as $result) {
if ($usertype_accounts != 'yes' && $result['department'] == 'Accounts') {
continue; // skip to the next result
}
//...

2) 或者(这是更好的解决方案),您可以使查询动态化,以便根据权限变量的值添加某些条件。例如:

$baseSql = "SELECT * from tickets where deleted = :deleted and status = :status";
// Do something like this for all your permission variables
if ($usertype_accounts != 'yes') {
$baseSql .= " and department != 'Accounts'";
}
$pdo_conn->prepare($baseSql);
//...

关于如果变量不等于 'yes',PHP 函数不显示行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21310834/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com