Using PHP/MySQL to replace multiple strings from a table - php

I have a table called Products that contains Titles of products. Some of these Titles have words I would like to replace. The table contains over 6,000 records. The text strings are found in the Title Column
ID | Title | Description | Price
I have another table called ReplaceText that contains a list of strings I am searching for and new text string. (e.g. small | tiny) This currently has 102 records.
ID | OldString | NewString
I would like to create a PHP/MySQL script to search the Title field in the Products table for matching words from the 'OldStringfield in theReplaceText` table.
I have tried various methods but all can't crack the code. Please help :)
Thank you in advance.

I suggest the following approach, using only SQL queries.
First, create a temporary table ProductsTMP containing only the rows Products with a new Title.
CREATE TEMPORARY TABLE ProductsTMP AS
SELECT p.ID, r.NewString AS Title, p.Description, p.Price
FROM Products p INNER JOIN ReplaceText r ON p.Title = r.OldString;
Then, remove the old rows from the original Products table:
DELETE FROM Products
WHERE ID IN (SELECT ID FROM ProductsTMP);
Finally, add the updated rows from ProductsTMP to the original table Products, and remove the temporary table:
INSERT INTO Products (ID, Title, Description, Price)
SELECT ID, Title, Description, Price FROM ProductsTMP;
DROP TABLE ProductsTMP;

Related

Getting Data from multiple tables SQLite

I have an android application that has 2 tables, one table stores posts and the other table stores images of the posts incase it has an image, changes are not all posts will have images, if a post has an image its primary key will be stored in the foreign Key table, when loading the posts I have to get all posts wether with image or not then check in the image table to see which posts have images and add the images to the posts below is a graphical overview of my tables
Post Table
`|post_id |post | post_description|
|--------|-----|-----------------|
| | | |`
Image Table
`|img_id |fk_postID | imagePath |
|--------|----------|-----------------|
| | | |`
I could have used a join like
Query = "SELECT post_id, post, post_description, imagePath FROM PostTable,
ImageTable, Where PostTable.post_id = ImageTable.fk_postID;
but this query is only returning posts with images and forgeting about posts without images, How can I get all posts be it with image or not? Thanks in advance.
ok, you asked, so give this a whirl, see if you like the output
SELECT pt.post_id, pt.post, pt.post_description, im.imagePath
FROM PostTable pt
left join ImageTable im
on im.fk_postID=pt.post_id
It will bring along for the ride the right table (ImageTable) of those posts that don't have images.
Uses table aliases (pt and im). That helps to be explicit which table the columns come from on the first line in case there are common column names in both, plus a little less typing.
Untested
reference Mysql Left Joins
Try using Left join and that will result all entries from left table and matched entries from right table.
SELECT posttable.postid,posttable.post,posttable.postdescription, imagetable.imageid,imagetable.fkpostid,imagetable.imagepath
FROM posttable
LEFT JOIN imagetable
ON posttable.postid=imagetable.fkpostid
ORDER BY posttable.postid;
Code should look like that.
http://www.w3schools.com/sql/sql_join_left.asp

Finding a list inside a list

I'm having an issue with finding related database rows that aren't specifically linked. Lets say we have 5 tables.
Products
Id | name
Product_tags
Id | product_id | tag_id
Tags
Id | name
Blog
Id | name
Blog_tags
Id | blog_id | tag_ids
So what I do is I grab a product, with this grab the tags this product has via the Product_tags table. Say I get something like this.
Product 101
Tags 123,124,125,126
Now I have this product page. The thing is, now I want to find a blog if it fits this product, but it doesn't need to have all tags that product has, product just needs to have all that blog has. So if blog has tags 124 and 126 it needs to match. Currently I save all the tags a blog has in one row (comma seperated) but this could be changed to save 1 tag per row if needed. If it was reversed I could do it but I need to check for a partial list in a different table.
Product 101
Tags 123,124,125,126
Should find
Blog 201
Tags 124,126
But not
Blog 202
Tags 120,124
I tried a few ways but I couldn't find a way to make this work, my closest attempt was a reverse like like this.
select * from Blog_tags where "'".implode(",",$tagIds)."'" LIKE concat("%", tag_ids, "%")
This sort-of worked but not for when the product had 1,2,3 and the blog only had 1,3.
This is untested and only from memory, so feel free to comment and tell me if it doesn't work.
First of all, normalize your tag list, do not use comma-separated string. You said this could be changed, so I'm going work to assume it's one row per tag, and the column name is tag_id.
Let's start by finding the blogs having some of the tags of you product 101.
SELECT *
FROM Blog
JOIN Blog_tags ON Blog.Id = Blog_tags.blog_id
WHERE tag_id IN (123,124,125,126)
GROUP BY Blog.Id
Now this query will also include blogs that have tags not in this list. So we need to remove them. I think something like that could work:
SELECT *
FROM (
SELECT Blog.id as blogId
FROM Blog
JOIN Blog_tags ON Blog.Id = Blog_tags.blog_id
WHERE tag_id IN (123,124,125,126)
GROUP BY Blog.Id
) as Blog_filtered
LEFT JOIN Blog_tags ON Blog_filtered.blogId = Blog_tags.blog_id AND Blog_tags.tag_id NOT IN (123,124,125,126)
WHERE Blog_tags.id IS NULL
GROUP BY Blog_filtered.blogId

