How to define when you should hide "Show More" button? - php

I have a mysql table of products.
I have a product listing page where User has a button "Show more" to load more products on the page.
After clicking on this button jQuery calculates how many displayed products are on the page (N) and makes an AJAX request to the table for getting 20 more products (N+20).
Now I should create a trigger when I should hide the button.
Which trigger I should choose? What is the best way to define that there are no products.
Please, help.

I have no idea how have you reached that functionality since you are not sharing the code and thus I cannot give you the most accurate help, only this one:
when loading the page and retrieving the first X rows from DB, retrieve the amount of all rows (matching the search criteria) as well
make this count visible to the template
in jQuery, count how many rows have you already loaded
this means, that on page start you are displaying X rows, the amount of total rows is Y, and the number of currently displayed rows is Z
immediately after the page is loaded you need to compare if Z <= Y then do not display button else display the button
if the button was displayed I guess you are listening to click event on this button and then performing your AJAX request, here after the AJAX is successfully completed, you need to do Z += X and again compare if Z > Y then hide the button
That should be your logic, now turn it into your code.

There are 2 ways I guess,
The simple way would be counting number of response rows that's returned by your AJAX function. If it's smaller than 20 then hide the button.. However, if your records number is a multiple of 20 then there will be a glitch in the last response, because it won't hide the button.
The perfect way is to calculate number of records in your table, then save that data in your js variable. Then compare that variable value with the number of times you ask for another record.. If they have same value then hide the button, if not then show the button.

It depends on the frequency of product list updating.
If the amount of products changes often (say, more than once a day or so), I would execute a query on each AJAX request, checking if I have more items (for example, checking if N+20 >= count(id) of products), and on the callback hide the button.
Otherwise, I would just inject the amount of products as a JS parameter into the page, validate against it on each click (N+20 <= count_products), and save myself some AJAX \ SQL loading time.

you can get the count of all lines using PHP and save it in an hidden field or in a variable after that you can test (using jquery) to see the current number of records in the page (you are already doing that ) then test it with the number in the hidden field/variable and show/hide your button

Finally I choose next variant:
I want to load more 20 products.
In backend code I switch this number to 21.
So then in jQuery I show just 20 BUT I check if there one more product I should NOT hide the button.
That's it.
Thanks guys anyway!

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
...

Dynamic Column Selection Menu and formula generator

How to create a dynamic menu in php html page wherein user can do all of following from front end:
I want that to create a two column window that displays different db col names from different tables. First window shows options for selection while, second window shows options selected and arthimetic operations on them from front end. We can save and create the output of these operations over selected columns into a new table or column of output. We can reuse this formula every time..since we have saved it by simply pulling the result col and echo it on a page.
Example: user selects a database column (e.g. "net profit") from first window and by clicking simply moves into 2nd window. Next he click "subtract button" and a minus sign appear in front of "netprofit" in 2nd window and then selects "tax" from first window and moves it to 2nd window. Next he clicks "divide button with / sign on it" and then clicks "total assets" from first window . Next he clicks save and winow prompts to assign a name to out put of the above..user names it as average profit over assets and saves it into new table. The output in table therefore will be processed based on this custom formula creation window and we can create complex formulas and save thier output and extract as required. The formula we created in above example is as under:
(Net profit before tax- tax)/total assets = Average net profit over assets.
Please guide on highlevel, how i can accomplish this . Also.tell me if there is an Api that can help achieve this quickly or I need to do all coding from scratch.
I believe one solution is to implement the window using checkbox list for colums pulled from db and embed them in scroll down div.
Next for all selections made take them and echo in a formula feild. For each add or subtract or multiply or divide button ..take respective values i.e + , - , × , ÷ and echo them in respective field. Once the formula is constructed .. post it and store in a new variable $ result. Next use it in a function or simply save/ insert it into a new table called formulas. Next extract values from the table and display/ echo on screen.
Any suggestions??? Am i thinking correctly?

on click link (More) Show more table field data ..??

There is large number of column in my database. I fetch all this on my webpage.enter image description here Is that any way to show 4 to 5 column with MORE link. On click remaining wiil be display on the same page same table raw wise
Ajax is the best solution for it. Or You can do that by jQuery hide() and show() function. Hide all after 4/5 column and append a button which will show rest of the column when clicked.
check it - https://jsfiddle.net/iqbalbary/sjnybu05/

Update Magento stocks with CSV and PHP script

Just found this great article: http://www.blog.magepsycho.com/updating-product-qty-in-magento-in-an-easier-faster-way/ but something works not very well.
When I have a CSV with the columns "sku" and "is_in_stock", and I use the script, it also sets the "qty" to 0 when "is_in_stock" is 0. How is that possible? Because I only want to update "is_in_stock", and not the "qty".
Can you help me with this?
Thanks in advance!
The linked script ignores the header row and treats the second column as the quantity, regardless that you named it "is_in_stock". That is why "qty" is set to 0.
If all you need is a quick way to mark some items as unstocked:
Go to the admin, then Manage Products.
Search for those you want to change and put a checkmark next to them.
In the Actions box select Update Attributes then click Submit.
On the Inventory tab click the Change box for Stock Availability then select Out of Stock.
Click Save.
You could use this script instead, http://www.sonassi.com/knowledge-base/magento-kb/mass-update-stock-levels-in-magento-fast/
As a side note, if you have 5000 rows in your CSV, you should retain the previously uploaded file, then perform a diff on the rows that have actually changed - that way, you're only going to be modifying a much smaller number of products.

Pagination with ajax

I've build an application (with PHP, Codeigniter and jQuery) that uses ajax for pagination, similar to how Twitter made it back in the day (clicking a button to load in more data).
It's all quite well, but there are a few issues however.
When there are no more posts left to load in, I want the "load-more"-button to be removed. However, right now I can only check if there are any posts left when I click the button, and the script returns null.
This is how it is now: Let's say that there are 14 posts in the database. 5 is loaded per default.
1 - click load-more, 5 more posts are loaded. 4 remain.
2 - click load-more, the remaining 4 posts are loaded.
3 - click load-more, no more remaining posts, button disappears
But I want to get rid of step 3, the application should "be aware" of that there are no more posts left to render at step 2.
I am sure there are a simple way that I haven't thought if yet...
simple. select 6 posts instead of 5 and display only 5.If no of posts less than 6 dont display more button.
if ($num_rows < 6){
//remove more button
}
You could set a global Javascript variable holding the amount of total posts something like this:
var total = <?php echo $total ?>;
And after the call compare the amount to the total:
if($('.post').length == total) // .post being database results
$('#loadmore').hide();
Return a JSON object with one property that is an array (the entries) and one property that is a bool "EOF" or such to indicate when there is no more entries to load.
if(response.EOF)
//Hide button..
Ohh and add the id of the last post from current pagination in the AjaxRequest.
That way the server gives you say 25 posts from the last one it gave you.
Then you dont get offsets if a new post is added during pagination.
you could try something like this
if ($num_rows < 5){
/*what ever you want removed */
}
at the end of you loop
Please, please, please - do not do pagination in Ajax!
It is pain in the a$$ to navigate such pages natural way, unable to use back button or reload button or set a bokmark or anything!
not to mention clients that doesn't support js at all

Categories