game logic for bingo - php

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.

Related

A lot of prepared modals or one modal + ajax requests?

I'm building a web app using Yii2 (php, mysql). Users can click on others' game results to see what item the user used for this result. There can be from 100 to 1000 results displayed on a single page. I don't know which option is the best in terms of speed for the page and the server :
1- On page load, a modal is loaded for every result and displayed when users click on a result. This way, there can be from 100 to 1000 modal loaded on the page. Is this too heavy considering that only a few of them will be used? Or even none of them sometimes.
2 - Load only one modal that is brought up when users click on any results and dynamically adjust his content using an ajax request to the server depending on which results was clicked. This way, less code loaded on the page but more requests to the server.
First option is easier to code but I think the second one might be better for page load. I'm far from expert in terms of page size and server requests handling, so I'd like to get some opinions.
I think you've answered the question yourself.
The second option is right. Only one modal (per type of action -- edit, view, etc) should exist. Then use ajax to load data only when requested.
Your users will thank you. The page size and load times will be significantly better.

How to execute mysql_fetch_row() upon a button click

I am tying to put some user data from the database using php.
The database contains so many record and i am displaying it using mysql_fetch_row() and this gives me the fields of first row.
Now the problem is a have two buttons namely next and previous. And when a click next the fields in the next row has to be obtained the they should be inserted into page without loading the page once again. I am facing problem with this.
I am using php for server side programming and html, css for client side programming.
I think that no one will give you the answer. Too many to write. You need to read about ajax (this is the technology that will allow you to do some action without page reloading).
You need to write javascript(you can use some library like jquery) function which will open some php page with the data that you want and then update your site.
Here you have some nice tutorial:
http://www.w3schools.com/jquery/jquery_ajax_intro.asp

PHP autoupdate form data without pageload

I run a php script game site, and there's a section of it where a player can distribute his 'skill points' in 'Attack', 'Defence', or 'Stamina'.
At the moment, it's just a basic HTML/PHP form with a + next to each stat, so if the user had 100 points and wanted to put them all into ATTACK, he'd have to click (and pageload) 100 times. Obviously that's silly.
I want to be able to make it so the player can distribute them (without page reloads), then finally click SUBMIT once he's happy with it.
Could anyone point me in the direction of what I need to do this? Is it some ajax or javascript? I suppose I could do form fields and clicking the PLUS would increment each field..
It sounds like you need an onclick() to call a javascript function for the plus button, that increments the value in javascript, updates on the page and checks that the user has distributed the points correctly (i.e. distributed 100 points or under in your example) and then have another button at the bottom of your points distribution that sends the data to a PHP page that handles the storage of the values.
If you wanted you could then use ajax to send that data to the PHP page without having to reload the page. I've found the W3schools site adequate for teaching basic ajax usage: http://www.w3schools.com/ajax/default.asp
(But remember to check the user input on the server side as well, don't trust user input!)

What technology use to make a webpage to update in real time?

I want to make a webpage where an user can add the title from a book he has read. These changes are reflected in real time on a list that contains all books he has introduced on the database, without the need to press any "reload" button. By example: there is no need to refresh (F5) the page to see the last book added.
I don't know if I can do this in PHP or in any other language, so I would like to know which is the best suited for something like this.
Thank you.
I think you are looking for Ajax. Would be able to asynchronously update the section of the page (the post in this case) without the need for page refresh.
You will want to do this with javascript, using the onchange event, and for a discussion on this you can look at: Call Javascript onchange event by programatically changing textbox value.
Basically, you react to the data being changed, then just send it immediately over using ajax to the server, but, you need to be aware of two things.
First, how will you handle errors, such as there is no book with that title, or the length is too long. I tend to put the error message in or by the place where they had the bad data.
The other is that you need to pass back the id when the data was inserted, so that when they change it again you can just do an update, so you will need to store that. I tend to put the database id I need in the element id, but you can keep it in an array in javascript, since it will maintain state for you.

PHP Poll system to each article

the Poll system have two part(the user cast poll part and the result show part) the cast poll part has two buttons. which is up and down. the result show part using five star to show the average result which casted by all the vistors. say concisely: the poll system has two button, on is up, another is down. when a vistor polls up/down. it will show the average poll result of the article. which is displayed by five stars.
You need at least Javascript for that. To update the database dynamically when a button is pressed, you need some AJAX code.
Glance over http://jquery.com/ There are tons of examples, plugins, discussion there.

Categories