<?php
ob_start();

include_once 'payments.php';

$userobj      = new adminuser();
$adminUserId  = $_SESSION['ADMINUSERID'] ?? 0;

$mysqli = db_connect(); // Make sure this function returns a valid mysqli connection

$status         = isset($_GET['status']) ? trim($_GET['status']) : '';
$page           = isset($_GET['page']) ? intval($_GET['page']) : 0;
$affid          = isset($_GET['affid']) ? intval($_GET['affid']) : 0;
$loginflag      = isset($_GET['loginflag']) ? trim($_GET['loginflag']) : '';
$mode           = isset($_GET['mode']) ? trim($_GET['mode']) : '';
$sortby         = $_GET['sortby'] ?? '';
$affiliateorder = $_GET['affiliateorder'] ?? '';
$regdateorder   = $_GET['regdateorder'] ?? '';
$aprovedorder   = $_GET['aprovedorder'] ?? '';
$pendingorder   = $_GET['pendingorder'] ?? '';
$paidorder      = $_GET['paidorder'] ?? '';

$sortingtable = $_SESSION['SORTINGTABLE'] ?? '';

if (empty($sortingtable)) {
    $sql = "CREATE TEMPORARY TABLE IF NOT EXISTS admin_affil_heap (
        heap_id BIGINT NOT NULL AUTO_INCREMENT PRIMARY KEY,
        affiliate_id BIGINT NOT NULL,
        affiliate_status VARCHAR(250) NOT NULL,
        heap_name VARCHAR(250) NOT NULL,
        heap_regdate DATE NOT NULL,
        heap_approved BIGINT NOT NULL,
        heap_pending BIGINT NOT NULL,
        heap_paid BIGINT NOT NULL
    ) ENGINE = MEMORY";

    $mysqli->query($sql);
    $_SESSION['SORTINGTABLE'] = "existing";
}

if (empty($sortby)) {
    $mysqli->query("DELETE FROM admin_affil_heap");
}

if (empty($status)) {
    $status = $_SESSION['SESSIONSTATUS'] ?? '';
} else {
    $_SESSION['SESSIONSTATUS'] = $status;
}

if (empty($page)) {
    $page = $partners->getpage(); // This should be defined in your system
}

