Javascript/PHP - Get last post ID from livechat system - javascript

I am currently upgrading my livechat system to use the append function with JavaScript.
And in order to get it to work I will need to get the latest chat date or ID somehow.
So I can then do a query like:
SELECT date, message, name FROM chat WHERE id > $lastPostId";
This will then allow me to append all the latest messages instead of updating all of the HTML everytime there is a new message
So basically, my chat right now works like this:
Every second it checks for my PHP file, loadchat.php.
Loadchat.php will get every single message from the database
It then replaces all current data in my div: #chatbox by using .html() function.
And I want to change it to the append function in jquery allowing it to run a lot smoother
Because it only needs to read everything the first time and then append
So my question is, how do I get the last post ID?

Case 1:
Add one more field in your database called timestamp. When you update the chat you can store the value of last timestamp in session.Now each time fetching a new chat just send that timestamp to loadchat.php and query the db to fetch rows where timestamp > stored_timestamp.
Case 2:
You can take a flag in db like is_read. When you insert the new chat the default value of is_read will be 0. When you fetch the data just update the flag i.e is_read = 1. Now in your query you just have to check for the flag.
where is_read = 0.
It will fetch all the unread messages and will do the job.
Hope it will help!!

Related

Get product details from the previous page

I want take list of purchased products. Unfortunately there are no product details on my "order-received" page. Can I get product details from the previous page using GTM?
Order-received Page
I have product details on a previous(basket) page:
function printOrderItems() {
var hrefs = document.querySelectorAll("#form_cart > table > tbody > tr > td.product-info > a");
var result = [];
hrefs.forEach(function(t) {
result.push(t.text)
});
return result;
}
previous page
this is being created when a customer chooses to make an order correct? could you not, using a jquery click event, send that information via an ajax and then load your order received page dynamically from a handler?
In case you still need an answer: Use a custom HTML tag to extract the products on the basket page and store the result in sessionStorage. SessionStorage accepts only a string, but you can use JSON.stringify to convert the array of product names into a string.
On the 'order-received' page use a variable of type Custom JavaScript to retrieve the products from sessionStorage. Use JSON.parse to convert the string back to an array. Then you can use this variable in any GTM tags that you want to run on that page.
Note though that sessionStorage is only available within the same subdomain and within the same tab. If that is an issue you could try to use a session cookie to persist the data from one page to the next. However, cookies are limited in size and therefore are not really suitable for persisting a potentially long list of product names.

How to check changed columns in mysql

