Jump to content

[SOLVED] Creating popular items from orders in database.


Recommended Posts

Hi,

 

I am setting up an e-commerce website where orders are stored in a mysql database after processing.

 

A table called `itemorder` contains a record for each item that has been bought, the quantity purchased, and the order that it belongs to.

 

I'd like to create a list of popular items based on this table. I've tried putting the itemids into an array but can't work out how to do the maths in php to count up how many of each value there are. The quantity field would make things even harder.

 

Any suggestions how I can do this?

 

Many thanks.

 

[attachment deleted by admin]

Something like:

 

SELECT `itemid` FROM `table` ORDER BY SUM(`quantity`) GROUP BY `itemid`

 

Should do it.

 

It's not very efficient though so will get slower the more items you have in your table. A better way would be to maintain a separate table with just itemid and quantity.

another way...

 

$q=mysql_query("SELECT * FROM `yourtable`");
$i=array();
while($r=mysql_fetch_assoc($q)) $i[$r[itemid]]+=$r[quantity];

 

there you then have an array with all the itemids and their respective quantitys... sort it and yer ready to go...

 

take yer pick...

another way...

 

$q=mysql_query("SELECT * FROM `yourtable`");
$i=array();
while($r=mysql_fetch_assoc($q)) $i[$r[itemid]]+=$r[quantity];

 

there you then have an array with all the itemids and their respective quantitys... sort it and yer ready to go...

 

take yer pick...

 

That's perfect, just what I was looking for. Thanks!

 

Using the usort() function I can sort the array, but it sorts the wrong way down.

i.e.

item 21 = 1

item 18 = 5

item 1 = 9

 

How can I get it to sort the quantity values on the right the other way round?

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.