# Handling affiliate actions
if (in_array($mode, ['ViewProfile', 'ChangePassword', 'Reject', 'Remove', 'Approve', 'TierGroup'])) {
    $h = 400;
    $w = 450;

    if ($mode === 'ViewProfile') {
        $pageurl = "viewprofile_affiliate.php?";
    } elseif (in_array($mode, ['Remove', 'Reject'])) {
        $pageurl = "remove_affiliate.php?loginflag=" . urlencode($loginflag) . "&mode=" . urlencode($mode);
    } elseif ($mode === 'Approve') {
        $pageurl = "affiliate_approve.php?page=$page&status=" . urlencode($status) . "&sortby=" . urlencode($sortby);
    } elseif ($mode === 'TierGroup') {
        $pageurl = "affiliate_approve.php?mode=TierGroup&page=$page&status=" . urlencode($status) . "&sortby=" . urlencode($sortby);
    } else {
        $pageurl = "change_pass.php?loginflag=" . urlencode($loginflag);
    }
    ?>
    <script type="text/javascript">
        function help() {
            window.open('<?= $pageurl ?>&id=<?= $affid ?>', 'new', 'height=<?= $h ?>,width=<?= $w ?>,scrollbars=yes');
        }
        help();
    </script>
  <?php
    }

    // search based on status
    switch ($status) {

        case 'waiting': // waiting affiliates
            $sql = "SELECT *, DATE_FORMAT(affiliate_date, '%d-%b-%Y') AS d 
                    FROM partners_affiliate 
                    WHERE affiliate_status = 'waiting'";
            if (!empty($affiliatename)) {
                $affiliatename1 = mysqli_real_escape_string($conn, $affiliatename);
                $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '%$affiliatename1%' 
                          OR affiliate_company LIKE '%$affiliatename1%')";
            }
            break;

        case 'approve': // approved affiliates
            $sql = "SELECT *, DATE_FORMAT(affiliate_date, '%d-%b-%Y') AS d 
                    FROM partners_affiliate 
                    WHERE affiliate_status = 'approved'";
            if (!empty($affiliatename)) {
                $affiliatename1 = mysqli_real_escape_string($conn, $affiliatename);
                $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '%$affiliatename1%' 
                          OR affiliate_company LIKE '%$affiliatename1%')";
            }
            break;

        case 'pending': // affiliates with pending transactions
            $sql = "SELECT DISTINCT a.affiliate_id, a.affiliate_firstname, a.affiliate_lastname, 
                           a.affiliate_company, a.affiliate_status, a.affiliate_date, 
                           DATE_FORMAT(a.affiliate_date, '%d-%b-%Y') AS d 
                    FROM partners_affiliate a
                    JOIN partners_joinpgm c ON c.joinpgm_affiliateid = a.affiliate_id
                    JOIN partners_transaction e ON e.transaction_joinpgmid = c.joinpgm_id
                    WHERE e.transaction_status = 'pending'";
            if (!empty($affiliatename)) {
                $affiliatename1 = mysqli_real_escape_string($conn, $affiliatename);
                $sql .= " AND (CONCAT(a.affiliate_firstname, ' ', a.affiliate_lastname) LIKE '%$affiliatename1%' 
                          OR a.affiliate_company LIKE '%$affiliatename1%')";
            }
            break;

        case 'suspend': // suspended affiliates
            $sql = "SELECT *, DATE_FORMAT(affiliate_date, '%d-%b-%Y') AS d 
                    FROM partners_affiliate 
                    WHERE affiliate_status = 'suspend'";
            if (!empty($affiliatename)) {
                $affiliatename1 = mysqli_real_escape_string($conn, $affiliatename);
                $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '%$affiliatename1%' 
                          OR affiliate_company LIKE '%$affiliatename1%')";
            }
            break;

        case 'all':
        default: // all affiliates
            $sql = "SELECT *, DATE_FORMAT(affiliate_date, '%d-%b-%Y') AS d 
                    FROM partners_affiliate";
            if (!empty($affiliatename)) {
                $affiliatename1 = mysqli_real_escape_string($conn, $affiliatename);
                $sql .= " WHERE CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '%$affiliatename1%' 
                          OR affiliate_company LIKE '%$affiliatename1%'";
            }
            break;
    }

 <?php
// Assuming $conn is your valid mysqli connection
if (!isset($page)) $page = 1;
if (!isset($lines)) $lines = 20;
$start = ($page - 1) * $lines;
$affiliatename1 = !empty($affiliatename) ? '%' . $conn->real_escape_string($affiliatename) . '%' : null;

