473,397 Members | 1,985 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

Keeping only 40 comments for each product

258 100+
Hi everyone

I have 2 tables in my MySQL DB

Products :
Expand|Select|Wrap|Line Numbers
  1. +----+--------+
  2. | id | Product|
  3. +----+--------+
  4. | 1  | Prdct1 |
  5. | 2  | Prdct2 |
  6. | 3  | Prdct3 |
  7. | 4  | Prdct4 |
  8. +----+--------+
  9.  
And then I have another table for comments posted by visitors for each product

comments:
Expand|Select|Wrap|Line Numbers
  1. +----+---------+---------+
  2. | id | prdctid | commect |
  3. +----+---------+---------+
  4. | 1  |   1     |  text   |
  5. | 2  |   4     |  text   |
  6. | 3  |   2     |  text   |
  7. | 4  |   1     |  text   |
  8. | 5  |   2     |  text   |
  9. | 6  |   3     |  text   |
  10. | 7  |   1     |  text   |
  11. +----+---------+---------+
  12.  
Now I need to keep (at most) 40 comment for each product and delete the older ones. I mean when someone writes a comment on a product which has 40 comments the oldest comment will be removed.

I know that I can only show 40 by LIMIT command in MySQL but I actualy need to keep only 40 comments for each product on the DB and remove the older ones.

Here's what I am doing right now.

Expand|Select|Wrap|Line Numbers
  1. SELECT * FROM comments WHERE prdctid="current product id"
  2.  
And then :
Expand|Select|Wrap|Line Numbers
  1. if (mysql_affected_rows() > 40){
  2.   $query = mysql_query('SELECT id FROM comments WHERE prdctid="current product id" LIMIT 1');
  3.  
  4.   $tmpVar=mysql_fetch_array($query);
  5.  
  6.   mysql_query('DELETE FROM comments WHERE id='.$tmpVar["id"]);
  7. }
  8.  
This works but as you can see it's not fast enough and not professional.

Is there any other way to do this?

Thanks / Behzad
Jun 12 '10 #1
3 1385
Dormilich
8,658 Expert Mod 8TB
personally, I wouldn’t delete any comments*, if I can LIMIT my SQL clause, that’d be fine for me (plus you cannot incidentaly delete something).

* - databases are able to handle millions of entries …
Jun 12 '10 #2
bnashenas1984
258 100+
Hi Dormilich
Actualy i'm working on a project for several companies with a shared server and the problem is that the database is limited to 100 MB and 27 requests per second.

I'm already using a lot of that space. So, letting visitors post unlimited comments might exceed that limit.

Thanks / Behzad
Jun 13 '10 #3
Dormilich
8,658 Expert Mod 8TB
the problem is that the database is limited to 100 MB and 27 requests per second.
that are some hard restraints. I’ll think about it.
Jun 13 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

2
by: Girish | last post by:
Hi I have list of products listed from a search criteria, Now I wanna show the client TOP VIEWED LIST OF PRODUCTS or number of hits for each product. So the page hits using the counter...
1
by: Angelos | last post by:
Hello, -I wrote a script that you use it to add products in the database. -Each product is associated with files. -In the Form to add the product you have to enter the product name in a textfield...
1
by: enrique.rojas | last post by:
Hello I have been trying to make an SQL statement that will get the the last product audited in a database. The problem is that this database holds data for several emplyees that audit...
11
by: Paul Aspinall | last post by:
Hi Can anyone offer any hints / tips for creating registration keys / application activation, as software copy protection measures for a Winform app developed in C#? Thanks
2
by: Craig Buchanan | last post by:
I have a class that inherits NameObjectCollectionBase called Products. In the class' constructor, I add items using BaseAdd(key,value), where key is a string representation of an id and value an...
19
by: pinkfloydhomer | last post by:
Please read the example below. I'm sorry I couldn't make it smaller, but it is pretty simple. When client code is calling newThingy(), it is similar to malloc: the client gets a pointer to a...
1
by: alan_conoco | last post by:
Hello all, I have a table looks like this in Access Product Sale Date A date A date A date B date B ...
1
by: ollielaroo | last post by:
Hi guys, Firstly I did do a search for this one first but I couldn't find anything related in this forum. I am using Dreamweaver MX and trying to build admin pages for an ASP site. My problem is...
3
by: lapkorts | last post by:
Hi, I am looking for product catalogue/shop, preferable for Joomla or known CMS with the following two features: 1. Multidimentional. I.e. each product could belong to several trees/ branches...
1
realayumi
by: realayumi | last post by:
ACCESS 2003 WIN XP Hi, i'm new in access, just a view days, this is my first post. Sorry for my bad English. I have 3 tables. tbl_product
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.