First element is 'Array'?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • mab464

    #1

    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.

  • J.O. Aho

    #2
    Re: First element is 'Array'?

    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().

    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

    Comment

    • sheldonlg

      #3
      Re: First element is 'Array'?

      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().

      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.

      Comment

      • mab464

        #4
        Re: First element is 'Array'?

        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='fillblan k' /" . addslashes($q[1]) ;

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

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

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

        foreach($choice s as $ch )
        {


        if (strcmp(trim($a ns),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($que stion);

        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_replac e("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" <user@example.n etwrote in message
        news:6dtulgF4au 84U1@mid.indivi dual.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().

        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

        Comment

        • Jerry Stuckle

          #5
          Re: First element is 'Array'?

          mab464 wrote:
          >
          "J.O. Aho" <user@example.n etwrote in message
          news:6dtulgF4au 84U1@mid.indivi dual.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($_POS T['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($_POS T['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='fillblan k' /" . addslashes($q[1]) ;
          >
          }
          elseif ($qtype==2) // multiple choice question. radio or checkbox
          {
          >
          if (strpos($questi on, "<r>")) // if <rexists then radio buttons
          {
          $dlm = "<r>";
          $boxtype = "radio";
          }
          else
          {
          $dlm = "<c>"; // if <cexists then checkboxes
          $boxtype = "checkbox";
          }
          $choices = explode($dlm,$q uestion);
          >
          $question = array_shift($ch oices) . "<br>" ;
          >
          foreach($choice s as $ch )
          {
          >
          >
          if (strcmp(trim($a ns),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($que stion);
          >
          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_replac e("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.
          jstucklex@attgl obal.net
          =============== ===

          Comment

          Working...