473,394 Members | 1,802 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

What am I doing wrong in this application?

20
It is just one page called me.php
[PHP]<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Find the number ...</title>
</head>
<body>
<h3>Find the numer generated by your PC between 1 and 100 </h3>
<?php

// I make a counter to save the number of attempts
if (! isset($teller)){
$teller = 1;
}

// The aim is that the computer takes a number in mind
// And that the user can find. I give hints of higher and
// Lower ...
srand((double) microtime() * 100000);

// Does the recommended number exit?
if (! isset($recommended_number)){

// Apparently there is not yet ... so I create a new one
$recommended_number = rand(1,100);

} else {
if ( isset($answer) && $answer != '' ){
// See if the answer is higher or lower (or correct)

if ($answer > $recommended_number){
$message = "Pleas --";
$teller++;

} elseif ($answer < $recommended_number){
$message = "Pleas ++";
$teller++;

} else {
$message = $teller > 1 ? "Your number is correct! And you have found this in " . $teller . " attempts!"
: "Your number is correct! And you have found this in 1 attempt!!";

$recommended_number = rand(1,100);
$teller = 1;

}
}
}

echo ("<font color=\"#FF0000\">" . $message . "</font> <br />");
?>

<form method="GET" action="me.php">
Take a gamble:
<input name="answer" value="<?php echo $answer?>"><br />
<input type="hidden" name="recommended_number" value="<?php echo $recommended_number?>">
<input type="submit" value="Find it ...">
</form>
</body>
</html>[/PHP]

I always get the same message:
Your number is correct! And you have found this in 1 attempt!! , each time I click on the submit, the variable teller doesn't change its value !!

Why ?

Thanks
Jan 31 '08 #1
4 1295
stepterr
157 100+
bchaib, try using a session variable to keep track of $teller. Since the page is being reloaded each time the user hits submit, $teller is never set. That means when you are going through your logic it is always getting set to 1. So in every place you have $teller, change that to $_SESSION['teller']. And at the very beginning of your page, before anything else add:

[PHP]<?php
session_start();
?>[/PHP]

You might also want to think about using a session variable for your recommended_number. Right now, if anyone was paying attention, they would just have to look at the URL to figure out what the number is.
Jan 31 '08 #2
bchaib
20
bchaib, try using a session variable to keep track of $teller. Since the page is being reloaded each time the user hits submit, $teller is never set. That means when you are going through your logic it is always getting set to 1. So in every place you have $teller, change that to $_SESSION['teller']. And at the very beginning of your page, before anything else add:

[PHP]<?php
session_start();
?>[/PHP]

You might also want to think about using a session variable for your recommended_number. Right now, if anyone was paying attention, they would just have to look at the URL to figure out what the number is.
Yes, I know that I should use POST in stead of GET but this is just a test and simple to test in order to answer my question ..
Ok, I also thought about session but why does this work for recomend_number and answer but not for teller ??

I only know the theory and unserstand how to use the session for passing a variable between different pages, I truelly don't know where or how I have to use the session in this example ...

Thanks
kind regards
Feb 1 '08 #3
stepterr
157 100+
[PHP]<?php
session_start();
?>
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML//EN">
<html>
<head>
<title>Find the number ...</title>
</head>
<body>
<h3>Find the numer generated by your PC between 1 and 100 </h3>
<?php

// I make a counter to save the number of attempts
if (! isset($_SESSION['teller']))
{

$_SESSION['teller'] = 1;
}
// The aim is that the computer takes a number in mind
// And that the user can find. I give hints of higher and
// Lower ...

srand((double) microtime() * 100000);
// Does the recommended number exit?
if (! isset($recommended_number))
{
// Apparently there is not yet ... so I create a new one
$recommended_number = rand(1,100);
}
else
{
if ( isset($answer) && $answer != '' )
{
// See if the answer is higher or lower (or correct)
if ($answer > $recommended_number)
{
$message = "Pleas --";
$_SESSION['teller']++;
}
elseif ($answer < $recommended_number)
{
$message = "Pleas ++";
$_SESSION['teller']++;
}
else
{
$message = $teller > 1 ? "Your number is correct! And you have found this in " . $_SESSION['teller'] . " attempts!": "Your number is correct! And you have found this in 1 attempt!!";
$recommended_number = rand(1,100);
//$teller = 1;
$_SESSION['teller']=1;
}
}
}
echo ("<font color=\"#FF0000\">" . $message . "</font> <br />");
?>



