Simple AJAX MySQL Query Load - php

I've asked this in a different post but finally realized what it is I'm actually trying to ask.
Is there anything simple that will allow me to load 5 rows from a MySQL table and then with an AJAX driven button, show 5 more, etc, etc (sorta like Facebook does with their 'View Older Posts' button (I am using PHP).

You can retrieve all of your data and store in DIVs, placing 5 records in every div and separating divs by id and then use javascript to show div on each click.

Create a JS var that stores your current record status.
Every time you load the 5 records, pass this value through ajax to your script to use in the MySQL "LIMIT $start, 5 "portion. Everytime you load, increment the variable by 5 and pass it back to your PHP page. It'll grab the next five, limiting 5 records.

Related

How do I make a simple pageview counter in PHP that is universal to all pages?

I'm trying to have a line of PHP that counts all pageviews on any given post. The code will be included in each blog post with the authorbio.php.
So I'd like to have a pageview counter that says the number of pageviews on [current page].
How do I accomplish this?
It's pretty simple. If you want it for each post, just make a field in your database named something like "views." Every time the page loads, make a post request to your server to increment that views field by 1. Display that at the bottom. If you want to show a "live" counter, you can query the page count with ajax every so often and display the results from that.
If you wanted to just keep a site page count, then all you need to do is store a number that goes up everytime the page loads. Have it persist by storing the number to a file or database. Retrieve, increment then update. If you need the counts to be specific to a page, then store the URL as well together with the count.
I guess you could also use something like google analytics to keep track of pageviews.

game logic for bingo

I am using php, html, css to create a bingo game (There will be nxn boxes. It will be filled in random order for 2 players. Players has to select numbers one by one. One who fills up rows or columns or diagonals five times wins the game).
I created player1.php and player2.php. I am having 9 boxes and populating it from databases. If user clicks a button I'll update the corresponding values as 0 so that when page loads I'll disable the button if the text is 0.
If player1 clicks a button in player1.php, it should reflect in player2.php. How can I do that? Is that possible by reloading page every second? Is it good to do that?
As short answer: no it is not.
Pure PHP is not the most suited for interactive game like that but there are few things you can do.
You could use javascript with Ajax to do this. Let's say you have X players (each using playerX.php although i would put it into player.php and determine each player by GET parameter or something). When one of the player clicks a link, it goes a server-side script, in PHP (possibly using Ajax if you want), that updates your database.
Now the tricky, each player page 'asks' the server every certain amount of time for the state of the game, and updates the page accordingly. The easiest way to do this is create a script (lets call it state.php) that will output data from your database using JSON. You can read the request using ajax and update your page using javascript.
I suggest you read about jQuery and Ajax (which can be also used with jQuery).
If you want to avoid having to refresh the page each time, learning javascript and ajax is your best bet.

Load big data with webGL

I need to load a lot of data fetched from a mysql db in my webGL page. The approach I found is this: http://www.w3schools.com/php/php_ajax_database.asp
although the idea of a first fetching with PHP and then passing it to the webGL page isn't exciting, any alternative?
It is not very clear what you are trying to do, but if what you want is to display data to the user and you have too much data, than you should use paging/pagination.
This is the process roughly:
You need to read a portion of the data (lets say 100 records) and display it to the user in page one.
you have buttons that enable the user to move to other page.
on click, you send an ajax request with the pressed number and you know that you need pressed number * 100 to ((pressed number *100) + 100)
for example:
first page displays rows 0-100, user clicks next you get rows 100-200 and display them.
first page displays rows 0-100, user clicks number 4, you get rows 400 - 500 (4*100 to ((4*100) + 100)).

jquery ajax pagination with php question

First off I would like to say that I have done ample research on jquery pagination tutorials and scripts and the ones I have tried had worked, but just not the way I wanted them to.
What I am looking for is a jquery+php pagination that has 5 buttons. One for the first page, last page, next page, previous page, and manually enter a page to go to (which would also display the current page). I am new to pagination and have no idea how to integrate or alter current scripts, but I'm willing to learn as long as I can have the 5 buttons stated above and use php mysql queries. Thanks for the help!
UPDATE: I can do this with only php (would have to reload the page) but I do not know how to do this with jquery
UPDATE2: I figured out how to have the user manually input page number, but I still do not know how to do the other for buttons. For the next and previous buttons, I think I need to return the current page, but I have no idea how to do this :/
The trick is you use limit and offset parameters in your mysql query. Depending on what page number of the result you are serving, you can modify the ordered mysql query to fetch the result from that offset. It's pretty simple to work with that after you understand how to paginate in mysql queries.
In order to accomplish this you simply need to enable your application to make an ajax call to fetch the 'more info' results. That ajax call should take two parameters, an OFFSET and a LIMIT. Simply store these values locally in the application (via hidden elements, JS cookies, etc) to reference. Next time the 'more info' button is pressed you increment the OFFSET by the LIMIT and repeat.
NOTE: It's important that your query that is fetching the results is also using an ORDER BY. If you are relying on the server to order the results the same automatically you could potentially repeat results if the data is being updated often.

display records randomly one after another

I want to display 5 records from a database with in a td one after another (about 60sec). How will i do this, with out the need of page refreshing? I don't know any idea about ajax
So you want the contents of one cell be replaced continually? In that case, you have to use AJAX to retrieve your data and a timer to trigger this retrieval and contents change.
You don't have to use AJAX. You can put the content you want to refresh in an iframe, and the framed webpage will have a meta refresh header. This type of thing has been done for years before AJAX existed.
by using you can achieve
Algorithm :
Create the td with some id.
Send the ajax request( for require time of interval )
Get the random rows from by ajax , and put the values
into that specific row id using innner.HTML
That's all it will work.
If you want to know how to use ajax refer this below url
http://www.w3schools.com/php/default.asp
In this case, have to use AJAX

Categories