Sorting by sql_attr_float in Sphinx - php

I just added price and sale_price attributes to my indexes in sphinx and it seemed like it was sorting just fine using, $s->SetSortMode(SPH_SORT_EXTENDED,'price desc');, however after a few pages the price shot back up again.
For instance, page one showed prices $1,000 - $800, page two showed prices $800 - $500, page three showed prices $500 - $200, then out of nowhere page four showed $300 - $100 or some random assortment of prices.
I'm wondering if sphinx is possibly still sorting by weight event though I haven't explicitly told it to... Does that sound like the problem?
I'm open to indulging any other ideas as well. Also, I definitely reindexed and rotated all indices and sphinx is not returning any errors.
Thanks in advance for your help.
Here are all the options I'm currently using: (update for #barryhunter)
$s->SetMatchMode(SPH_MATCH_BOOLEAN);
$s->SetSortMode(SPH_SORT_EXTENDED,'price desc');
$s->SetFilter("is_private", array(0));
$s->SetFilter("is_deleted", array(0));
$s->SetFilter("site_type", array(1));
$s->SetLimits($start, $count);

The answer was suggested in a comment by #barryhunter!
I added a new integer col to mysql called sort_price and filled it with (price * 100).
I then added that col as sql_attr_uint in sphinx and the sorting works perfecto!!
$s->SetSortMode(SPH_SORT_EXTENDED,'sort_price desc');
Thanks Barry :)

At a guess, are you using a Group-By?
If so, then SetSortMode does NOT set the order of the final results. The third param to setGroupBy does instead.
read the documentation on Group by to see how the two sort orders work.
Otherwise what column type do you store the price? Maybe its not getting converted to sphinxes float type correctly.

Related

Flagging table entries not to show again after that row of entries has been selected/used

