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

First element is 'Array'?

P: n/a
I have this code on my WAMP server running on my XP machine

if ( isset( $_POST[ 'ans' ] ) )
{
for($i=0; $i<count($_POST['ans']);$i++)
{

if ($ans != NULL )
$ans .= ", " . $_POST['ans'][$i] ; // Not the first
element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so comma
not needed
}
echo 'answer is '. $ans . '<br>';
}

The purpose of the for loop is to concatenate all the elements into a comma
seperated string.
It works fine so if the first element is 22/7 it prints 'answer is 22/7'
However when I upload the file to the actual Linux based server and run the
same I get 'answer is Array, 22/7'. Here the word 'Array' is given as the
first element. So what's wrong and how can I fix this.

Thanks.

Jul 13 '08 #1
Share this Question
Share on Google+
4 Replies


P: n/a
mab464 wrote:
I have this code on my WAMP server running on my XP machine

if ( isset( $_POST[ 'ans' ] ) )
You should ensure that the $_POST['ans'] is of the right type, in your case an
array, have you thought about what happens if someone manages to post
something else than your array?
{
for($i=0; $i<count($_POST['ans']);$i++)
Here you recalculate the size of the array EVERY turn in the loop, better to
store the size of the array to a variable and compare that to the size of $i,
it's a lot faster.

{

if ($ans != NULL )
you should test the $ans variable if it's set or not, as at this point it may
be undeclared.
$ans .= ", " . $_POST['ans'][$i] ; // Not the
first element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so
comma not needed
This can be done a lot faster and easier with an implode().
http://www.php.net/manual/en/function.implode.php
You can then drop the loop too.
}
echo 'answer is '. $ans . '<br>';
}

