Convert Received Data from SQL and Translate - php

I am wanting to convert data that is queried from the database to another string. The best way I can explain what I'm trying to do is below:
I have a table called "users". Inside that table I have a column called "rank". Rank is a two-digit integer ranging from 1 to 21.
I want to convert that "1" or whichever number into more understandable text in my php file. So if your rank is set to "1", the output is "User" or what-have-you.
How do I go about doing this? I'm not sure what this process is called. I'm very new to php.
Thank you!

Create a rank table having 2 columns, id and rank:
id | rank
-------------
1 | user
2 | admin
3 | goof
Then JOIN the users table with the rank table:
SELECT u.name, r.rank
FROM users u
LEFT JOIN rank r
ON u.rank = r.id

You can store the ranks title in another table like mentioned by Jay Blanchard or you can define an array with the rank titles like this
$ranks = array(
1 => 'User',
2 => 'Administrator',
3 => 'Moderator'
);
and can echo like this
echo $ranks['rank_id_fetched_using_sql'];
rank_id_fetched_using_sql is supposed to be 1 to 21

Related

how to table values in php

I have TWO table
task_emp_designation
task_employee
*task emp_designation table like this *
id -> 1 2 3 4
designation -> webdeveloper, designe, tester, programmer
And task_employee tabel one field nane is there, called emp_designation,in that table i stored like 1 or 4 or 3 ...
For my question is while selecting the emp_designation values,it will return like 1 or 4 or 3,
i want to show 1 means developer,2 means designer ,3 means tester,like that i want show,i don't know how to do this
SELECT task_employee.id, task_emp_designation.designation
FROM task_employee
INNER JOIN task_emp_designation
ON task_employee.emp_designation = task_emp_designation.id;
That would do what you want if I understood you correctly

select query records if multiple type is matched

I have some mysql tables like below,
1)videos
id name
1 test1
2 test2
2)tags
id name
1 theme1=test1
2 theme1=test2
3 theme2=test1
4 theme2=test2
5 age=senior
6 age=children
3)tags_to_items
vid tagid
1 1
1 5
Here in table tags_to_item you see videos.id=1 has 2 tags
1)theme1=test1 AND 2)age=senior. It means video.id=1 has 2 tags.
Now here is one conflict,
what i am doing now is, i am searching for theme's only. It means if i search where theme1=theme1 then it need to also search theme1 in theme2 and vice versa. like this
WHERE tagid=1 OR tagid=2
This is functioning proper, but now i want to search if video have multiple tag with AND condition like this
WHERE tagid=5 AND tagid IN (1,2)
It should return video.id=1
So, probably it need to search where video 1 contains tag tag age=senior AND theme1=theme1 OR theme2=theme1. But its not working, anyone have idea how can i do like this?
I think i got the answer.
I need to do join like this,
INNER JOIN tags_to_items t2 ON (v.id = t2.tagid AND t2.tagid = 5)
INNER JOIN tags_to_items t3 ON (v.id = t3.tagid AND t3.tagid in ('27' ,'69','84' ,'99' ))

Select from three tables

I have three tables where table_2 is the middle(connected) between table_1 and table_3
tables
table_id
...
...
table_rest
rest_id
table_id
...
rest
rest_id
...
...
And the query to select I use
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
WHERE rest_id = '$rest_id'
What I need now is to join in this query another table reserv
id
...
status
To check if status is 0, 1,or 2 to not show me anything if there is no status this mean there is no record to show me. In other words this is resserved system where I show on screen few tables. If status is 0,1,2 thats mean the table is taken. If nothing is found for status this mean that there is no record for table and can be shown to user.
EDIT: Sample scenario
tables
table_id
1
2
3
4
5
rest
rest_id
1
2
table_rest
table_id | rest_id
1 2
2 2
3 2
4 2
5 2
So the query that is above will generate 5 tables for rest_id=2 and none for rest_id=1
So now I have another table
reserv
id | status
1 0
2 1
3 2
So in this table reserv currently are saved 3 tables. The idea is to show me other two whit id=4 and id=5 because they are not in table reserv and don't have any status.
Hope is a little bit more clear now.
You have to point from table reserv to which table is beign booked, let's call it reserv.table_id
SELECT m.table_id, table_name
FROM tables m
JOIN table_rest mr
ON m.table_id = mr.table_id
left join reserv
on reserv.table_id = m.id
WHERE rest_id = '$rest_id'
and reserv.status is null (*note)
*note use 'is' or 'is not' depending of your needs, as far as I read, first seems that you want !=, later that what you want is =
It's better if you provide sample data or sqlfiddle. Based on what I realize: Is this what you want:
select tables.table_id, rest.rest_id
from tables
left join table_rest on table_rest.table_id = tables.table_id
left join rest on rest.rest_id = table_rest.rest_id
where rest.rest_id = '$rest_id'
and tables.table_id not in (select id from reserv)

JOIN with 2 Tables (Voting) Mysql, PHP

Hello I have 3 Tables but i need only help with 2 Tables.
The first Table is champions. In this Table are over 100 names.
The second Table is champion_names there are nicknames for the first Table.
The third Table is champion_names_vote. There are the votes for the nicknames. Thumb up if I like any nickname or thumb down.
On my website I have a site where I can see a list full of Names (Table 1). There are 2 columns. In the first is the normal name (Table 1) in the second the nickname (Table 2). Now I want to show the best nickname in column 2. Actually it's random but I only want to show the best nickname.
I can show all Names that's not the problem. But if I want to show only the best nicknames I don't know how.
Table 2: id(AI), champ_id(this is the id for Table 1), sender_id, name
Table 3: id(AI), userid, name_id(Table 2 ID), like_dislike
like_dislike = 1 is like, -1 is dislike and 0 is nothing.
Example:
Table 2: 50, 2, 4, Test
Table 3: 1, 3, 50, 1,
I liked the name of Table 2. So the name_id in Table 3 is the id of Table 2
So how can I do this with JOIN?
Can you help me please.
something like this should do the trick.
Idea is to Analyse your votes per nickname first. Then select the Maximum vote-score by HAVING:
SELECT voteResult.name, vote as winnerVoteScore
FROM(
select a.name_id, b.name,
sum(IF(a.like_dislike = 1, 1, IF(a.like_dislike = 2, -1, 0 ))) as vote
FROM champion_names_vote as a JOIN champion_names as b
ON a.name_id = b.ID
GROUP BY 1
) as voteResult
GROUP BY 1
HAVING vote = max(vote)

MYSQL returning duplicate rows

I have a Database table in MYSQL, it looks like this:
Project_ID user_ID name
11 1 fred
11 2 rick
11 1 fred
I want to get the names in the table, but when I display it I get the same name twice because the user_ID 1 appears twice in the table. I tried to display it with GROUP BY and SUM, but I don't want to do it this way. How do I get it not to return duplicate rows?
Use DISTINCT
SELECT DISTINCT user_ID
, verschil_ma
FROM project_uren
WHERE project_ID = 11
GROUP BY user_ID
Point 1 is that should the user be assigned to the project twice?
Point 2 - Use the DISTINCT keyword to return only unique records - http://dev.mysql.com/doc/refman/5.0/en/distinct-optimization.html
SELECT DISTINCT user_ID
FROM TABLE
WHERE Project_id = 11
That will return you 1 and 2 (You won't get 1 twice)
Thanks
$results = // query
$results = array_unique($results);

Categories