Page 1 of 2

What is this error???

Posted: Fri Apr 19, 2002 8:53 am
by slipstream
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.

Posted: Fri Apr 19, 2002 10:03 am
by jason
Well, this

Code: Select all

$qOneї1] = $qOneї1] + 1;
Should actually be this:

Code: Select all

$qOneї1] = 1;
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.

OK

Posted: Fri Apr 19, 2002 10:07 am
by slipstream
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???

Posted: Fri Apr 19, 2002 10:21 am
by jason
Well first this IS correct:

Code: Select all

$qOneї1] = $qOneї1] + 1;
Never said it wasn't. I was going by what you showed me.
I'm not making $qOne[1] = 1, i am adding 1 to it's value already, it increments by one many times.
So then why not do it the easy way?

Code: Select all

$qOneї1]++;
That increments that value by 1.
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.
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.

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.
You didn't answer my question, can anyone else???
I did, you simply dismissed me as someone who doesn't know what they are talking about.

well

Posted: Fri Apr 19, 2002 10:38 am
by slipstream
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.

Posted: Fri Apr 19, 2002 10:52 am
by jason
No, I think you misunderstood me...put this at the top instead:

Code: Select all

$qOneї1] = 0;
And then try your normal code.

Posted: Fri Apr 19, 2002 10:57 am
by BigE
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...

whew

Posted: Fri Apr 19, 2002 11:02 am
by slipstream
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.

Posted: Fri Apr 19, 2002 4:14 pm
by Zmodem
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

Posted: Fri Apr 19, 2002 4:22 pm
by Craig
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.

Posted: Fri Apr 19, 2002 4:23 pm
by sam
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...
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.

Cheers Sam

nope,

Posted: Mon Apr 22, 2002 9:32 am
by slipstream
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 :)

Posted: Mon Apr 22, 2002 10:38 am
by enygma
That's just a warning because you didn't declare the variable before you used it.

but

Posted: Mon Apr 22, 2002 10:42 am
by slipstream
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..

Posted: Mon Apr 22, 2002 10:46 am
by enygma
well, sounds like you have bigger issues than that, but if you change the error reporting level for the script, then it wont show that Warning.