By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
437,968 Members | 1,684 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 437,968 IT Pros & Developers. It's quick & easy.

arrays and for loop problems

P: 12
Hi there guys

Im having a bit of a problem storing the values of some strings called $item1 ...$item6 (they are numbers from a radio button in a questionnaire, each item represents an answer for each question)

I want to put them in an array so when I use a For Loop I can whiz through them and put them into a variable which can then be put into an insert statement in mysql. (The MYSQL is looped depending on how many questions there are)


[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

for ($answer = $answer[0]; $answer < $answer[5] ; $answer++) {

$answers = $answer;

}[/PHP]

I keep getting the following error

Notice: Uninitialized string offset: 5 in c:\Project\htdocs\dissertation\v18\questionnaire.p hp on line 200

Notice: Undefined variable: answers in c:\Project\htdocs\dissertation\v18\questionnaire.p hp on line 221
Value:
The Answer did not submit: Query was empty

.............................and so on 75 times (I have 75 questions)


Please help as im a newbie

Thanks

Nabz
Mar 2 '07 #1
Share this Question
Share on Google+
8 Replies


P: 30
I'm a relative newbie myself, but the most apparent flaw here is that you're trying to initalize a loop with the string elements you declared up above. To initialize a for loop, you need to 1) count the # of elements in the array (in PHP, you use the count() method, in java, the .length method), and 2) initialize a counter that keeps track of the number of loop iterations).

For example:

[PHP]$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter[/PHP]

...then you code for whatever you want the loop to do (i.e. print to the screen, save to a file, or in this case, send it to the database). I know next to nothing about databases, so you'll need someone else to help you with that part. I hope this makes sense! :o)
Mar 2 '07 #2

10K+
P: 13,264
Hi there guys

Im having a bit of a problem storing the values of some strings called $item1 ...$item6 (they are numbers from a radio button in a questionnaire, each item represents an answer for each question)

I want to put them in an array so when I use a For Loop I can whiz through them and put them into a variable which can then be put into an insert statement in mysql. (The MYSQL is looped depending on how many questions there are)


[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

for ($answer = $answer[0]; $answer < $answer[5] ; $answer++) {

$answers = $answer;

}[/PHP]

I keep getting the following error

Notice: Uninitialized string offset: 5 in c:\Project\htdocs\dissertation\v18\questionnaire.p hp on line 200

Notice: Undefined variable: answers in c:\Project\htdocs\dissertation\v18\questionnaire.p hp on line 221
Value:
The Answer did not submit: Query was empty

.............................and so on 75 times (I have 75 questions)


Please help as im a newbie

Thanks

Nabz
Do you want to combine all the strings into one string or do you want to put each string into the database?
Mar 2 '07 #3

P: 12
Do you want to combine all the strings into one string or do you want to put each string into the database?
Hi guys thank you both for replying I want to individually store each string value in the database as an individual answer. (answers range from 0 to 7)

The flollowing code is where how im printing my questionnaire on screen

[PHP]//Fetch and print the questionnaire.
$counter = 1;
while ($row = mysql_fetch_array($result,MYSQL_BOTH)) {
echo '<tr><td align="left">'.$counter. '</td><td align="left">'.$row['q'].'</td><td align="left" style="left">';
echo "<input type=\"hidden\" name=\"question[]\" value=\"".$row['qid']."\" />\n";
print("<input type=\"radio\" name=item".$row['qid'] ." value=\"1\" />1
<input type=\"radio\" name=item".$row['qid'] ." value=\"2\" />2
<input type=\"radio\" name=item".$row['qid'] ." value=\"3\" />3
<input type=\"radio\" name=item".$row['qid'] ." value=\"4\" />4
<input type=\"radio\" name=item".$row['qid'] ." value=\"5\" />5
<input type=\"radio\" name=item".$row['qid'] ." value=\"6\" />6
<input type=\"radio\" name=item".$row['qid'] ." value=\"7\" />7
<input type=\"radio\" name=item".$row['qid'] ." value=\"0\" />n/a");
'</td></tr>';
$counter++;
}[/PHP]


Finally the second bit of code is where I am trying to loop the MYSQL INSERT so it stores each answer individually


[PHP]if (isset($_POST['question']) && is_array($_POST['question']))
{
foreach($_POST['question'] as $question_no)
{
echo "Value: $answers<br>\n"; // a test to see what values are being stored //
$query1 ="INSERT INTO answers (answer_score, question_id, user_id) ".
"VALUES ('".addslashes($answers)."', '".addslashes($question_no)."',$num11')";

$result1 = @mysql_query ($query1); // Run the query.

if (mysql_query($result1))
{
echo("Answers Submitted.<br>");
}
else
{
echo("The Answer did not submit: " . mysql_error());
}
}
}
}
}[/PHP]


I am using the first piece of code to store all the answers and then put it in a variable and insert each answer one at a time into the MYSQL statement
Mar 2 '07 #4

