473,408 Members | 1,938 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,408 software developers and data experts.

First element is 'Array'?

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
4 4542
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
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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Randell D. | last post by:
Folks, I know I could do this with a foreach loop but it looks dirty. I'm wondering if I can do this via array_walk() or asort() and would appreciate some help.. I have an array - an example...
17
by: Michael Hopkins | last post by:
Hi all I want to create a std::vector that goes from 1 to n instead of 0 to n-1. The only change this will have is in loops and when the vector returns positions of elements etc. I am calling...
6
by: Fungii | last post by:
Hello, I have a stylesheet that sets p:first-letter to a certain size and colour. I was playing around with Javascript to change paragraph stylesheets using an array like this: var paras =...
8
by: ehames | last post by:
Hi guys, As far as I know, for any type T, and positive integer N, if I have an array declared as: T array; then, &array and array are the same element. Is there any reason why a
4
by: =?Utf-8?B?cm9nZXJfMjc=?= | last post by:
hey, I have a method that takes a char array of 10. I have a char array of 30. how do I make it send the first 10, then the next 10, then the final 10 ? I need help with my looping skills....
3
by: SM | last post by:
Hello, I have an array that holds images path of cd covers. The array looks like this: $cd = array( 589=>'sylver.jpg', 782=>'bigone.jpg', 158=>'dime.jpg' );
7
by: Szabolcs Borsanyi | last post by:
I know that this topic has been discussed a lot, still I'd appreciate a clear cut (and correct) answer: I pass a multidimensional array to a function, which is defined as int f(int a) { int...
4
by: nembo kid | last post by:
I have the following bidimensional array int a ; Why the first address of this array is only: & (mat) and not also:
6
by: CSharper | last post by:
I am trying to use the following; I have an array with bunch of values in it. I am trying to find a value that contains part of the string I am passing eg string array = {"help","Csharp rocks"} ...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

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.