i have an array (Car, SUV, Truck, Bike) that's used in a form. when the option is selected the value is saved to the customers account in database. Value is the array position (eg: 0, 1, 2, 3).
I want this info to be displayed on another page in a table using php as i'm using magento. Currently it only displays the database saved value (0,1,2,3), but i want it to show as ((Car, SUV, Truck, Bike). How do i do this?
i need a way to make it know that 0=Car, 1=SUV, 2=Truck, 3=Bike)
heres what the table looks like now:
The make column represents the vehicle manufacturer (toyota, honda, etc)and type represents (car, bike, suv)
Please help!
Make the table field type an enum that matches the array you specified.
More Details
An ENUM data type is an array that maps a key to a value. If you change the column type to an enumeration, then your Database will display an human readable value instead of a number (e.g., Car, SUV, Truck, Bike).
Also, you can interact with the enum using the Key or the Value so you shouldn't have to change your current implementation.
Related
I found a JSON dump of items for a game I play
http://mooshe.pw/files/items_rs3.json
I want to be able to search/find the item's id by searching/inputting the name.
ex: Search 'dwarf remains' and get the number 0 returned (does the name need to be case sensitive and if so can I make it so it isn't?)
So far all I have is this
$itemName = $_GET['name'];
But once I have the name, I don't know how to convert it to the id number prefixing the object which I need
Edit: Sorry for not being so clear - my end goal is to be able to input a 'name' of an item, and I then need to convert the name to the ID (?) that prefixes the relevant object so I can then pass it on to an API and gather stats etc.
How would I convert the name to the ID e.g. Dwarf remains to 0?
I want to develop an web-service where a user can add tickets. Every ticket has got an input text field title and a textarea description. If the user klick the save button, the data will be saved in a mysql database.
The admin has got an admin panel. He can add or remove input fields in this admin panel to change the add ticket view form the user.
For example: The admin adds a select field category. You can select category A, B or C. If you select category A, there will be a new input text field called animal. If you select category B, nothing happens. If you select category C, there will be 2 new fields: A text field and a number field. The number field is required. And so on, and so on. After a week, the admin could remove some fields, or add a category or... To conclude, the admin can add and remove select, text, number, password,... fields to the add ticket section with relationships, length and requirements.
I dont know how to structure the database and to save the data. I think about something like a mysql table tickets with title, desc, and data and put an XML / JSON String to the data field and another table ticketFields with name (category, animal,...), type (text, number,...), required (yes/no), length (int), data (to store data for select fields). The problem is, that the relationships are missing in this model. So how can I save this data efficient?
The relational model might look like this:
Ticket:
id PK
Answer:
ticketId FK PK
fieldId FK PK
value
Field:
id PK,
name,
parentFieldId NULL FK,
parentFieldValue NULL,
type,
required,
min NULL,
max NULL,
range NULL
(...other "constraint" fields, checked regarding choosen type)
At first, fields with parentFieldId of NULL are displayed. Fields having parentFieldId set are shown only if the answer for parent field is given. Fields having parentFieldId and parentFieldValue set are displayed if the answer for parent field is given and it equals parentFieldValue.
Given answer is checked regarding to field's type (e.g. if type is "number" then the answer must be a number between min and max).
You can create another table with id, category and field for the admin to use and relate them both using a common id field, preferably primary key. Now, this table can be queried for the reference of new categories inserted against a particular id in your main table. Use javascript/jquery to dynamically create html code for the new fields and show them on your page.
Suppose your the user selects Option 'A', then the new table can be queried to see if there are any fields set by the admin against the option 'A'. If yes, retrive those fields and show them to the user.
I have to store an array in one field of a db. I know how to do that technically. My questions is, which version makes sense to do that.
First a short introduction:
I have categories in a table and need translations for each category for all languages. To start, I have 2 languages (later I will have more). Ok, let's say my category is car and the structure is like that:
id name translation
My first idea was:
id name translation
1 car array('eng' => 'car', 'deu' => 'Auto')
The second idea was:
id name translation
1 car 'eng' => 'car', 'deu' => 'Auto'
The third idea was:
id name translation
1 car 'eng'='car','deu'='Auto'
At the end, the script reads the translation cell and loads into an array and returns the value for the given key 'eng' or 'deu' or whatever it comes later.
I use PHP / MySQL
Thank you for any pros and cons.
Ivo
Try adjusting your database structure. This might be ideal:
id name language word
1 car eng car
2 car deu Auto
Primary Key (`name`,`language`)
Unique Key (`id`) (auto_increment)
In this way you can have as many (or as few) languages as you like.
You need to go about this completely different. Add another table, category_translation.
You'd have category:
id name
and language:
id name
and category_translation:
id lang_id category_id translation
This way you'll have things beautifully separated.
you should have a translation table, with the language being one of the columns.
id, name, language, value
1, car, eng, car
2, car, deu, Auto
...
if you only have a single set of values, you can keep everything in this table. if you are looking up multiple values from different sources you can either add a lookup key to this table, or use the name as your lookup value (though indexing on text will not be as good as indexing on integer IDs)
Please do not store an array in your database in one column. At least serialize if it you have to do it
serialize($array)
I want to show values in a dropdown box (either from an array or database - please advise on which option is method)
Then
when a user selects a value in dropdown box, i want to have and get its associated index value (like 1, 2).
For example: dropdown box shows values:
"Car"
"Bicycle"
If user selects "Car", when i get dropdown selected value, i should get 1, similarly for "Bicycle" i get 2 .. and so on.
Please advise easy and simplest method to implement this
Thanks
You can use two methods:
1) Set up prior to display a code that holds associative array with key -> value i.e: 1 => Car (you can keep it in config file if it doesn't change frequently, you can pull it from database or you can keep it in some other form: serialized, file etc.) and use it when the submitted form is being processed.
2) Use array with key and value with the same string i.e: Car => Car and when the form is processed you will have value right away. This solution has some limitations and be troublesome with using more words or other characters that need to be sanitized.
I would advise option 1, you will have to set up the list before and use it after form has been processed but it allows more freedom and its less maintenance.
So I created this application, that displays widgets. Each widget is a *.php file that displays different type of data from the mysql database. Also I created a file options.php, that each user can access and they can change the position where each widget is displayed, the height, the color, and the width. (lets say the widget is a div with info pulled out for the database)
The issue I am having is how to store the data to be specific to the user. At this point after the button save is pushed it UPDATES all the rows while each row contains a specific widget options.
For example:
Options table has these fields: [id] [name] [height] [position] [color]. For now all users see the same placement because it is not user specific. I was thinking about storing all the widget names in 1 row with a separator, then all positions with a separator ...but I'm not sure how to do it.
I recommend you to store every field in a different column, is more clear and elegant, use separator in a column to store different values seems a bit nasty.
You can create a table widget configuration where you have different columns for the required parameters :)
If every widget has a different amount of settings ( therefore different amount of columns ) you could make one field blob and pass serialized array there. Other option is to find common fields for every widget and serialize only what is left in separate column.
To make widget settings
specific to the user
just link corrent widget settings row to the user id. Better in same if every user will have personal row with settings.
All of the following is of course if I understood your question correctly.
You want one table for position options:
Divtable:
id - INT (You want this to be Auto Incrementing Primary Key)
Name - VARCHAR
userid - INT
Top - INT
Left - INT
Width - INT
Height - INT
the ID is the primary key, and really should be the first thing
in damn near every MySQL Table...
The name is the name of the Div.
UserId is a foreign key in your table that points to the ID number on the "Users" table.
Then you know which user owns that DIV, you can select all of HIS divs to redraw later easily, etc, etc...
you can get the user id number from the Users table by username (or whatever)...
select ID from `Users` where username='johndoe121'
Lets say it returns 21. Find all of /his/ DIV options now like so:
select * from `options` where userid = '21'