how to use codeigniter where in on comma seperated row values - php

I am using codeigniter for my project and implementing search. I want to filter users from users table with cars they own. Structure of this table is shown below
+-----+----------+---------------------+---------------+
| #id | username | cars | other_details |
+-----+----------+---------------------+---------------+
| 1 | MAC | Jaguar,Porche | -- |
| 2 | DEV | Porche,Ferrari,Ford | -- |
| 3 | MONICA | Ford,Audi | -- |
+-----+----------+---------------------+---------------+
On front end, I am selecting cars from checkboxes which are returning car array for find users who have these cars like ["Porche","Ferrari"]. I am not able to find solution for how to get users using codeigniter active records in model. Please help me find out how to get users/rows having cars available in array.
Thanks in advance.

Finally, I find answer.First I had implode array with comma(,) and then use sql query and regular expression for getting an answer like this;
$this->db->sql("select * from users where `cars` NOT REGEXP '$cars'");
This query give data according to my requirement.

Related

How to return MySQL rows as columns in PHP

I hope someone can help, I have a database of different odds (betting odds) which are linked to a unique eventID. All of the odds are stored as a row in the MySQL database as per the below description.
ID | EventID | BookmakerName | BoommakerURL | BookmakerHome | BookmakerDraw | BookmakerAway
12345 | 111213 | Bookmaker | http://wwww.URL.com | 1.3 | 1.6 | 1.5 <p>
12345 | 111213 | Bookmaker2 | http://wwww.URL2.com | 2.3 | 2.6 | 2.5
What i'm looking to do though is to display these in columns as per the below example. I'll be doing a INNER Join on get the team name from another table.
EventID | Team | Bookmaker | Bookmaker2 <p>
111213 | HomeTeam | 1.3 | 2.3<p>
111213 | AwayTeam | 1.6 | 2.6<p>
111213 | Draw | 1.5 | 2.5<p>
I'm using PHP and managed to loop through the results and display the rows but I'm struggling to change the layout.
Any help would be appreciated as I'm stuck. Happy any MySQL solutions e.g. create a new table or PHP based solutions.
First of all you should explain properly what you actually want to do and what you have tried and exactly you have got stuck.
As far as i have understood , this will surely help :
you can join the two tables and can get the data from other table.
For example :
Select * from table1
join table2 on table1.EventID=table2.EventID
where (what ever required condition)
Try this it will work for you

Get Matching MySQL Rows from List?

I've got a list of ID numbers, (ex: 100230, 123890, 342098...). I've also got a table, with one column devoted to ID numbers:
thisID | name | dateBirth | State
----------------------------------
192465 | Fred | 94-12-06 | OR
197586 | Alex | 78-04-26 | NM
197586 | Alex | 78-04-26 | CA
178546 | Sam | 65-12-01 | NY
112354 | Katy | 89-06-22 | CO
...
I need to return any rows with 'thisID' that matches any of the items in the list I've got. Also, note that sometimes there may be multiple rows with the same ID that match an item in the list... in that case, all matching records should be returned.
I've looked around, and seen some recommendations to use arrays or temporary tables or something, but nothing definitive. How should I do this?
You can use the IN sql syntax for this, if I understand you correctly.
SELECT * FROM tablename
WHERE thisID IN (100230, 123890, 342098);
You can do like this:
select * from [table_name] where thisID in ([your IDs]);
It will return all the rows that match the given IDs.
See the SQLFiddle Demo

Check if value exists in MySQL table and then select [Laravel 5]

Here is my pivot table project_group:
+-----+----------+------------+----------+---------+
| ids | group_id | project_id | admin_id | user_id |
+-----+----------+------------+----------+---------+
| 4 | 115 | 1 | 1 | [3,4,5] |
| 5 | 115 | 2 | 1 | [5,2,1] |
| 6 | 115 | 3 | 1 | [1,3,6] |
This table represent group linked to the projects....user_id is which users can see projects/group... Is there any way to display correct projects/group only to the users defined in user_id?
Also content in user_id field can be changed....
The best way to handle this would be to first normalize your database. Storing comma separated lists in a cell is allowed, but generally bad practice, as explained in this question.
If you can have multiple users per project, you should have a linking table with a column for project and a column for user, like this:
project_users:
| project_id | user_id |
and you can make (project_id, user_id) a composite primary key.
That way, you can select the users for a project (say, project 1) like this:
SELECT user_id
FROM project_users
WHERE project_id = 1;
Once you have these, you can display the project data only to users whose id is returned in the above list.
I have built an SQL Fiddle that helps demonstrate this visually, if it helps.
It is good to note that this proper normalization gives the opportunity to a lot of useful data as well, as it becomes easier to search for users by project, but also you can search for project information based on a user.