// Base SQL builder
switch ($status) {
    case 'waiting':
        $sql = "SELECT *, DATE_FORMAT(affiliate_date,'%d-%b-%Y') as d 
                FROM partners_affiliate 
                WHERE affiliate_status = 'waiting'";
        if ($affiliatename1) {
            $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '$affiliatename1' 
                     OR affiliate_company LIKE '$affiliatename1')";
        }
        break;

    case 'approve':
        $sql = "SELECT *, DATE_FORMAT(affiliate_date,'%d-%b-%Y') as d 
                FROM partners_affiliate 
                WHERE affiliate_status = 'approved'";
        if ($affiliatename1) {
            $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '$affiliatename1' 
                     OR affiliate_company LIKE '$affiliatename1')";
        }
        break;

    case 'pending':
        $sql = "SELECT DISTINCT a.affiliate_id, a.affiliate_firstname, a.affiliate_lastname, 
                       a.affiliate_company, a.affiliate_status, a.affiliate_date,
                       DATE_FORMAT(a.affiliate_date,'%d-%b-%Y') as d
                FROM partners_affiliate a
                JOIN partners_joinpgm c ON c.joinpgm_affiliateid = a.affiliate_id
                JOIN partners_transaction e ON e.transaction_joinpgmid = c.joinpgm_id
                WHERE e.transaction_status = 'pending'";
        if ($affiliatename1) {
            $sql .= " AND (CONCAT(a.affiliate_firstname, ' ', a.affiliate_lastname) LIKE '$affiliatename1' 
                     OR a.affiliate_company LIKE '$affiliatename1')";
        }
        break;

    case 'suspend':
        $sql = "SELECT *, DATE_FORMAT(affiliate_date,'%d-%b-%Y') as d 
                FROM partners_affiliate 
                WHERE affiliate_status = 'suspend'";
        if ($affiliatename1) {
            $sql .= " AND (CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '$affiliatename1' 
                     OR affiliate_company LIKE '$affiliatename1')";
        }
        break;

    case 'all':
    default:
        $sql = "SELECT *, DATE_FORMAT(affiliate_date,'%d-%b-%Y') as d FROM partners_affiliate";
        if ($affiliatename1) {
            $sql .= " WHERE CONCAT(affiliate_firstname, ' ', affiliate_lastname) LIKE '$affiliatename1' 
                      OR affiliate_company LIKE '$affiliatename1'";
        }
        break;
}

$pgsql = $sql;
$sql .= " LIMIT $start, $lines";
$ret = $conn->query($sql);

if ($ret && $ret->num_rows > 0) {
    // Reset sort indicators
    $affiliatesort = $dateesort = $pendingsort = $paidsort = $approvedsort = 
        "<img src='../images/normal.gif' width='10' height='10' alt='' border='0' />";

    // Set current sort image and toggle direction
    switch ($sortby) {
        case 'affiliate':
            $affiliatesort = ($affiliateorder == 'asc') ? 
                "<img src='../images/up.gif' width='10' height='10' alt='' border='0' />" : 
                "<img src='../images/dawn.gif' width='10' height='10' alt='' border='0' />";
            $affiliateorder = ($affiliateorder == 'asc') ? 'desc' : 'asc';
            break;

        case 'regdate':
            $dateesort = ($regdateorder == 'asc') ? 
                "<img src='../images/up.gif' width='10' height='10' alt='' border='0' />" : 
                "<img src='../images/dawn.gif' width='10' height='10' alt='' border='0' />";
            $regdateorder = ($regdateorder == 'asc') ? 'desc' : 'asc';
            break;

        case 'aproved':
            $approvedsort = ($aprovedorder == 'asc') ? 
                "<img src='../images/up.gif' width='10' height='10' alt='' border='0' />" : 
                "<img src='../images/dawn.gif' width='10' height='10' alt='' border='0' />";
            $aprovedorder = ($aprovedorder == 'asc') ? 'desc' : 'asc';
            break;

        case 'pending':
            $pendingsort = ($pendingorder == 'asc') ? 
                "<img src='../images/up.gif' width='10' height='10' alt='' border='0' />" : 
                "<img src='../images/dawn.gif' width='10' height='10' alt='' border='0' />";
            $pendingorder = ($pendingorder == 'asc') ? 'desc' : 'asc';
            break;

        case 'paid':
            $paidsort = ($paidorder == 'asc') ? 
                "<img src='../images/up.gif' width='10' height='10' alt='' border='0' />" : 
                "<img src='../images/dawn.gif' width='10' height='10' alt='' border='0' />";
            $paidorder = ($paidorder == 'asc') ? 'desc' : 'asc';
            break;
    }
}