P: 12
I'm a relative newbie myself, but the most apparent flaw here is that you're trying to initalize a loop with the string elements you declared up above. To initialize a for loop, you need to 1) count the # of elements in the array (in PHP, you use the count() method, in java, the .length method), and 2) initialize a counter that keeps track of the number of loop iterations).

For example:

[PHP]$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter[/PHP]

...then you code for whatever you want the loop to do (i.e. print to the screen, save to a file, or in this case, send it to the database). I know next to nothing about databases, so you'll need someone else to help you with that part. I hope this makes sense! :o)

Cheers for your help mate I gave it a go but unfortunately still cant get it to collect all the answers except the first string.

[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter
$answers = $answer;

}[/PHP]


The following error message is displayed from the above code

Value: Array
The Answer did not submit: Query was emptyValue: Array
The Answer did not submit: Query was emptyValue: Array
.
.
.
repeated 75 times
Mar 2 '07 #5

10K+
P: 13,264
Cheers for your help mate I gave it a go but unfortunately still cant get it to collect all the answers except the first string.

[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter
$answers = $answer;

}[/PHP]


The following error message is displayed from the above code

Value: Array
The Answer did not submit: Query was emptyValue: Array
The Answer did not submit: Query was emptyValue: Array
.
.
.
repeated 75 times
Use

[PHP] for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter

$answers = $answer[$i];
//Insert that into the DB



}
[/PHP]
Mar 3 '07 #6

P: 12
Use

[PHP] for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter

$answers = $answer[$i];
//Insert that into the DB



}
[/PHP]

Hi thanks again for replying Ive tried the code you suggested but its still not inserting all the answers I still get the following:

Value: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
.
.
.
repeated 75 times

I have noticed it doesnt insert any values at all in the database not even the first one. Im assuming there may be something wrong with my code of insertion or where it is storing all the string values.


// Storing the strings code

[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter
$answers = $answer[$i];

}[/PHP]


Insertion code

[PHP]if (isset($_POST['question']) && is_array($_POST['question']))
{
foreach($_POST['question'] as $question_no)
{

$query1 ="INSERT INTO answers (answer_score, question_id, user_id) ".
"VALUES ('".addslashes($answers)."', '".addslashes($question_no)."',$num11')";
echo "Value: $answers<br>\n";
$result1 = @mysql_query ($query1); // Run the query.

if (mysql_query($result1))
{
echo("Answers Submitted.<br>");
}
else
{
echo("The Answer did not submit: " . mysql_error());
}
}
}
}
}[/PHP]
Mar 3 '07 #7

10K+
P: 13,264
Hi thanks again for replying Ive tried the code you suggested but its still not inserting all the answers I still get the following:

Value: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
The Answer did not submit: Query was emptyValue: 7
.
.
.
repeated 75 times

I have noticed it doesnt insert any values at all in the database not even the first one. Im assuming there may be something wrong with my code of insertion or where it is storing all the string values.


// Storing the strings code

[PHP]$answer = array();
$answer[0] = $item1;
$answer[1] = $item2;
$answer[2] = $item3;
$answer[3] = $item4;
$answer[4] = $item5;
$answer[5] = $item6;

$numElements = count($answer); //this counts the elements
for ($i = 0; $i < $numElements; $i++)//the $i variable is your loop counter
$answers = $answer[$i];

}[/PHP]


Insertion code

[PHP]if (isset($_POST['question']) && is_array($_POST['question']))
{
foreach($_POST['question'] as $question_no)
{

$query1 ="INSERT INTO answers (answer_score, question_id, user_id) ".
"VALUES ('".addslashes($answers)."', '".addslashes($question_no)."',$num11')";
echo "Value: $answers<br>\n";
$result1 = @mysql_query ($query1); // Run the query.

if (mysql_query($result1))
{
echo("Answers Submitted.<br>");
}
else
{
echo("The Answer did not submit: " . mysql_error());
}
}
}
}
}[/PHP]
You did not retrieve the values from the form.
Use $_POST or $_GET (the method of your form )to get the values
Mar 3 '07 #8

P: 12
You did not retrieve the values from the form.
Use $_POST or $_GET (the method of your form )to get the values

Hi thanks for the reply Ive tried that previously and once again and although no error message comes up nothing gets inserted into the table.

[PHP]The following is the code ive used:

if (isset($_POST['answers']) && is_array($_POST['answers']))
{
foreach($_POST['answers'] as $user_answers){

echo "Value: $user_answers<br>\n";

if (isset($_POST['question']) && is_array($_POST['question']))
{
foreach($_POST['question'] as $question_no)
{

$query1 ="INSERT INTO answers (answer_score, question_id, user_id) ".
"VALUES ('".addslashes($user_answers)."', '".addslashes($question_no)."',$num11')";
echo "Value: $user_answers<br>\n";
$result1 = @mysql_query ($query1); // Run the query.

if (mysql_query($result1))
{
echo("Answers Submitted.<br>");
}
else
{
echo("The Answer did not submit: " . mysql_error());
}
}
}
}
}
}
}[/PHP]


Sorry to be such a nuisance but this has been bugging me for weeks

Thanks again

Nabz
Mar 3 '07 #9

Post your reply

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