• Vote in November Mapping Competition to select a winner!
  • There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!

AAC [ZnoteACC] Help-me optimize my isMarried on characterprofile

Lurk

Active Member
Joined
Dec 4, 2017
Messages
339
Reaction score
44
Helo, I just added this to my server NPC - Marriage system 8.6+ [Fixed] (https://otland.net/threads/marriage-system-8-6-fixed.161590/) and I wanted to show on my website who the characters were married to and I managed to do it but the code is so ugly it makes me sad :( can anyone help? here's what I've done

PHP:
<?php
    $is_married = false;
    $player_id = (int)$user_id;
                $db_query = "
                    SELECT `partner`
                    FROM `marriage_system`
                    WHERE `player_id`={$player_id}
                ";
    $marriedto = mysql_select_single($db_query);
    $marriedto = ($marriedto !== false) ? (int)$marriedto['partner'] : 0;
   
    if ($marriedto != false){
        $is_married = true;
        $db_query = "
                    SELECT `name`
                    FROM `players`
                    WHERE `id`={$marriedto}
                ";
        $nameofparter = mysql_select_single($db_query);
        $nameofparter = (string)$nameofparter['name'];
    }
   
    $is_married2 = false;
    $player_id = (int)$user_id;
                $db_query = "
                    SELECT `player_id`
                    FROM `marriage_system`
                    WHERE `partner`={$player_id}
                ";
    $partnerof = mysql_select_single($db_query);
    $partnerof = ($partnerof !== false) ? (int)$partnerof['player_id'] : 0;
   
    if ($partnerof != false){
        $is_married2 = true;
        $db_query = "
                    SELECT `name`
                    FROM `players`
                    WHERE `id`={$partnerof}
                ";
        $nameofplayer = mysql_select_single($db_query);
        $nameofplayer = (string)$nameofplayer['name'];
    }   
?>
<!-- is married? nameofplayer -->
                <?php if ($is_married): ?>
                <tr>
                    <td><b>Married to</b></td>
                    <td><a href="characterprofile.php?name=<?php echo $nameofparter; ?>"><?php echo $nameofparter; ?></a></td>              
                </tr>
                <?php endif; ?>
                <!-- is married? nameofplayer -->
                <?php if ($is_married2): ?>
                <tr>
                    <td><b>Married to</b></td>
                    <td><a href="characterprofile.php?name=<?php echo $nameofplayer; ?>"><?php echo $nameofplayer; ?></a></td>              
                </tr>
                <?php endif; ?>

here's how it looks on the website
1593825601136.png
1593825629570.png
 
Solution
PHP:
<?php
/*
CREATE TABLE marriage_system (
    id INTEGER NOT NULL,
    player_id   INTEGER NOT NULL,
    partner   INTEGER NOT NULL,
    marriage_date INTEGER NOT NULL,
    PRIMARY KEY ( id )
);
*/
$player_id = (int)$user_id;
$marriage = mysql_select_single("
    SELECT 
        CASE WHEN `m`.`partner` = {$player_id}
            THEN `p2`.`name`
            ELSE `p1`.`name`
        END AS `partner_name`
    FROM `marriage_system` AS `m`
    INNER JOIN `players` as `p1`
        ON `m`.`partner` = `p1`.`id`
    INNER JOIN `players` as `p2`
        ON `m`.`player_id` = `p2`.`id`
    WHERE {$player_id} IN(`m`.`player_id`, `m`.`partner`)
    LIMIT 1;
");
?>
<!-- is married? -->
<?php if ($marriage !== false): ?>
    <tr>...

Znote

<?php echo $title; ?>
Staff member
Global Moderator
Premium User
Joined
Feb 14, 2008
Messages
6,834
Solutions
240
Reaction score
1,616
Location
Norway
GitHub
Znote
PHP:
<?php
/*
CREATE TABLE marriage_system (
    id INTEGER NOT NULL,
    player_id   INTEGER NOT NULL,
    partner   INTEGER NOT NULL,
    marriage_date INTEGER NOT NULL,
    PRIMARY KEY ( id )
);
*/
$player_id = (int)$user_id;
$marriage = mysql_select_single("
    SELECT 
        CASE WHEN `m`.`partner` = {$player_id}
            THEN `p2`.`name`
            ELSE `p1`.`name`
        END AS `partner_name`
    FROM `marriage_system` AS `m`
    INNER JOIN `players` as `p1`
        ON `m`.`partner` = `p1`.`id`
    INNER JOIN `players` as `p2`
        ON `m`.`player_id` = `p2`.`id`
    WHERE {$player_id} IN(`m`.`player_id`, `m`.`partner`)
    LIMIT 1;
");
?>
<!-- is married? -->
<?php if ($marriage !== false): ?>
    <tr>
        <td><b>Married to</b></td>
        <td><a href="characterprofile.php?name=<?php echo $marriage['partner_name']; ?>"><?php echo $marriage['partner_name']; ?></a></td>              
    </tr>
<?php endif; ?>
 
Solution
Top