The purpose of the for loop is to concatenate all the elements into a
comma seperated string.
if(isset($_POST['ans'] && is_array($_POST['ans'])) {
$ans = implode(', ',$_POST['ans']);
echo 'answer is '. $ans .'<br>';
}
It works fine so if the first element is 22/7 it prints 'answer is 22/7'
However when I upload the file to the actual Linux based server and run
the same I get 'answer is Array, 22/7'. Here the word 'Array' is given
as the first element. So what's wrong and how can I fix this.
The first cell in $_POST['ans'] is an array, do a var_dump($_POST['ans']) on
the top of your page and you will see what you really did post to the page.
I would think there is something that ain't completely right in your
post-form, which with the version of PHP you are using at home don't handle on
the same way as on the version of PHP that the remote servers has.

You can run phpinfo() on both and you will see that there will most likely be
a major version difference.
--

//Aho
Jul 13 '08 #2

P: n/a
J.O. Aho wrote:
mab464 wrote:
>I have this code on my WAMP server running on my XP machine

if ( isset( $_POST[ 'ans' ] ) )

You should ensure that the $_POST['ans'] is of the right type, in your
case an array, have you thought about what happens if someone manages to
post something else than your array?
> {
for($i=0; $i<count($_POST['ans']);$i++)

Here you recalculate the size of the array EVERY turn in the loop,
better to store the size of the array to a variable and compare that to
the size of $i, it's a lot faster.

> {

if ($ans != NULL )

you should test the $ans variable if it's set or not, as at this point
it may be undeclared.
> $ans .= ", " . $_POST['ans'][$i] ; // Not the
first element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so
comma not needed

This can be done a lot faster and easier with an implode().
http://www.php.net/manual/en/function.implode.php
You can then drop the loop too.
> }
echo 'answer is '. $ans . '<br>';
}

The purpose of the for loop is to concatenate all the elements into a
comma seperated string.

if(isset($_POST['ans'] && is_array($_POST['ans'])) {
$ans = implode(', ',$_POST['ans']);
echo 'answer is '. $ans .'<br>';
}
>It works fine so if the first element is 22/7 it prints 'answer is 22/7'
However when I upload the file to the actual Linux based server and
run the same I get 'answer is Array, 22/7'. Here the word 'Array' is
given as the first element. So what's wrong and how can I fix this.

The first cell in $_POST['ans'] is an array, do a
var_dump($_POST['ans']) on the top of your page and you will see what
you really did post to the page.
I would think there is something that ain't completely right in your
post-form, which with the version of PHP you are using at home don't
handle on the same way as on the version of PHP that the remote servers
has.

You can run phpinfo() on both and you will see that there will most
likely be a major version difference.


Minor correction of a typo: You left out the closing parentheses if the
isset.
Jul 13 '08 #3

P: n/a
Thanks. This a website where students can take online Math tests. Questions
could be of multiple choice or fill in the blank.

$ans is a local variable that gets declared at assignment. $_POST[ 'ans' ]
is an array which could be an input box, textarea, radio button or checkbox.
I'm using an array in all cases because in case it is a checkbox then
multiple checkboxes could be true and I would need an array to hold their
values. This code gets called when creating the question.

if ($qtype==3) // fill in the blank type question
{
$q = explode("<blank>",$question);

$question = "<br>" . addslashes($q[0]) . " <input type=text name='ans[]'
value='$ans' class='fillblank' /" . addslashes($q[1]) ;

}
elseif ($qtype==2) // multiple choice question. radio or checkbox
{

if (strpos($question, "<r>")) // if <rexists then radio buttons
{
$dlm = "<r>";
$boxtype = "radio";
}
else
{
$dlm = "<c>"; // if <cexists then checkboxes
$boxtype = "checkbox";
}
$choices = explode($dlm,$question);

$question = array_shift($choices) . "<br>" ;

foreach($choices as $ch )
{
if (strcmp(trim($ans),trim($ch))==0)
$chk = "checked" ;
else
$chk = "";

// generate html for mulitple choice
$question .= "<input type=$boxtype name='ans[]' $chk value=$ch$ch
<br>";
}

}
else
$question = addslashes($question);

The code I posted earlier gets called when student submits answer (by either
checking the correct option or typing the correct value). I've replaced the
for loop with implode function and it works fine.
To solve the 'Array' problem I've just chopped of the 'Array,' string

$ans = trim(str_replace("Array,", "", $ans));

It must be the PHP version 4.x used by client compared to 5.x that I'm using
for development.

Thanks

"J.O. Aho" <us**@example.netwrote in message
news:6d************@mid.individual.net...
mab464 wrote:
>I have this code on my WAMP server running on my XP machine

if ( isset( $_POST[ 'ans' ] ) )

You should ensure that the $_POST['ans'] is of the right type, in your
case an array, have you thought about what happens if someone manages to
post something else than your array?
> {
for($i=0; $i<count($_POST['ans']);$i++)

Here you recalculate the size of the array EVERY turn in the loop, better
to store the size of the array to a variable and compare that to the size
of $i, it's a lot faster.

> {

if ($ans != NULL )

you should test the $ans variable if it's set or not, as at this point it
may be undeclared.
> $ans .= ", " . $_POST['ans'][$i] ; // Not the
first element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so
comma not needed

This can be done a lot faster and easier with an implode().
http://www.php.net/manual/en/function.implode.php
You can then drop the loop too.
> }
echo 'answer is '. $ans . '<br>';
}

The purpose of the for loop is to concatenate all the elements into a
comma seperated string.

if(isset($_POST['ans'] && is_array($_POST['ans'])) {
$ans = implode(', ',$_POST['ans']);
echo 'answer is '. $ans .'<br>';
}
>It works fine so if the first element is 22/7 it prints 'answer is 22/7'
However when I upload the file to the actual Linux based server and run
the same I get 'answer is Array, 22/7'. Here the word 'Array' is given
as the first element. So what's wrong and how can I fix this.

The first cell in $_POST['ans'] is an array, do a var_dump($_POST['ans'])
on the top of your page and you will see what you really did post to the
page.
I would think there is something that ain't completely right in your
post-form, which with the version of PHP you are using at home don't
handle on the same way as on the version of PHP that the remote servers
has.

You can run phpinfo() on both and you will see that there will most likely
be a major version difference.
--

//Aho
Jul 13 '08 #4

P: n/a
mab464 wrote:
>
"J.O. Aho" <us**@example.netwrote in message
news:6d************@mid.individual.net...
>mab464 wrote:
>>I have this code on my WAMP server running on my XP machine

if ( isset( $_POST[ 'ans' ] ) )

You should ensure that the $_POST['ans'] is of the right type, in your
case an array, have you thought about what happens if someone manages
to post something else than your array?
>> {
for($i=0; $i<count($_POST['ans']);$i++)

Here you recalculate the size of the array EVERY turn in the loop,
better to store the size of the array to a variable and compare that
to the size of $i, it's a lot faster.

>> {

if ($ans != NULL )

you should test the $ans variable if it's set or not, as at this point
it may be undeclared.
>> $ans .= ", " . $_POST['ans'][$i] ; // Not the
first element so append a comma
else
$ans = $_POST['ans'][$i] ; // first element so
comma not needed

This can be done a lot faster and easier with an implode().
http://www.php.net/manual/en/function.implode.php
You can then drop the loop too.
>> }
echo 'answer is '. $ans . '<br>';
}

The purpose of the for loop is to concatenate all the elements into a
comma seperated string.

if(isset($_POST['ans'] && is_array($_POST['ans'])) {
$ans = implode(', ',$_POST['ans']);
echo 'answer is '. $ans .'<br>';
}
>>It works fine so if the first element is 22/7 it prints 'answer is
22/7'
However when I upload the file to the actual Linux based server and
run the same I get 'answer is Array, 22/7'. Here the word 'Array' is
given as the first element. So what's wrong and how can I fix this.

The first cell in $_POST['ans'] is an array, do a
var_dump($_POST['ans']) on the top of your page and you will see what
you really did post to the page.
I would think there is something that ain't completely right in your
post-form, which with the version of PHP you are using at home don't
handle on the same way as on the version of PHP that the remote
servers has.

You can run phpinfo() on both and you will see that there will most
likely be a major version difference.
--

//Aho


Thanks. This a website where students can take online Math tests.
Questions could be of multiple choice or fill in the blank.

$ans is a local variable that gets declared at assignment.
$_POST[ 'ans' ] is an array which could be an input box, textarea,
radio button or checkbox. I'm using an array in all cases because in
case it is a checkbox then multiple checkboxes could be true and I
would need an array to hold their values. This code gets called when
creating the question.

if ($qtype==3) // fill in the blank type question
{
$q = explode("<blank>",$question);

$question = "<br>" . addslashes($q[0]) . " <input type=text
name='ans[]' value='$ans' class='fillblank' /" . addslashes($q[1]) ;

}
elseif ($qtype==2) // multiple choice question. radio or checkbox
{

if (strpos($question, "<r>")) // if <rexists then radio buttons
{
$dlm = "<r>";
$boxtype = "radio";
}
else
{
$dlm = "<c>"; // if <cexists then checkboxes
$boxtype = "checkbox";
}
$choices = explode($dlm,$question);

$question = array_shift($choices) . "<br>" ;

foreach($choices as $ch )
{
if (strcmp(trim($ans),trim($ch))==0)
$chk = "checked" ;
else
$chk = "";

// generate html for mulitple choice
$question .= "<input type=$boxtype name='ans[]' $chk value=$ch$ch
<br>";
}

}
else
$question = addslashes($question);

The code I posted earlier gets called when student submits answer (by
either checking the correct option or typing the correct value). I've
replaced the for loop with implode function and it works fine.
To solve the 'Array' problem I've just chopped of the 'Array,' string

$ans = trim(str_replace("Array,", "", $ans));

It must be the PHP version 4.x used by client compared to 5.x that I'm
using for development.

Thanks
(Top posting fixed)

No, $_POST['ans'] MAY be an array. It may also be a single value or it
may not be declared at all. The same is true with any value coming from
the user. Never assume anything when you're dealing with user input;
always check it.

Why are you calling addslashes() all over the place? That's just plain
wrong.

Also, you don't understand something - PHP outputs 'Array' when you try
to print a variable which is an array. There should be no difference
between php 4.x and php 5.x here.

You need to find your real problem. But your code is so convoluted I'm
not surprised you have such a problem.

Also, please don't top post.

--
==================
Remove the "x" from my email address
Jerry Stuckle
JDS Computer Training Corp.
js*******@attglobal.net
==================

Jul 13 '08 #5

This discussion thread is closed

Replies have been disabled for this discussion.