<form method="GET" action="me.php">

Take a gamble:

<input name="answer" value="<?php echo $answer?>"><br />

<input type="hidden" name="recommended_number" value="<?php echo $recommended_number?>">

<input type="submit" value="Find it ...">

</form>

</body>

</html>[/PHP]

Try this. And the difference has to do with the fields on your form.
Feb 1 '08 #4
bchaib
20
Hi,
Thank you so much, it works perfectly but there is a little diference between my code and yours:
On line 47 you write:
Expand|Select|Wrap|Line Numbers
  1. $message = $teller > 1 ?  "Your number is correct! And you have found this in " . $_SESSION['teller'] . " attempts!":  "Your number is correct! And you have found this in 1 attempt!!"; 
But I just use the variable $teller:
Expand|Select|Wrap|Line Numbers
  1. $message = $teller > 1 ?  "Your number is correct! And you have found this in " . $teller . " attempts!":  "Your number is correct! And you have found this in 1 attempt!!"; 
===> Does $_SESSION['teller'] = $teller then ?

I have another question about this application, so as you see I'm using the names of input values in my form as variable on this way:
Expand|Select|Wrap|Line Numbers
  1. <input name="answer" value="<?php echo $answer?>"><br />
So not:
Expand|Select|Wrap|Line Numbers
  1. $answer = $_GET['answer'];
===> Is there any difference between those two styles?

I asked befor about the using of $recommended_number and $answer and you said it has to do with the form because when I follow the same method to save the variable $teller it doesn't work as you saw befor in the application.

Unfortuntly I still don't understand why !


Thank u very much
kind regards
chaib
Feb 1 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

137
by: Philippe C. Martin | last post by:
I apologize in advance for launching this post but I might get enlightment somehow (PS: I am _very_ agnostic ;-). - 1) I do not consider my intelligence/education above average - 2) I am very...
125
by: Sarah Tanembaum | last post by:
Beside its an opensource and supported by community, what's the fundamental differences between PostgreSQL and those high-price commercial database (and some are bloated such as Oracle) from...
121
by: typingcat | last post by:
First of all, I'm an Asian and I need to input Japanese, Korean and so on. I've tried many PHP IDEs today, but almost non of them supported Unicode (UTF-8) file. I've found that the only Unicode...
46
by: Keith K | last post by:
Having developed with VB since 1992, I am now VERY interested in C#. I've written several applications with C# and I do enjoy the language. What C# Needs: There are a few things that I do...
13
by: Jason Huang | last post by:
Hi, Would someone explain the following coding more detail for me? What's the ( ) for? CurrentText = (TextBox)e.Item.Cells.Controls; Thanks. Jason
8
by: Midnight Java Junkie | last post by:
Dear Colleagues: I feel that the dumbest questions are those that are never asked. I have been given the opportunity to get into .NET. Our organization has a subscription with Microsoft that...
2
by: Phil Certain | last post by:
Hi, Relative newbie to .Net but experienced with classic ASP. I am trying to create a simple business object to contain commonly used functions. This is what I have done: 1 - Created a simple...
8
by: watkinsdev | last post by:
Hi, I have created a mesh class in visual studio 6.0 c++. I can create a device, render objects and can edit the objects by for instancnce selecting a cluster of vertices and processing the...
6
by: BJ | last post by:
I just started this week on a new project. The existing project uses BEA Tuxedo as a second layer service broker. The clients make calls to the Tux services which in turn retrieves data from an...
4
by: Jason | last post by:
Here is what I am trying: My.Computer.FileSystem.CopyFile("\\server\all\sets\!!!!!PREFS \Application Data", _ "C:\Documents and Settings\" & Environment.UserName & "\Application Data\Adobe",...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.