#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0

    Displaying text not INT field contents


    HI
    Apologies for getting anything wrong in my description, I've just starting learning.

    I've got a small problem I cant see how to overcome.

    I've got this code in one file (FRONT END FILE):
    <?
    for($i=0; $i<count($associateevent); $i++)
    {
    $sqli="select id,grpname,grpstyle,grpgroupsize,grptransport,grpaccommodation,grptour_pic,grptour_info from trip_group where id='$associateevent[$i]'";
    $sqli=mysql_query($sqli);
    $rowi=mysql_fetch_array($sqli);
    $tppsql="";
    $id=$rowi['id'];
    $grpname=$rowi['grpname'];
    $grpstyle=$rowi['grpstyle'];
    $grpgroupsize=$rowi['grpgroupsize'];
    $grptransport=$rowi['grptransport'];
    $grpaccommodation=$rowi['grpaccommodation'];
    $grptour_pic=$rowi['grptour_pic'];
    $grptour_info=nl2br(stripslashes($rowi['grptour_info']));
    ?>

    and then:

    <tr><td class="smallbold"><?=$rowi['grpstyle']?></td></tr>

    The problem is that the database field for 'grpstyle' is an INT(11) and I have the following code for selecting the correct 'grpstyle' data in another file (BACKEND FILE):

    <tr><td align="left"><strong>Style of trip:</strong><select name="grpstyle<?=$ct?>" <?=$disabled?>>
    <option value="1" <? if($grpstyle==1) { ?> selected <? } ?>>Challenging</option>
    <option value="2" <? if($grpstyle==2) { ?> selected <? } ?>>Moderate</option>
    <option value="3" <? if($grpstyle==3) { ?> selected <? } ?>>Comfortable</option>
    <option value="4" <? if($grpstyle==4) { ?> selected <? } ?>>Luxury</option>
    </select></td></tr>

    The problem is that I can't get the text 'Challenging, or Moderate etc to display on the Front End File. What I get is the actual number itself.

    I hope the explanation is clear!

    Thanks in advance
  2. #2
  3. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    I thought I understood it, then I didn't... now I'm just not sure.

    Where is it displaying wrong? In the dropdown form field?

    If you're expecting this...
    Code:
    <tr><td class="smallbold"><?=$rowi['grpstyle']?></td></tr>
    ... to output the text form, there's no reason it would. That's going to display whatever value is in the database. And if it's that form populating the database it'd either be 1,2,3 or 4.

    Maybe you mean for the option values to be the same as the labels? If you change those then you'll need to change the database column from an INT to a VARCHAR or something, then it would come out of your query being displayed as text.

    ... in theory.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2013
    Posts
    2
    Rep Power
    0
    Hi- sorry for the confusion!

    I am selecting the options from a drop down option on my page. The option I select is being stored in the database as a digit, regardless of the VARCHAR or INT column type it has in the database.

    How can I get it to display the option text on another page from the dropdown rather than the digit from the database?

    Thanks for any further suggestions.
  6. #4
  7. --
    Devshed Expert (3500 - 3999 posts)

    Join Date
    Jul 2012
    Posts
    3,957
    Rep Power
    1046
    Hi,

    if you actually just store plain digits in the database without any information what that means, you're doing it wrong. "Magic numbers" like this are terrible. They're cryptic (What was "3" again?), error-prone and cumbersome. Every time you wanna display the actual meaning, you have to translate the digit -- as you just saw.

    Don't do that. Either store the styles as strings ("Challenging", "Comfortable", ...) and get rid of those numbers altogether. Or make a styles table and reference the entries by their ID (which may very well be an integer). The latter will allow you to store additional information.
    The 6 worst sins of security ē How to (properly) access a MySQL database with PHP

    Why canít I use certain words like "drop" as part of my Security Question answers?
    There are certain words used by hackers to try to gain access to systems and manipulate data; therefore, the following words are restricted: "select," "delete," "update," "insert," "drop" and "null".
  8. #5
  9. No Profile Picture
    Dazed&Confused
    Devshed Novice (500 - 999 posts)

    Join Date
    Jun 2002
    Location
    Tempe, AZ
    Posts
    506
    Rep Power
    128
    ^ What he said.

    If you insist upon it you can translate the number to a string just by testing which number is in the variable and outputting the textual form based on that, but if you're having to translate something twice then there's the opportunity to refactor.

    And in this case also the opportunity to save yourself from a polluted database.

    To some extent you should be able to understand what the database stores by just looking at the database and nothing else. A numeric value representing a string that only code knows how to interpret, is contrary to that goal.

    So as Jacques suggests, there's two solutions:
    1. Easier: change the column to a string and store the string
    2. Better: use a lookup table to map IDs to their textual form

    I consider the second better because:
    1. It optimizes data storage.
    2. There's inherent validation value.


    Your main table:
    Code:
    | id | grpname | grpstyleid | grptransport | ...
    |----|---------|------------|--------------|----
    |  1 | fu      |          1 | abc          | ...
    |  1 | bar     |          2 | xyz          | ...
    |  1 | tar     |          3 | 123          | ...
    |  1 | fu      |          1 | a lot        | ...
    Lookup table:
    Code:
    | grpstyleid | name        | additional info | ...
    |------------|-------------|-----------------|----
    |          1 | Challenging | Something       | ...
    |          2 | Moderate    | Anything        | ...
    |          3 | Comfortable | Nothing         | ...
    |          4 | Luxury      | Everything      | ...
    Then when you SELECT from your table for display purposes you simply JOIN the lookup table based on the grpstyleid to get its textual form.

    You can also use the lookup table itself to dynamically populate your dropdown, so in the future if you want to add/change the options you need only update the database. (though be careful not to break existing data by changing it...)

    Comments on this post

    • Jacques1 agrees

IMN logo majestic logo threadwatch logo seochat tools logo