MySQL returning results from one table based on id data in another table

I tried to make this inside this question, but i am too young on #stackoverflow to post comments.
MySQL returning results from one table based on data in another table
I cannot get this to work. My intentions are slightly different.
I have two tables (and more in the future) that I intend to work together. I want to keep my db size down, so instead of using full words to reference time_code_department, I added a column to reference the "department_id". now I want to grab all the "time_codes" from table where the "time_code_depart" id matches the variable entered.
So if user selects "Solar" department and time_code_department table has "9" as the "solar" "department_id", then i want to return all the entries in "time_codes" that have the "department_id" "9" on the time_codes table. Which in this example would be lines with id 40 and 75.
Table Structure:
----------------------------------------------
| time_codes (table) |
| |
| id | department_id | code_number | code_name |
----------------------------------------------
| 40 | 9 | 35 | Safety |
| 52 | 10 | 725 | Inventory |
| 75 | 9 | 18 | Cabinets |
----------------------------------------------
-----------------------------------
| time_code_depart (table) |
| |
| department_id | name | manager |
-----------------------------------
| 9 | Solar | John |
| 10 | Finance | Mary |
| 11 | Design | Sue |
-----------------------------------
I've tried to query:
SELECT 'department_id'
FROM `time_codes`
INNER JOIN `time_code_depart`
ON 'time_codes.department_id' = 'time_code_depart.department_id'
WHERE 'name' LIKE 'Solar'
and
SELECT 'time_codes.id', 'time_codes.code_number', 'time_codes.code_name'
FROM `time_codes`
ON 'time_codes.department_id' = 'time_code_depart.department_id'
WHERE 'time_code_depart.name'
LIKE 'Solar'
Both of these I formed based on several readings on the subject, and i have used several variation of sentax. I cannot get it to return the entries for the lines with id 40 and 75.
Can you help me identify where I am going wrong?
You have several problems with quoting.
First, to quote table or column names in MySQL, you use backticks; single quotes are used for making strings.
Second, when you have a table.column, you must quote them each separately.
Note that it normally isn't necessary to quote table and column names at all. They only need to be quoted if they're the same as reserved words, or contain punctuation characters.
SELECT `time_codes`.`department_id`
FROM `time_codes`
INNER JOIN `time_code_depart`
ON `time_codes`.`department_id` = `time_codes_depart`.`department_id`
WHERE `name` LIKE 'Solar'
And when you have long table names like this, I recommend making use of table aliases to make expressions more readable:
SELECT tc.department_id
FROM time_codes AS tc
INNER JOIN time_code_depart AS tcd
ON tc.department_id = tcd.department_id
WHERE name LIKE 'Solar'

How to create a grid from three mysql tables

I am trying to create a grid with yii using three different tables. Here is a rough demonstration of what I am trying to achieve:
Table: Products
prod_id, prod_name
Table: Regions
reg_id, reg_name
Table: Prices
price_id, prod_id, reg_id, price
Products/Regions | Region#1 | Region#2 | ... | Region#N
--------------------------------------------------------
Product#1 | Price#1 | Price#2 | ... | Price#N
Product#2 | Price#1 | Price#2 | ... | Price#N
Product#3 | Price#1 | Price#2 | ... | Price#N
...
Product#N | Price#1 | Price#2 | ... | Price#N
I need to approach this from the products table. I need to be able to search the products. And I don't need to to use GridView, also it would be optimal if I can use it.
I really need recommendation more than anything in this situation. One idea I have is to create a double array of prices and access them and put them in the table $prices[$prod_id][$reg_id] but this system has to be really optimal and performance plays a huge role. So, I would appreciate if anyone can help me out with this.
Thanks,
Gasim

Categories