I am a beginner in programming and I don't know if my title suits my issue, but here is what I am dealing with:
I made a simple inventory program. I can insert, edit and delete entries and it all works perfect.
For my next challenge I need to make it that when an item gets sold out of the inventory that my database will flag that entry so that it will not show again in that list in the application. (Makes sense right? when u sell a product it can't still be there cuz its sold)
But since I am a beginner I have no idea whats o ever how to research this cuz I have never done this before! That's why its a challenge :)
Can you pls tell me what I should research to learn about how I should be doing this ?
Thank you in advanced for ur advice and attention!
And my apologies if this question is off-topic to this website but I don't know where else to ask since this website is so awesome!
Cheers
If products are coming INTO your inventory you would need to scan the products and the amount of how much has come in right? When you do this for a product you would just update the record with the ID of the product that has come in(By scanning the product you should get the ID I assume) and the update query could look something like this:
UPDATE myTable SET productStock=productStock+amount* WHERE productID = ID*
*Amount stands for the amount that came INTO your inventory.
*ID stands for the ID of the product.
The above was an intro, now your question which you can lead off of the above, when a customer wants your product, and clicks on it, orders it, you can do the UPDATE query again, but now MINUS 1 or whatever the amount the customer orders(again by doing multiple amounts you should always check if you have it in stock, but that's another part)
UPDATE myTable SET Stock=Stock-amount* WHERE productID = *ID
*Amount now stands for the amount that the customer orders OUT OFF your inventory.
*ID stands for the ID of the product.
When your "productStock" turns 0, you could simply modify or add a query which will only show values where the "productStock" IS NOT 0.
Example:
SELECT * FROM products WHERE productStock <>* 0;
*<> This symbol stands for NOT EQUAL and in some versions of SQL this is written as !=
Hopefully this would help you on your way.
EDIT:
Why you should not 'flag' or 'remove' the product out of your DB is because when it returns you don't have to fill in the entire product again...

How to use a combination of PHP and MySQL to get Related Products

I've a large table of products, and want to get related products based on a similarity of some kind - whether it is product code or title yet I am yet to decide.
What I want effectively is an output of the 4 most likely matches for a product based on title or code.
Example:
Product Code Related Products
JC83022 JC83021, JC83020, JC83029, JC8300
So I would ideally like a field appended to the product table which does some kind of string matching and returns the codes for the 4 most likely matches.
My idea is that I can use a MySQL query to perform this, but looking around PHP may have some better functions - maybe I could have a function on the product page that returns the products with the nearer levenshein distance - it's just how to query that against a table of over 30,000 products.
Cheers in advance.

PHP Ranking Products

I want to sort my products based on three criteria
Rating(5 star)
Comments
Weight of User favorite entry for this product
Does anyone know the better way to rank my product based on this three criteria ?
You can sort using a whole range of interesting criteria:
Most popular (i.e. those with the highest sales figure)
Most viewed (those with the highest viewcount)
Editor's favourites (ones which you manually select)
Most recent
etc.
Seems to be good practise. Maybe you can also rank by search engine reference (visitors refers from search enginge)

How do I find the qty of products ordered for a specific attribute option in Ubercart?

I'm trying to make a custom order summary module for Ubercart/Drupal that will display information something like this:
S M L
Prod1 10 20 40
Prod2 0 15 0
where S, M, and L are options for a custom attribute (size in this case), and the numbers are the quantities ordered of each product of each size.
I notice that the selected option is specified in the data field for the product in the uc_order_products table of the database. I can figure out all the arithmetic and everything if someone can tell me a good way of finding all the uc_order_products rows which contain a specific option for a specific attribute. I'd even be happy with just a simple way to return the data field as an object or associative array.
Alternate solutions involving Ubercart's API are also welcome, but from what I saw in the docs, there don't really appear to be functions for this kind of thing.
Thanks!
I found my answer in PHP's own unserialize(). I'd never had any experience with serialization in PHP, so I didn't recognize the format when I saw it.

PHP> Sort query results by name while letting each letter be on the top sometimes

I'm currently working on a site that will display a list of online shops,
Each shop will be stored on my database and I'll be using PHP to select and display them.
But since those shops will pay me, I want to let each shop to be on the top of the list sometimes,
(for example if the shop name starts with a "Z", they will probably complain for being on the bottom of the list all the time, so I want to keep it fair).
So I thought about letting each letter be on the top of the list for an hour, but i have no idea how to do that..
Is that even possible?
Thanks in advance!
I'd show a separate box and call it "today's pick" or something with just one shop in it. That way you can push the shops starting with "Z" to the top once in a while and at the same time keep the user experience of a list of shops which is sorted normally.
Then use the database to save which shop has been in the "today's pick"-box how many times to get them all up there equally.
There's no sane way (that I'm aware of) to handle this directly in SQL without adding a "priority" field to your schema (although it's possible, it would be convoluted at best). That said, here are two suggestions:
Modify your schema
Simply add a "priority" field to the relevant schema and sort by priority, name (or whatever the default is). You will of course need to reset the priority field every hour, but this is a fairly trivial task.
Handle it in PHP
Carry out the query as per usual.
Grab all the results into an array.
Re-prioritise as required based on the current hour. (You'll need to array_splice the item(s) you want to bump out of the array and then array_unshift them to the top.)
Output based on the array.
This will of course become more convoluted/less efficient if you need to handle pagination, but the basic idea is the same.
A nice solution would be to add another column to the database with the shop names, and call it something like "last_shown" then when you show this shop, update the column with a timestamp, and each select do something like:
"SELECT name,link FROM shops ORDER BY last_shown DESC"
then in php you could check
<?php
if($row['last_shown']+3600 > now()){
//run select but in ASC order
//update the new row's column to the current timestamp
}
?>
that way it will only update once an hour, but otherwise it will keep selecting the shop at the top of the list for the hour
sorry it's a bit of a mess i just typed this out quickly at work
You can add
1) a extra column as shown_times in schema
2) order by shown_times asc
3) & as a shop is shown you would +1
or
Another solution :
You can even use ORDER BY RAND()

Categories