?>
<table border='0' cellpadding="0" cellspacing="0" width="95%" class="tablebdr">
<tr>
<td class="tdhead" align="center" colspan="2" >Status</td>
<td width="31%" class="tdhead" align="left">
<a href="index.php?Act=affiliates&status=<?= $status ?>&page=<?= $page ?>&sortby=affiliate&affiliateorder=<?= $affiliateorder ?>"><?= $affiliatesort ?>Affiliate
</a></td>
<td width="11%" class="tdhead" align="center"><a href="index.php?Act=affiliates&status=<?= $status ?>&page=<?= $page ?>&sortby=regdate&regdateorder=<?= $regdateorder ?>"><?= $dateesort ?>Registered</a></td>

<td width="9%" class="tdhead" align="center"><a href="index.php?Act=affiliates&status=<?= $status ?>&page=<?= $page ?>&sortby=pending&pendingorder=<?= $pendingorder ?>"><?= $pendingsort ?>Pending</a></td>
<td width="9%" class="tdhead" align="center"><a href="index.php?Act=affiliates&status=<?= $status ?>&page=<?= $page ?>&sortby=paid&paidorder=<?= $paidorder ?>"><?= $paidsort ?>Balance</a></td>
<td width="22%" class="tdhead" align="left">Action</td>
<td width="6%" class="tdhead" align="center">Notes</td>
</tr>