I have table user where id = primary key, lastchange = int with lastchange data (in seconds). And i have frontend page with js script. Also all users placed in div like table by pages. So, items has user id and used changed timevalue in attributes.
I want to request all changed user ids in 1 request (1 http and 1 sql). They can be changed by other user on site.
How can i do this? I dont want check every user in page by timer, there is too many requests.
In my mind it looks like:
js do get request with list of users in page in json format [{"id":1, "lastchange":123123},{"id":2, "lastchange":123123}...
Php does request in mysql like SELECT * FROM `users` WHERE `id` IN (1, 2, 3) AND `lastchange` NOT IN (123459, 123456, 123459); (this not works fine, there is no queue for lastchange, it checks all inside braces and result are wrong)
Php return only ids of different rows [1, 15, 22] etc. to js.
Js check every id separately in other request getting full info about user by id
I can check every user in php separately, but i want to know how can i do it with 1 SQL request
Sorry my bad English.
I think you might want to implement the solution differently as per the first comment.... but, if you want keep your current model the SQL you need would look something like:
SELECT * FROM users WHERE
(id = 1 AND lastchange <> 123123) OR
(id = 2 AND lastchange <> 123123) OR
...
This will keep the id's and the lastchange values that relate to each other being compared properly. It is pretty ugly SQL but not hard to generate in a loop.
Sorry my stupidness. I think i solve this.
Js request api with only user id in page
php response json like id, lastchange
js checking lastchange value and id in page
js request full data about changed user replaces old in page
Thx all for helping

Firebase range query

Im using AngularFire+Firebase and have data at firebase-database.
Im trying to paginate Data with Smart Table
My problem is that I dont know how to range query without specifying any child i,e fetch records from record # 25 to 35
Below query gives me first 5 records
var queryFIrst = visitRef.startAt().limitToFirst(5);
$scope.Visits = $firebaseArray(queryFIrst);
now Im trying to get records next 5,from 6 to 10 and I tried below
var queryFIrst = visitRef.startAt().limitToFirst(5).endAt().limitToFirst(5);
$scope.Visits = $firebaseArray(queryFIrst);
but it giving error that startAt and endAt can't be used like this with limit
In general pagination is not a good fit for Firebase's realtime data model/API. You're trying to model a SQL SKIP operator, which won't work with the Firebase Database.
But if you want to model pagination in Firebase, you should think of having an "anchor point".
When you've loaded the first page, the last item on that page becomes the anchor point. When you then want to load the next page, you create a query that starts at the anchor point and load n+1 item.
In pseudo-code (it's real JavaScript, I just didn't run it):
var page1 = visitRef.orderByKey().limitToFirst(5);
var anchorKey;
page1.on('child_added', function(snapshot) {
anchorKey = snapshot.key; // this will always be the last child_added we received
});
Now when you want to load the next page of items, you create a new query that starts at the anchor key:
var page2 = visitRef.orderByKey().startAt(anchorKey).limitToFirst(6);
A few things to note here:
You seem to be using an approach from the Firebase 1.x SDK, such as an empty startAt(). While that code may still work, my snippets use the syntax/idiom for the 3.x SDK.
For the second page you'll need to load one extra item, since the anchor item is loaded for both pages.
If you want to be able to paginate back, you'll also need the anchor key at the start of the page.
Is that what you needed that time?
visitRef.orderByKey().startAt("25").endAt("35")
I asked a similar question Get specific range of Firebase Database children

how to validate existing records with new records in frontend using Jquery

Requirement:
Should add new holiday into holiday table and check into the database.If there is no holiday in database, then it should be added. Else it should show an alert, holiday already exists.
I have been able to Successfully add holidays, but duplicate records are also able to add.
How to validate this in front end using Jquery/Javascript.
Thanks in anticipation.
you need to make an ajax call to server every time you add an holiday and pass the entered holiday as a parameter to this ajax call, and then on server check if the received parameter(holiday) already exists in DB , return true or false in case of if record exists or not. In the ajax call success function check the response from server , if its "true" e.g record exists in DB , then make an alert.

get running count (in Cognos Javascript)

How can I get the number of rows that will be returned by a sql for a Report Studio report at run time in Javascript? The purpose is to terminate the report as it exceeded the row limitation and notify the user an alert message to re-enter prompts that would reduce the volume of data. Doing so the users will be able to run the report with new prompts without having to re-run the report.
I could find couple of options to limit the maximum rows returned but all they do is terminate the report with an error.
I can suggest another solution. Without Javascript.
Create condition block based on row count.
Add a Query Item with value
Count([Your field]) for report
Add Singleton with your query and this new Query Item.
Set Query property for Page to your main query.
Set Processing property for your main query to "Limited local"
Add Conditional Block with new variable e.g. boolean. Value
[Your query].[Your new field] > 1000
Put you report on a "No" page of conditional block, and a message about row limitation on a "Yes" page.
This solution will work even if you use Excel output.
You can try to add HTML Item to row. Source Type = Report Expression
Put inside
'<script language="javascript">
if ('+number2string (RowNumber ())+'>500){
//Put here code that stops page loading
}
</script>'
But, I don't know how to stop load Cognos page. Usual ways don't work.
You can hide or erase a part of report (whole List), but Cognos load all data anyway.

Categories