Sphinxsearch and php next prev functionality - php

I am using sphinxsearch + php for full text search in my app.
I need next prev buttons on my product page. For example I run query "Men shoes" it returns me result of 20 shoes (paging limit is 20), then I click into product and open up a product page, and here I have next and previous products. But next and prev should work to specific to my search query "Men shoes". I store my query in session, and I have ID of product. So how can I find the position of these product and increment or decrement it find products I need.
Is it possible to without query-ing the whole table and looping through to find what I need.

While you can* create a cursor solution, it may be simpler to just use offset.
Page 1 is offset zero, page 2 is offset 20, page 3 is 40 and so on.
where you call setLimits, you can set the offset.
*Basically you need a consistent sort order, and need to store the value of the column, as the 'cursor' for paging, and use it for filtering the next page. If sorting by weight, you store the weight of the last result on the page. Then for the next page, you only want weights bedlow that one. (with weights is even more complicated as there can be many results with the same weight, so you need to do multi-column sorting)

Related

How to go back to same MySql record without using pagination

I have a MySql table with over 1000 products that I scroll through, I understand the SQL query below says "return only 10 records, start on record 16 and display the next 10 records"
sql =”SELECT * FROM items LIMIT 10 OFFSET 15”;
I could keep track of LIMIT and OFFSET within variables and use a pagination function to scroll through my table.
BUT I don’t want to use pagination. I just want to scroll up or down through all my records up or down, even if there where more than 1000 records, I don't care. So here is the problem, lets say product record 567 is displayed on page(A) and I have a link to another page(B) that displays more product information. Then I want to come back to page(A) to same spot, record 567 and be able to scroll up or down through my records. Even the records less than 567. This is why OFFSET is not necessarily what I need. Any Ideas would be great.
If you just use the back button of the browser it's up to the browser to memorize the position and hopefully restore it. If you go back via link an page B use anchor links in page A and have the link in page B point at that. You have to pass the right link from page A to page B of course.
E.g.:
Page A:
...
Product 4711
View details
...
Page B:
...
<h1>Product 4711</h1>
(Description of product 4711)
Go back to the list of products
...

JQGrid Display group field data in one page if pagination is applied

In JQGrid I want to show group data on one page. That is, if containing all of the items in an order would exceed the list limit, then no items from that order are contained and the list is simply slightly shorter than the limit and shows data to previous order. Is there any way?
Personally I think that this should be done at server and not at client grid. If I understand right , suppose you have 10 items limit per page and the response contain 8 from one group and 2 from the other - if the second group contain really 2 items, which will be deleted, this will be not correct.
In case there are more records from previous page - the previous deleted 2 records should be included into the next page, but according to your server response they will be missed.

How can one display different records on different pages?

For eg I have 5 records of same user. What is the best way I can sort and display each one of them on a different page using next and previous links.For eg on clicking previous i must get previous record. Then i again click previous i must get previous to that record and so on.
Pick an order and set it with ORDER BY [order], then use LIMIT start, amount to slice the result.

Numbering products in admin

I have been asked if I could number products in the admin area of OpenCart 1.5.
The numbering would be like:
1
2
3
4
5
etc and continues on the next page.
I am just unsure how to do the count and pull the numbers through into the view.
I've never used OpenCart 1.5 before, however, with a little PHP you could make it work:
Your result array is probably indexed. Add 1 to the index and you have your row number.
For pagination support you just need to use the following equation: (index + (page_size * (page_number - 1)). Then, you'd just add one for each item.
Depending on how your product table is set up, you might also be able to output the primary key of the table. Quite often, those will start at one and increment by one each time. The problem with this, however is that if you delete product #3 then you'll have a gap in your numbers.
Good luck!

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