CodeIgniter or Simple PHP , how to join a table with two colomns of another table?

I have a form with two select inputs. In each input there are products as options. Each visitor must select two products as their favorites. Their choice goes into a table named: visitors_fav
The structure of table is:
visitors_fav:
vis_ID - productID_1 - productID_2
As you see i fetch the product IDs not their names and store them into the table.
I have another table named: Products, the structure is like:
Product_ID - Product_Name
How can I get the Product_Name for both productID_1 & productID_2 in a query?
(PHP Codeigniter active record is preferred, and also my database is mySQL)
you must join visitors_fav with Products two times to get the results you want. try the following query:
select vis_ID, prod1.Product_Name as Product_Name1, prod2.Product_Name as Product_Name2
from visitors_fav vis join Products as prod1 on vis.productID_1 = prod1.Product_ID
join Products as prod2 on vis.productID_1 = prod2.Product_ID

match one column value with multiple values in mysql

Ok, so I am creating a sort of online shopping service and I have a DB that requires a field to match up against multiple other fields. They need to be stored, so that they can be ignored and set as maybe for later viewing. I know that setting up a comma delimited field is usually unwise, but I cannot figure out a better way to keep track of the matches while being able to categorize them into separate columns later on.
For eg, I have 25 products to sell and have 10 cities in which I want to sell them. Some can be shipped to 5 cites and some can be shipped to 9 cities. Now if someone searches for products in one particular city, how will only those products appear as a search result that are available for sale in that city only?
Does anyone have any better way of doing this without the comma delimited field?
I'd use about 3-4 tables:
products - having data of each product
cities - having data of relevant cities
product_cities - having relation between product and city
If a product can be shipped to 5 cities, it would have 5 rows in product_cities containing the same product_id and different city_id.
When a person searches, have something like:
SELECT pr.*
FROM cities AS c
INNER JOIN product_cities AS pc
ON c.id = pc.city_id
INNER JOIN products AS pr
ON pc.product_id = pr.id
WHERE c.name LIKE 'New York'
Two way to you will be match one column value with mutiple value using 'case statement' or in statement in where clause like:
select (case when col1>col2 then col2 else
when col1>col4 then col4 else
col1 end) as colname, col5 from tablename
or
select * form tablename where col11 in (1,2,3)
Make three different tables - one for the products you have, one for the cities you want to sell them in and one for describing where each product can be send (id, product_id, city_id).
From there, just use simple joins to select only the possible products for the selected city.
use a n:n Table:
Products Table
products
id | name
city table
cities
id | name
Mapping (unique constraint over BOTH columns, not single constraints):
shipping_information
product_id | city_id
Then you can easily select all available cities for a product, or select all products, which are shipped to a certain city.
All available cities for product 4: SELECT * FROM cities, shipping_info WHERE cities.id = shipping_info.city_id AND shipping_info.product_id = 4
All available products for city 3: SELECT * FROM products,shipping_info WHERE shipping_info.id = 3 AND shipping_info.product_id = products.id

Fetching the title of top searched result in PHP & mySQL

I am having a table named tb_search_report which contains the fields search_report_id,sales_id,cat_name,search_keyword. My purpose is to create a list in the admin side of my website to get the list of the keywords used to search in the front end as well as the count of those keywords.
For that i used the following query:-
SELECT search_keyword,
cat_name,
COUNT(search_keyword) AS cnt
FROM tb_search_report GROUP BY search_keyword
The result is as follows,
search_keyword |cat_name | cnt
------------------------------------
NULL |Handbags | 6
Shoes | | 1
Fabrics | | 3
Now i need to get/list the top search results viewed in the front end along with the above table in another column. The search listing comes from tbl_sales whose primary key is sales_id. How can i display the names of the sales sales_title in top search sales from search keyword. Need help. Thanks in advance.
You can join the tables so that you can show the sales_title
SELECT tbl_sales.sales_title,tb_search_report.search_keyword,tb_search_report.cat_name,
COUNT(tb_search_report.search_keyword) AS cnt
FROM tb_search_report
INNER JOIN tbl_sales ON tb_search_report.sales_id = tbl_sales.sales_id
GROUP BY search_keyword
Just update the above query that will suit your requirements

Categories