简体   繁体   中英

HOW to assign an integer value coming from mysql database so as to present a string at the front end?

I am trying to store user provided data in the database using bootstrap drop down options: I am assigning numeric values to different objects like Gender Female=1, Male=2, Prefer Not to Say = 3. Likewise for education, School Certificate = 1, Undergrad = 2, Grad = 3, Masters = 4

There are a host of other fields that I intend storing the same way using the bootstrap form select options. A part of the bootstrap is given below in the code section.

My forms are working fine and there is no issue with storing the data. However, I am having difficulty coming up to a solution to present back this data to the users after doing the necessary matching using mysql query.

<div class="form-group">
<label for="gender">Your Gender</label>
<select class="form-control" name="gender" id="gender" required>
<option data-hidden="true" value="">Select One</option>
<option data-hidden="true" value="1">Female</option>
<option data-hidden="true" value="2">Male</option>
</select>
</div>

My question is what is the best option for me to re-assign these numeric variables to their original strings. Should i try writing a function which I think would turnout to be very lengthy considering there are number of other variables being stored and retrieved. Or is there any other option?

Thanks

You really have multiple options here depending on exactly what your requirements. For something that needs to do these kinds of replacements at scale, I would typically use lookup tables in a database. Something like

_optionID_|_optionGroup_|_optionValue_|_optionName_
    1     |   gender    |      1      | Female
    2     |   gender    |      2      | Male
    3     |   gender    |      3      | Prefer not to say
    4     |   education |      1      | Certificate

And so forth. Depending on your needs you may prefer to split out each set of values into their own tables.

This approach has a couple of advantages

  • You could query the "pretty" names as part of the original SQL query using joins (so you wouldn't need to do a separate query later on).
  • You could eventually use these tables as a starting point for dynamically generating the forms (adding a new form could become as simple as adding a new set of optionGroups .

This technique is functionally similar to having a function with hard coded values but is much more maintainable/extendable. In this scenario, assuming you weren't pulling the optionName as part of your original query, a function that fetches them would look something like this

function getOptionName($optionGroup, $optionValue)
{
    /** 
        ...Code to get a PDO object of some kind
        @var \PDO $pdo
    **/
    $stmt = $pdo->prepare('SELECT optionName FROM lookupValues WHERE optionGroup= :optionGroup: AND optionValue = :optionValue:')
    $stmt->execute(['optionGroup' => $optionGroup, 'optionValue' => $optionValue]);
    if ($stmt->rowCount() > 0) { //If at least 1 row was returned
        $data = $stmt->fetch(PDO::FETCH_ASSOC); //Get the top row (there should usually only be 1 row returned
        return $data['optionName'];
    }

    return null; //The option wasn't found
}

Without knowing more about your application's structure it is hard to give an example of how the JOIN version would look. Assuming you are comfortable with that type of query, using JOINs would usually be more efficient than running a secondary query.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM