What is this error???
Moderator: General Moderators
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
What is this error???
Hi there,
I have an array and when I want to output or assign a value I simply did this:
$qOne[1] = $qOne[1] + 1;
fine I understand that it's the second element in my array. But when I run the php file, it works but gives me this error:
Undefined offset: 1
Meaning my 1 in my array $qOne[1]. First off, what is an undefined offset, and what is the problem with puting 1 in my [] ???? Thanks.
I have an array and when I want to output or assign a value I simply did this:
$qOne[1] = $qOne[1] + 1;
fine I understand that it's the second element in my array. But when I run the php file, it works but gives me this error:
Undefined offset: 1
Meaning my 1 in my array $qOne[1]. First off, what is an undefined offset, and what is the problem with puting 1 in my [] ???? Thanks.
Well, this
Should actually be this:
Also, the undefined offset error you are getting basically says you are using [1] before you have assigned it a value. Basically, you have to have a variable or array element on the left side of a operator assigning it a type or value before you actually use it.
This is called declaring your variables.
Code: Select all
$qOneї1] = $qOneї1] + 1;Code: Select all
$qOneї1] = 1;This is called declaring your variables.
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
OK
Well first this IS correct:
$qOne[1] = $qOne[1] + 1;
I'm not making $qOne[1] = 1, i am adding 1 to it's value already, it increments by one many times. Second, why do I have to declare a variable that has the value 1? 1 is not a variable, it is a integer value, plain and simple. You didn't answer my question, can anyone else???
$qOne[1] = $qOne[1] + 1;
I'm not making $qOne[1] = 1, i am adding 1 to it's value already, it increments by one many times. Second, why do I have to declare a variable that has the value 1? 1 is not a variable, it is a integer value, plain and simple. You didn't answer my question, can anyone else???
Never said it wasn't. I was going by what you showed me.Well first this IS correct:Code: Select all
$qOneї1] = $qOneї1] + 1;
So then why not do it the easy way?I'm not making $qOne[1] = 1, i am adding 1 to it's value already, it increments by one many times.
Code: Select all
$qOneї1]++;Because that is what you should do in programming in general. Just like in C, C++, or Java, you should declare your variables before using them. In PHP you don't necessarily have to, but if you use a variable (which $qOne[1] is) in an expression BEFORE assigning it a value and declaring what it is, you might get an error.Second, why do I have to declare a variable that has the value 1? 1 is not a variable, it is a integer value, plain and simple.
If you want, before you use the variable, simply assign it a value of 0 or 1, depending on your needs.
As far as your statement that 1 is a value, not a variable, that is true. However, I never said you need to declare a value, I said you should declare your variable.
I did, you simply dismissed me as someone who doesn't know what they are talking about.You didn't answer my question, can anyone else???
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
well
I tried to create a var $one = 1; at the top and then use that instead of 1, but it gave the same response, thanks for trying.
No, I think you misunderstood me...put this at the top instead:
And then try your normal code.
Code: Select all
$qOneї1] = 0;-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
whew
finally it works, thanks..I did misunderstand you, sorry. I still get one more error when the page loads. I get:
Undefined variable: action
not its this line:
if ($action=="submitC" ){
submitClick($zeros,$qOneStr,$qTwoStr,$qThreeStr,$qOne,$qTwo,$qThree);
}
any ideas?? I tried declaring $action up top but then nothing happens in my page.
Undefined variable: action
not its this line:
if ($action=="submitC" ){
submitClick($zeros,$qOneStr,$qTwoStr,$qThreeStr,$qOne,$qTwo,$qThree);
}
any ideas?? I tried declaring $action up top but then nothing happens in my page.
That error means that $action does not exist yet, and has no value.
My question to you is this: What is $action doing? What is it supposed to contain?
Typically, I will use $action in my query string, when calling a php file. Something like this:
http://www.mydomain.com/myscript.php?action=add
or
http://www.mydomain.com/myscript.php?action=delete
etc etc.
somehow, action has no value.
good luck
My question to you is this: What is $action doing? What is it supposed to contain?
Typically, I will use $action in my query string, when calling a php file. Something like this:
http://www.mydomain.com/myscript.php?action=add
or
http://www.mydomain.com/myscript.php?action=delete
etc etc.
somehow, action has no value.
good luck
Hey I recognise that 
I think I was helping out with this code before the server move - and the action variable was the problem (but not on my machine)
action is working in a query string and returns a function, I think the problem may be in the code I initially gave - it was a quick example - but if you are still using the same setup try changing
<form method="post" action="?action=submitC">
remove action="?action=submitC
and add this into your form
<input type="hidden" name="action" value="submitC">
I dunno if this will sort you out, but it would be the next thing I try.
I think I was helping out with this code before the server move - and the action variable was the problem (but not on my machine)
action is working in a query string and returns a function, I think the problem may be in the code I initially gave - it was a quick example - but if you are still using the same setup try changing
<form method="post" action="?action=submitC">
remove action="?action=submitC
and add this into your form
<input type="hidden" name="action" value="submitC">
I dunno if this will sort you out, but it would be the next thing I try.
- sam
- Forum Contributor
- Posts: 217
- Joined: Thu Apr 18, 2002 11:11 pm
- Location: Northern California
- Contact:
Not aly that but also trying to access the data in an offset that has not been defined. Atleast php will give you a warning rather than crashing your program unexpectedly such as some programming languages I know.BigE wrote:Just out of plain curiosity, wouldn't "undefined offset" mean that your trying to manipulate a value in an array that doesn't exist? Or am I just being stupid? hmm...
Cheers Sam
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
nope,
That didn't work. here is my complete code:
<html>
<body>
<form method="post" action=submitC">
Please complete the following 3 questions
<p><p>
<BR>How many times have you visited this site including today?<BR>
<input type=radio name=choice value=1>1-5<BR>
<input type=radio name=choice value=2>6-15<BR>
<input type=radio name=choice value=3>16-30<BR>
<input type=radio name=choice value=4>31+<BR>
<BR>Did you find what you were looking for on the site?<BR>
<input type=radio name=choice2 value=1>Yes<BR>
<input type=radio name=choice2 value=2>No<BR>
<BR>Would you like the site to remain online?<BR>
<input type=radio name=choice3 value=1>Yes<BR>
<input type=radio name=choice3 value=2>No<BR><BR>
<div align="left"><input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
//VARIABLES
$visits = array ("1", "2-15", "16-30", "31+");
$find = array ("Yes", "No");
$keep = array("Yes","No");
$num = 0;
$num2 = 0;
//END VARIABLES
function submitClick($choice,$choice2,$choice3,$action){
if ($choice == 1){
echo "1-5, ";
$num = 1;
}
elseif ($choice == 2){
echo "6-15, ";
$num = 2;
}
elseif ($choice == 3){
echo "16-30, ";
$num = 3;
}
elseif ($choice == 4){
echo "30+, ";
$num = 4;
}
else{
$num = 0;
}
if ($choice2 == 1){
echo "yes, ";
$num2 = 1;
}
elseif ($choice2 == 2){
echo "no, ";
$num2 = 2;
}
else{
$num2 = 0;
}
if ($choice3 == 1){
echo "yes";
$num3 = 1;
}
elseif ($choice3 == 2){
echo "no";
$num3 = 2;
}
else{
$num3 = 0;
}
$fp = fopen ("file.txt", "a");
$wri = fwrite($fp,$num);
$wri = fwrite($fp,$num2);
$wri = fwrite($fp,$num3);
fclose($fp);
}
if ($action=="submitC" ){
submitClick($choice,$choice2,$choice3,$action);
}
?>
it just keeps saying:
Warning: Undefined variable: action in \php\firstphp.php on line 89
this is my major problem, if anyone solves it, i will be in their debt
<html>
<body>
<form method="post" action=submitC">
Please complete the following 3 questions
<p><p>
<BR>How many times have you visited this site including today?<BR>
<input type=radio name=choice value=1>1-5<BR>
<input type=radio name=choice value=2>6-15<BR>
<input type=radio name=choice value=3>16-30<BR>
<input type=radio name=choice value=4>31+<BR>
<BR>Did you find what you were looking for on the site?<BR>
<input type=radio name=choice2 value=1>Yes<BR>
<input type=radio name=choice2 value=2>No<BR>
<BR>Would you like the site to remain online?<BR>
<input type=radio name=choice3 value=1>Yes<BR>
<input type=radio name=choice3 value=2>No<BR><BR>
<div align="left"><input type="Submit" name="Submit" value="Submit">
</form>
</body>
</html>
<?php
//VARIABLES
$visits = array ("1", "2-15", "16-30", "31+");
$find = array ("Yes", "No");
$keep = array("Yes","No");
$num = 0;
$num2 = 0;
//END VARIABLES
function submitClick($choice,$choice2,$choice3,$action){
if ($choice == 1){
echo "1-5, ";
$num = 1;
}
elseif ($choice == 2){
echo "6-15, ";
$num = 2;
}
elseif ($choice == 3){
echo "16-30, ";
$num = 3;
}
elseif ($choice == 4){
echo "30+, ";
$num = 4;
}
else{
$num = 0;
}
if ($choice2 == 1){
echo "yes, ";
$num2 = 1;
}
elseif ($choice2 == 2){
echo "no, ";
$num2 = 2;
}
else{
$num2 = 0;
}
if ($choice3 == 1){
echo "yes";
$num3 = 1;
}
elseif ($choice3 == 2){
echo "no";
$num3 = 2;
}
else{
$num3 = 0;
}
$fp = fopen ("file.txt", "a");
$wri = fwrite($fp,$num);
$wri = fwrite($fp,$num2);
$wri = fwrite($fp,$num3);
fclose($fp);
}
if ($action=="submitC" ){
submitClick($choice,$choice2,$choice3,$action);
}
?>
it just keeps saying:
Warning: Undefined variable: action in \php\firstphp.php on line 89
this is my major problem, if anyone solves it, i will be in their debt
-
slipstream
- Forum Commoner
- Posts: 86
- Joined: Fri Apr 19, 2002 8:53 am
- Location: Canada
but
I tried to declare it before, but then nothing happens when I click my button, it only works if I don't declare it. Will the warning show up to the general user ?? How do I declare it and get it to work because like I said, it doesn't work when I declare $action..