Tic Tac Toe with PHP [closed] - php

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
i'm getting so stuck with my homework. .
how to make tic-tac-toe with php_self?
So, i have 9 button with the number range from 1 to 9. then, when first button is clicked the value will be changed with 'O'. after the player with 'O' symbol had pushed the button, then turn to 'X' symbol appeared if user click the button. the process will be continued until the same symbols appear on horizontal, vertical, or diagonal series.
I hope somebody could help me :(
Thanks

Am not sure if you need to re-event the wheel .. what you need to do is to change the interface ...
There are so many solutions online that can help you with this
PHP & HTML & Javascript
http://code.activestate.com/recipes/276962-php-tic-tac-toe/
http://t-dev.iglu.cz/
http://scripts.franciscocharrua.com/javascript/tic-tac-toe/
http://www.mystcommunity.com/board/index.php?/topic/11367-tic-tac-toe-in-php/
PHP & HTML
http://refactormycode.com/codes/1524-simple-tic-tac-toe-php-pure-html
http://jadendreamer.wordpress.com/2012/01/31/php-tutorial-2-player-tic-tac-toe-game-no-database-required/
Conclusion
If you select any of the script that you are interested in then you can comeback if you are having any difficulty in making it work

Well in the process of answering this question I pretty much did your homework for you. But you should pretend that I have not and I am going to forget about that as well and just focus on what you have asked.
Also, PHP_SELF by itself means nothing. $_SERVER['PHP_SELF'] which is what you are most likely referring to pretty much points to the page itself that you are writing code in. You make it seem as if there is something that is going to help you to make tic-tac-toe but I do not think so. Maybe you can correct me if I am wrong.
You seem to mention that you already have an interface that you are working with. I do not have the code for this interface so I am going to assume that it looks something like this:
http://jsfiddle.net/vyyXP/1/
the form html tag usually contains another attribute called action. You usually use this if you want the data from the form to go to some other page. Since we don't want to create another page we can either omit this altogether or use something like:
<form method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>"></form>
This is the only place your PHP_SELF comes in. Again, if you know something else about it, let me know.
So.. now lets actually start solving this problem of yours now.
Step 1: Figure out how to get and use the button clicks.
Every time you click a button. The form submits and the page reloaded. If you have knowledge about headers, it'll add an extra pressed=1 or pressed=2, etc every time you click a button and submit the form. This 'pressed' keyword comes from the fact that our buttons have name="pressed" as an attribute. (Notice that all buttons have the same name!).
We will need to use this data and figure out what the person actually clicked.
Since I'm using <form method="post"> you can get this data by using <?php $pressed = $_POST['pressed'] ?> alternatively if you used method="get" you would have had to use $_GET['pressed'] instead.
As an exercise just display an 'X' on whichever button the user clicks on. You should dynamically generate the html code for the buttons inside the form to make your life easier.
<?php
for ($i=0; $i<9; $i++) {
echo '<button name="pressed" value="'.$i.'">';
if ($_POST['pressed']==$i)
echo 'X';
echo '</button>';
if ($i!=0 && ($i+1)%3==0)
echo "</br>\n";
}
?>
Step 2: Figure out how to remember data.
Since you're using php. I'd recommend that you use php sessions. You basically just need to put session_start() in the beginning of the file and then you can store values into the $_SESSION variable and php will remember then next time you visited the page. You can use this to count how many times a button has been pressed so that you can alternate between O's and X's.
Figure out how to destroy sessions. (Make that reset button work). This is pretty important.
You can also use cookies or write to a file if you want but sessions are probably easiest thing to use.
Here is an example of a simple counter.
<?php
session_start();
$count = 0;
if (isset($_SESSION['count']))
$count = $_SESSION['count'];
echo $count;
$_SESSION['count'] = $count+1;
?>
And.. you're done!
This is probably everything important that you need to do. The rest is just implementing how the game tic-tac-toe works. Let me know if you need any clarifications on anything I have written or if you need more information. Hope this helps!

Related

php - different design of webpage for logged in users [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I just have one question. I looked through one book and the Internet and unfortunately didn't find an concrete answer. So... I have a webpage where user can log in. If the user is logged in then the bar at the top of webpage is different(user sees his own photo, name etc.). I know how to use sessions&databases in this case, but I don't know how to make this two different websites. I mean.... in the home site of my whole webpage i can write sth like (in php):
if(isset($_SESSION["User"])) ..... .
But what then? I should somehow hide the html for unlogged user in "else" and part for logged user in "if" or should i create a whole new site for logged in users and redirect to this site if user is logged...? Please, help me.
It seems that you need to spend a little time looking into PHP deeper. My advice would be to learn about including PHP files in order to create a template system (so you would have a base PHP file with the HTML/PHP that is on every page (like a master page) that would include the code:
if(isset($_SESSION["User"]))
{
// Do code for logged in user...
}
else
{
// Do code for generic user...
}
Although that is a really rudimentary example, you could have a global variable if you need things on specific pages too. If you have a more specific question about implementing it, feel free to ask.
one cool thing that you can do in PHP is include html "inline". eg:
if(isset($_SESSION["User"]))
{
?>
<p>Welcome User! <?php echo $_SESSION["User"]; ?></p>
<?php
}
else
{
?>
<p>Please login to see all features...</p>
<?php
} ?>
You should first set the session variable's value(eg. $_SESSION['logged_user_id']) to some value as per your website. Then assuming that you have only one page, you will have to include many php codes at the place you wanted to display user image, Hello xyzuser etc content. You will have to use "if/else" statements to check if the session variable for some user is set or not. If the session variable is set then it will be using the if block statements where you can write code to display image of the user corresponding to e.g. that logged_user_id using your db. Otherwise it will use the else part that will display your default content.
You will have to perform "if/else" checks at each place where your content has a chance to change based on the user status i.e. use the "if" statement to check if the user is logged in or not if no user is logged in then it won't go to the "if" block statements and execute the "else" part for default display. Ok.

Multiple html form and submit [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm wondering how to differentiate different submit and forms to a certain php function.
Currently I have 2 forms in my page, but each submit button will do 2 different thing.
I've tried using ISSET to control the submit but if failed, it always refer back to the same function.
Initially what I want to do is I wanna have the user to key in some verification info and submit the info to the database to do some checking (the data is in the database) and update the result on the same page, then only they proceed to submit the whole updated form to the payment gateway.
Assign a name to your submit button like
<input type="submit" value="Update" name="first_form" />
<input type="submit" value="Update 2" name="second_form" />
So, now you can execute a particular code like
if(isset($_POST['first_form'])) {
//Process first form
}
if(isset($_POST['second_form'])) {
//Process Second Form
}
I just read your question again, not getting much, but it seems like you want to carry values to another form or you want to show forms only if the first form is completed, so the best way to do this is to have a session var, which will hold the users form data, so that you can carry it on another page, also you can set flags from which you can show particular data to the user, for example, if user completes form 1 set $_SESSION['completion'] = 1 so you can use a condition to check whether session var isset, and if it is, whats the value and show the content to the user accordingly.
Due to the fact that PHP is a server side language, it only runs when the page loads. If you want information on the screen to change without reloading the page, you will probably need to use Javascript or Ajax.
if you use 2 form on basic html page, that wont do, because once it submitted, the page reloaded..
ajax is the aswers..
var dataSet={ SERIALIZE_VALUE FROM FORM1 };
$.post(URL_TO_PHP_ACTION_SCRIPT,dataSet,function(data){
// check the return value here if its valid, then enable the 2nd form for submitting
});

Is using an HTML Form the ONLY way to post a $var to $PHP_SELF?

IS there a way to post a $var to $PHP_SELF so that I can use it further down the script?
After 2 hours reading dozens of questions which helpfully appear in the sidebar to the right,
it became apparent that they pretty much all assume an HTML Form has been / will be
activated.
But psuedo~code of what I need looks more like this:
< php
$someVariable=y;
$otherVar=X;
// and the usual setup for accessing the `$_POST` of php:
$HokeyDino=`$_POST`["SendOFF"];
$SendOFF=101;
// etc. and then come to a point where I need the script to just automatically post a value
[ the lack of knowledge ]
// which if I had tha codez!
// would permit the use of that $var, $HokeyDino ...
if($HokeyDino==100){
// do stuff
}
I don't like looking foolish, but gotta ask away, because I figure I have missed learning some elementary aspect of programming, being self-taught so far, but not knowing what might be lacking makes it hard to go look productively.
Thanks very much!
EDIT // Clarification.
Wow, this is amazing. half an hour, 24 people reading the question. Blows my mind.
Right. What I have gotten done so far to give more background:
A php script which uses fopen to create on the fly another php / html page, and all the
code on the Authouring originating script, to write (a+) to the newly created temp page, the whole thing.
From a loop on the authouring page, I have code for retrieving POSTS I send TO that temp page, and that code gets written to a very temp page... then I cause the first part of the page to be written, to get placed on the Temp page, by put_contents etc.,
Next, from another loop on the Authouring page, I write code which item by item matches the things which were included in the < head > of the Temp page.
Anyhow, without graphics, it's a bit tough to explain. What I have at the point I have gotten to so far, is the newly created/assemble Temporary page, can be accessed as a WebPage,, and a button click on it, will successfully POST a value back to the originating/Authouring script.
Here's the tricky part: There isn't any way I was able to devise, to dynamically create code ON THE AUTHOURING page, to recieve POSTS from the Temp Page.
But I realized that if, in the Loops on the Authoring Page, I was able to $PHP SELF post a
string which would be the code for creating a * $Var = $ POST; to catch the values from button clicks on the TEMP page, it would work.
Critical, is that the Authoring Page, doesn't know how many buttons will be made over on the Temp Page ~ that depends on the number of items in the database, which the loops are reading and translating into code which builds the Temp Page.
So, there is no way to hard~code, on the Authouring Page, all possible code for recieving posts, but I could use one Universal $Var= $ POST[ X ] if I could generate it on the fly in the loop, on the Authoring Page.
Hence the need to write code which will $SELF POST, and have it triggered just by normal programme flow, and not the click of a button in a form.
Hmm.... clear as mud yet? :) the question still is pretty straight foreward.
Cheers!
//// Loop
Create CViewerTemp
read DB and manipulate data
Loop B
create, and write to VeryTempHead page
code which creates the top of CViewer, HEAD items
create, and write to VeryTempBody page
code which will work there, items one by one matching head items
end Loop B
Write code which is 1ne time only stuff, to begin CViewer.
then transfer the stuff from VeryTempHead page, into CViewer, kill
VeryTempHead
then transfer the stuff from VeryTempBody to CViewer, kill Very Temp Body.
Open CTempViwer, click on a Button, a value gets posted to Authouring Page.
Authouring Page doesn't recieve anything, no code to do so exists [YET! :)]
If you want to create data on the fly, but not from $_POST, you can just populate $_POST from any other source like this:
<?php
// some calculations
$_POST['my_var'] = $some_calculated_stuff;
// later in your code
if(isset($_POST['my_var'])) {
// works as if it had been posted
}
?>
Is this what you're looking to do?
It's a bit hard to follow, so I'll suggest another potential:
Have you taken a look at cURL?
I think you mean hidden input fields. You can print them with php and they will be posted to your next php script.
Please note, that the user can change the values and you shouldn't trust them.
Also, you can consider using $_SESSION. That would be the better way to solve the task.
Please note that you shouldn't use PHP_SELF because it's insecure.

Re-ask: How do I use php so that when a user presses a specific button, they can write to a specific .txt file? [duplicate]

This question already has answers here:
Closed 11 years ago.
Possible Duplicate:
How do I use php so that when a user presses a specific button, they can write to a specific .txt file?
First of all, am I able to re-ask a question? I got two answers, yes, but I tried both and they didn't work. I'm worried that people will forget about my question and I might not ever get the answer to my question and patch up my code. Anyway, here it is. If, however, re-asking questions is against some rule I was not aware of, I will refrain from this in the future. Thanks!
Basically, as in other questions I've asked related to my php chat application, I am trying to get it so that there is a text field where $msg is displayed via msg.txt. Two users can communicate to another in this way. This would be easy if I wanted to use a simple include function. But I don't want to take all the trouble to make and upload all those pages to my server. So how can I have it where when the user, say named Aaron, clicks on a button titled Benjamin, and types to a file called aaronbenjamin.txt, and if Aaron wants to talk to another user, he can press on a button titled Chris, and type to a file called aaronchris.txt? And all from the same box and text field? Thanks, I appreciate it.
The most important thing about my question was that the user is able to press the name of another user and they will be able to chat with the user. This means that they will be switching to a different .txt file whenever they click on a user. Thank you to #Cyclone and #cronoklee for your answers, but sadly they didn't work.
Use this for the buttons:
<textarea name=msg></textarea>
<input type=submit name=file value=Aaron>
<input type=submit name=file value=Benjamin>
And this for the PHP:
$fn = preg_replace('/\W+/', '', $_REQUEST["file"]);
file_put_contents("$fn.txt", $_REQUEST["msg"]);

How is the Pop up alert which is displayed on stackoverflow when you are answering a question and someone submits another answer created?

Ok, so you know when you're answering a question and are in the middle of typing it, and someone else posts an answer to your question and you get a little popup that says there is a new answer to the question? My question is how do you do that? I think I have the basic concept down... A question is answered, added to the database. The page your on keeps checking the database for new answers, and if there is something new displays a popup. (I'm not sure if that's how it's done, but just and idea) Anyway, I'm trying to create an application with similar functionality to that popup using php, and jQuery / Ajax / something else? I have a page that will be on the screen and will display information from the database. What I need: to figure out how to get that popup to display only when there is new content added to the database.
Thanks for the help!
I should also add... if anyone has any tutorials, or code snippets to share about the ajax / jquery integration with sql that'd be great. I'm pretty decent at PHP but totally new to ajax and jquery :-/
when you first load the question page, also read the number of answers.
Now poll the server with ajax requests every few seconds/minutes that return the number of answers..
If the number of answers is greater than the number when you first loaded the question then show the message that additional answers have been posted..
This is a tutorial on how to do the actual notifications.
As for the querying, it's a fairly simple AJAX call to the database on a timed interval to check for new results. If count > 0, then fire the notification process.
The refresh method on this tutorial could easily be re purposed for such notifications.
Create a timer in Javascript and call an AJAX endpoint that provides you with messages to diplay or that delivers HTML to place in a placeholder at the top of each page. If you get data returned then insert that into the page DOM using jQuery.
You got the basic concept - I don't know for certain, but this does seem to be the obvious way to do this.
As for the specific question - the result from the ajax call should indicate if an answer has been added or not. Only display the popup when the results indicates that a result has been added.
You can use jQuery to do almost all of this.
Create a script that will return the needed JSON data, probably just the id of the current article and then the count of answers. In your page, create a timer that performs an ajax call to the script every ~30 seconds. In your success callback function, compare the number of answers that were returned against the number of answers currently on the page. If the answer is greater, then perform a notification using the show() or fadeIn() functions. (You can either have the message HTML loaded when the page loads or you can append/prepend all of the html for the message with the ajax call.
This is a vague answer, but the question is actually very broad so you could do this in a million different ways. If you need some help with it, you can PM me.

Categories