user7796548
user7796548

Reputation:

Alternative to Using Global Variable in PHP

I have developed a function which runs a query and then uses the resulting variables in a logic in another file. I have used global variables to achieve this. I have read global variables are to be avoided, is there another way to write this function to avoid use of global variables.

File 1 (Function File)

<?php 

    function usertype() 
    {

        include "../classes/sessionstart.php";
        include "../config/dbconnect.php";

        $user_id = $_SESSION['user_id'];
        $select = $con->prepare("SELECT user_usertype, user_gender FROM tbl_user WHERE user_id = $user_id");
        $select->setFetchMode(PDO::FETCH_ASSOC);
        $select->execute();
        while($data=$select->fetch()){

            $GLOBALS['gender'] = $data['user_gender'];  
            $GLOBALS['usertype']  = $data['user_usertype'];
        }
    }

?>

File 2 (File Using the Function File)

<?php
    usertype();
?>
<div>
    <select class="searchpropertyinputs" name="user_usertype" id="user_usertype">
        <?php if ($gender == "Male" && $usertype != "Marriage Bureau") { ?> <option value="Bride">Bride</option> <?php } ?>
        <?php if ($gender == "Female" && $usertype != "Marriage Bureau") { ?> <option value="Groom">Groom</option> <?php } ?>
        <?php if ($usertype == "Marriage Bureau") { ?>
            <option value="" hidden>Bride or Groom</option>
            <option value="Bride">Bride</option>
            <option value="Groom">Groom</option>
        <?php } ?>  
    </select>
</div>

Upvotes: 0

Views: 546

Answers (1)

Lawrence Cherone
Lawrence Cherone

Reputation: 46602

You should return the value from the function:

Updated code, also fixed prepared query, and set an alias for the return columns.

<?php

    function usertype() 
    {
        include_once "../classes/sessionstart.php";
        include_once "../config/dbconnect.php";

        $select = $con->prepare("
            SELECT user_usertype as `type`,
                   user_gender   as `gender`
            FROM tbl_user
            WHERE user_id = :user_id LIMIT 1
        ");
        $select->bindValue(':user_id', (int) $_SESSION['user_id'], PDO::PARAM_INT);
        $select->execute();

        return $select->fetch(PDO::FETCH_ASSOC);
    }

?>

.

<?php
    $usertype = usertype();
?>
<div>
    <select class="searchpropertyinputs" name="user_usertype" id="user_usertype">
        <?php if ($usertype['gender'] == "Male" && $usertype['type'] != "Marriage Bureau") { ?> <option value="Bride">Bride</option> <?php } ?>
        <?php if ($usertype['gender'] == "Female" && $usertype['type'] != "Marriage Bureau") { ?> <option value="Groom">Groom</option> <?php } ?>
        <?php if ($usertype['type'] == "Marriage Bureau") { ?>
            <option value="" hidden>Bride or Groom</option>
            <option value="Bride">Bride</option>
            <option value="Groom">Groom</option>
        <?php } ?>  
    </select>
</div>

You may also want to move out the includes to connect, session start, or you will have issues for future functions. So that should most likely lead you on to grouping functions into a user class, for example:

<?php

include_once "../classes/sessionstart.php";
include_once "../config/dbconnect.php";

class User {

    public function __construct(PDO $con)
    {
        $this->con = $con;
    }

    public function type($user_id = 0) 
    {
        $select = $this->con->prepare("
            SELECT user_usertype as `type`,
                   user_gender   as `gender`
            FROM tbl_user
            WHERE user_id = :user_id LIMIT 1
        ");
        $select->bindValue(':user_id', (int) $user_id, PDO::PARAM_INT);
        $select->execute();

        return $select->fetch(PDO::FETCH_ASSOC);
    }

    //...
}

$user = new User($con);

$usertype = $user->type($_SESSION['user_id']);
?>

Upvotes: 2

Related Questions