<?php
# IF SORTING REQUEST EXISTS adding sorted records
$sortby = $_GET['sortby'];
if ($sortby != "") {
    switch ($sortby) {

        case 'affiliate':
            $sql_sort = "SELECT *, DATE_FORMAT(heap_regdate,'%d-%b-%Y') d
                         FROM admin_affil_heap ORDER BY heap_name $affiliateorder";
            $ret = mysqli_query($conn, $sql_sort);
            break;

        case 'regdate':
            $sql_sort = "SELECT *, DATE_FORMAT(heap_regdate,'%d-%b-%Y') d
                         FROM admin_affil_heap ORDER BY heap_regdate $regdateorder";
            $ret = mysqli_query($conn, $sql_sort);
            break;

        case 'aproved':
            $sql_sort = "SELECT *, DATE_FORMAT(heap_regdate,'%d-%b-%Y') d
                         FROM admin_affil_heap ORDER BY heap_approved $aprovedorder";
            $ret = mysqli_query($conn, $sql_sort);
            break;

        case 'pending':
            $sql_sort = "SELECT *, DATE_FORMAT(heap_regdate,'%d-%b-%Y') d
                         FROM admin_affil_heap ORDER BY heap_pending $pendingorder";
            $ret = mysqli_query($conn, $sql_sort);
            break;

        case 'paid':
            $sql_sort = "SELECT *, DATE_FORMAT(heap_regdate,'%d-%b-%Y') d
                         FROM admin_affil_heap ORDER BY heap_paid $paidorder";
            $ret = mysqli_query($conn, $sql_sort);
            break;
    }

    echo mysqli_error($conn);

    while ($row = mysqli_fetch_object($ret)) {

        $total = GetPaymentDetails($row->affiliate_id, 2);  //getting pending, approved, paid amounts from GetPayments.php
        $total = explode('~', $total);
        $status1 = $row->affiliate_status;                 //setting picture

        //getting records from table
        $affiliate_sql = "SELECT * FROM affiliate_pay WHERE pay_affiliateid='$row->affiliate_id'";
        $affiliate_ret = mysqli_query($conn, $affiliate_sql);

        //checking for each record
        if (mysqli_num_rows($affiliate_ret) > 0) {
            $affiliate_row = mysqli_fetch_object($affiliate_ret);
            $affiliate_pay_amount = $affiliate_row->pay_amount;
        }

              //-----------------------------------------------//

$name         = stripslashes($row->heap_name);
$regdate      = $row->d;
$approved     = $total[0];
$pending      = $total[1];
$paid         = $affiliate_pay_amount;
$heap_regdate = $row->heap_regdate;

$heap_count++;
?>
<tr>
     <td width="6%" height="25" align="center"  >
     <?php
        if ($total[1] > 0) //checking for pending transactions
        {
            ?>
            <img alt=" " border='0' height="<?= $icon_height ?>" width="<?= $icon_width ?>" src="../images/pending.gif" />
            <?php
        }
        ?>
    </td>
    <td width="6%" height="25" align="middle"  >
        <img alt="" border='0' height="15" src="../images/<?= $status1 ?>.gif"  width="15" />
    </td>
    <td width="31%" height="25" align="left">
        <a href="#" onclick="viewLink(<?= $row->affiliate_id ?>)"><?= $name ?></a>
    </td>
    <td width="11%" height="25" align="center"><?= $regdate ?></td>
    <td width="9%" height="25" align="center"><?= $currSymbol ?><?= round($pending, 2) ?></td>
    <td width="9%" height="25" align="center"><?= $currSymbol ?><?= round($paid, 2) ?></td>
    <td width="22%" height="25" align="left" >
    <?php

      /******setting affid+action selected*******/

        $Approve        = $row->affiliate_id . "~Approve";
        $Reject         = $row->affiliate_id . "~Reject";
        $Remove         = $row->affiliate_id . "~Remove";
        $ChangePassword = $row->affiliate_id . "~ChangePassword";
        $ViewProfile    = $row->affiliate_id . "~ViewProfile";
        $Suspend        = $row->affiliate_id . "~Suspend";
        $adjust         = $row->affiliate_id . "~adjust";
        $payment        = $row->affiliate_id . "~payment";
        $transaction    = $row->affiliate_id . "~transaction";
        $TierGroup      = $row->affiliate_id . "~TierGroup";
     /*******************************************/
    ?>
    <form name="f<?= $row->affiliate_id ?>" action="affiliate_actions.php?page=<?= $page ?>&amp;sortby=<?= $sortby ?>" method="post">
    <select name="selaction" onchange="document.f<?= $row->affiliate_id ?>.submit()">
        <option value="Select Action">Select Action</option>

<?php

/*************selecting action depending on status****************/

        if ($row->affiliate_status == 'waiting' || $row->affiliate_status == 'suspend') {
            //Checks if the Admin has permission to access the link
            if ($userobj->GetAdminUserLink('Approve Affiliate', $adminUserId, 2)) { ?> 
                <option value="<?= $Approve ?>">Approve</option>
            <?php } 
        }

        //show this option always so that the admin can reject them anytime
        if ($userobj->GetAdminUserLink('Reject Affiliate', $adminUserId, 2)) { ?> 
            <option value="<?= $Reject ?>">Reject</option>
        <?php } 

        if ($row->affiliate_status == 'suspend') {
            if ($userobj->GetAdminUserLink('Remove Affiliate', $adminUserId, 2)) { ?> 
                <option value="<?= $Remove ?>">Remove</option>
            <?php } 
        }

        if ($userobj->GetAdminUserLink('Change Password of Affiliate', $adminUserId, 2)) { ?> 
            <option value="<?= $ChangePassword ?>">ChangePassword</option>
        <?php } ?>
        
        <option value="<?= $ViewProfile ?>">ViewProfile</option>
        
        <?php if ($userobj->GetAdminUserLink('Payment History', $adminUserId, 2)) { ?> 
            <option value="<?= $payment ?>">Payment History</option>
        <?php } 

        if ($userobj->GetAdminUserLink('Transaction', $adminUserId, 2)) { ?> 
            <option value="<?= $transaction ?>">Transaction</option>
        <?php } 
    
        if ($row->affiliate_status == 'approved' || $row->affiliate_status == 'pending' || $row->affiliate_status == 'empty') {
            if ($userobj->GetAdminUserLink('Suspend Affiliate', $adminUserId, 2)) { ?> 
                <option value="<?= $Suspend ?>">Suspend</option>
            <?php } 
        }

        if ($userobj->GetAdminUserLink('Adjust Money', $adminUserId, 2)) { ?> 
            <option value="<?= $adjust ?>">AdjustMoney</option>
        <?php }  

        if ($userobj->GetAdminUserLink('Set Commission Group', $adminUserId, 2)) { ?> 
            <option value="<?= $TierGroup ?>">Set Commission Group</option>
        <?php }  

/******************end selecting action********************************/

?>
        </select>
        </form> 
    </td>
    <td width="6%" height="25" align="center">
        <?php
        //Show login option only if the user has permission
        if ($userobj->GetAdminUserLink('Affiliate Login', $adminUserId, 2)) { ?> 
            <a href="test1.php?aid=<?= $row->affiliate_id ?>">Log In</a>
        <?php } ?>
    </td>
</tr>

<?php
    } // end while

}         /***************************************** SORTED DISPLAY ENDS HERE *********************/
else
{
    /***************************cl0se adding records***************************/
?>


<?php
$heap_count = 1;

while ($row = mysqli_fetch_object($ret)) {

    $affiliate_id = $row->affiliate_id;
    $total = GetPaymentDetails($row->affiliate_id, 2);
    $total = explode('~', $total);
    $status1 = $row->affiliate_status;

    // Fetch payment info
    $affiliate_sql = "SELECT * FROM affiliate_pay WHERE pay_affiliateid = ?";
    $stmt = $conn->prepare($affiliate_sql);
    $stmt->bind_param('i', $row->affiliate_id);
    $stmt->execute();
    $affiliate_ret = $stmt->get_result();

    $affiliate_pay_amount = 0;
    if ($affiliate_ret->num_rows > 0) {
        $affiliate_row = $affiliate_ret->fetch_object();
        $affiliate_pay_amount = $affiliate_row->pay_amount;
    }

    $name = stripslashes($row->affiliate_company);
    $regdate = $row->d;
    $approved = $total[0];
    $pending = $total[1];
    $paid = $affiliate_pay_amount;
    $heap_regdate = $row->affiliate_date;

    $heap_sql = "INSERT INTO admin_affil_heap 
        (heap_id, affiliate_id, affiliate_status, heap_name, heap_regdate, heap_approved, heap_pending, heap_paid) 
        VALUES (?, ?, ?, ?, ?, ?, ?, ?)";
    $stmt_heap = $conn->prepare($heap_sql);
    $stmt_heap->bind_param(
        'iisssddd',
        $heap_count,
        $affiliate_id,
        $status1,
        $name,
        $heap_regdate,
        $approved,
        $pending,
        $paid
    );
    $stmt_heap->execute();

    $heap_count++;
?>
<tr>
    <td width="6%" height="25" align="center">
        <?php if ($total[1] > 0) { ?>
            <img alt=" " border="0" height="15" width="15" src="../images/pending.gif" />
        <?php } ?>
    </td>
    <td width="6%" height="25" align="center">
        <img alt="" border="0" height="<?= $icon_height ?>" width="<?= $icon_width ?>" src="../images/<?= $status1 ?>.gif" />
    </td>
    <td width="31%" height="25" align="left">
        <a href="#" onclick="viewLink(<?= $row->affiliate_id ?>)"><?= $name ?></a>
    </td>
    <td width="11%" height="25" align="center"><?= $regdate ?></td>
    <td width="9%" height="25" align="center"><?= $currSymbol ?><?= round($pending, 2) ?></td>
    <td width="9%" height="25" align="center"><?= $currSymbol ?><?= round($paid, 2) ?></td>
    <td width="22%" height="25" align="left">
        <?php
        $Approve = $row->affiliate_id . "~Approve";
        $Reject = $row->affiliate_id . "~Reject";
        $Remove = $row->affiliate_id . "~Remove";
        $ChangePassword = $row->affiliate_id . "~ChangePassword";
        $ViewProfile = $row->affiliate_id . "~ViewProfile";
        $Suspend = $row->affiliate_id . "~Suspend";
        $adjust = $row->affiliate_id . "~adjust";
        $payment = $row->affiliate_id . "~payment";
        $transaction = $row->affiliate_id . "~transaction";
        $TierGroup = $row->affiliate_id . "~TierGroup";
        ?>
        <form name="f<?= $row->affiliate_id ?>" action="affiliate_actions.php?page=<?= $page ?>&amp;sortby=<?= $sortby ?>" method="post">
            <select name="selaction" onchange="document.f<?= $row->affiliate_id ?>.submit()">
                <option value="Select Action">Select Action</option>
                <?php
                if ($row->affiliate_status == 'waiting' || $row->affiliate_status == 'suspend') {
                    if ($userobj->GetAdminUserLink('Approve Affiliate', $adminUserId, 2)) {
                        echo "<option value=\"$Approve\">Approve</option>";
                    }
                }

                if ($userobj->GetAdminUserLink('Reject Affiliate', $adminUserId, 2)) {
                    echo "<option value=\"$Reject\">Reject</option>";
                }

                if ($row->affiliate_status == 'suspend') {
                    if ($userobj->GetAdminUserLink('Suspend Affiliate', $adminUserId, 2)) {
                        echo "<option value=\"$Remove\">Remove</option>";
                    }
                }

                if ($userobj->GetAdminUserLink('Change Password of Affiliate', $adminUserId, 2)) {
                    echo "<option value=\"$ChangePassword\">ChangePassword</option>";
                }
                ?>
                <option value="<?= $ViewProfile ?>">ViewProfile</option>
                <?php
                if ($userobj->GetAdminUserLink('Payment History', $adminUserId, 2)) {
                    echo "<option value=\"$payment\">Payment History</option>";
                }

                if ($userobj->GetAdminUserLink('Transaction', $adminUserId, 2)) {
                    echo "<option value=\"$transaction\">Transaction</option>";
                }

                if (
                    $row->affiliate_status == 'approved' ||
                    $row->affiliate_status == 'pending' ||
                    $row->affiliate_status == 'empty'
                ) {
                    if ($userobj->GetAdminUserLink('Suspend Affiliate', $adminUserId, 2)) {
                        echo "<option value=\"$Suspend\">Suspend</option>";
                    }
                }

                if ($userobj->GetAdminUserLink('Adjust Money', $adminUserId, 2)) {
                    echo "<option value=\"$adjust\">AdjustMoney</option>";
                }

                if ($userobj->GetAdminUserLink('Set Commission Group', $adminUserId, 2)) {
                    echo "<option value=\"$TierGroup\">Set Commission Group</option>";
                }
                ?>
            </select>
        </form>
    </td>
    <td width="6%" height="25" align="center">
        <?php if ($userobj->GetAdminUserLink('Affiliate Login', $adminUserId, 2)) { ?>
            <a href="test1.php?aid=<?= $row->affiliate_id ?>">Log In</a>
        <?php } ?>
    </td>
</tr>
<?php
}
?>

      <tr>
    <td colspan="9" align="center">
        <?php
        /**********************************for pageno***************************/
        $url = "index.php?Act=affiliates&amp;status=$status&affiliate=$affiliatename"; // adding page nos
        include '../includes/show_pagenos.php';
        /************************************************************************/
        ?>
    </td>
</tr>
</table>

<?php
/******************************close if records exists*******************************/
} else {
?>
    <table width="100%" align="center">
        <tr>
            <td align="center" class="error"><?= $norec ?></td>
        </tr>
        <tr>
            <td></td>
        </tr>
    </table>
<?php
}
?>

<br />
<script type="text/javascript">
    function viewLink(affiliateid) {
        let url = "viewprofile_affiliate.php?id=" + affiliateid;
        let nw = window.open(url, 'new', 'height=450,width=450,scrollbars=yes');
        // nw.focus(); // optional
    }
</script>