Note that there are some explanatory texts on larger screens.

plurals
  1. POHow to organize storage of descriptions for dropdowns list items
    primarykey
    data
    text
    <p>Say i have many tables in database that contain <strong>numeric id</strong> e.g. fields <strong>user_type</strong>, <strong>user_status</strong>, <strong>user_currency</strong> etc.</p> <p>now i do have to display this information along other info on my pages of my website, say i have 10 pages where some of above info will be printed</p> <p><strong>METHOD 1</strong> when i write query i do something like</p> <pre><code>SELECT ... user_status ... FROM users WHERE user_id = 5 </code></pre> <p>then i pass to template language, i use Smarty v3</p> <p>and then inside page template i do something like </p> <pre><code>{section ...} ... {if $users[0].user_status == 1} New User {elseif $users[0].user_status == 2} Application Accepted {elseif $users[0].user_status == 3} Payment received {/if} ... {/section} </code></pre> <p>so same code basically goes into 10 different pages and when i change something or add new status, currency etc i got to go to each page and add it to the if statement</p> <p>I think this method is very unreliable and time consuming</p> <p><strong>METHOD 2</strong> so i started to do it like this i create another table with user statuses e.g. <strong>tbl_user_statuseds</strong> </p> <pre><code>user_status_id status_name 1 New User 2 Application Accepted 3 Payment received ... </code></pre> <p>then in main query for each page i do something like this</p> <pre><code>SELECT ... tbl_user_statuseds.status_name ... FROM users LEFT JOIN tbl_user_statuseds ON tbl_user_statuseds.user_status_id = users.user_status WHERE user_id = 5 </code></pre> <p>and in page i just use <code>{$users[0].status_name}</code> this takes out editing on each page if i add new status. All has to be done is add new line in database table statuses once</p> <p>However with second method i have now about 10 tables in database that just contain the numerical id descriptions <strong>tbl_user_statuseds, tbl_currency_names, tbl_user_sex</strong> etc.</p> <p><strong>METHOD 3</strong> i wonder would it be a good practice to use once table to store this ids? E.g.</p> <p><strong>tbl_id_labels</strong> </p> <pre><code> id id_type id_number id_label 421 2 1 New User 422 2 2 Application Accepted 423 2 3 Payment received 424 8 1 USD 425 8 2 EURO 426 5 1 Male 427 5 2 Female ... </code></pre> <p>Then i just do same as method 2 except i left join this table based on id_number e.g.</p> <pre><code>SELECT ... tbl_id_labels.status_name ... FROM users LEFT JOIN tbl_id_labels ON tbl_id_labels.id_number = users.user_status AND tbl_id_labels.id_type = 2 WHERE user_id = 5 </code></pre> <p>This way i only have one table with all labels for numeric id's i have in my system. If i have to add one in the future its not too difficult, all that matters is <strong>id_type</strong> and <strong>id_number</strong>, field <strong>id</strong> is just for compatibility.</p> <p>Do you see any problem with third method, or can you advice better approach?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload