problem with implode() function

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • tokcy
    New Member
    • Sep 2008
    • 45

    problem with implode() function

    Hi,
    I am having a problem with implode() function in PHP.
    whenever i am running my code it display an error like.

    Warning: implode() [function.implod e]: Bad arguments. in D:\xampp\htdocs \admin\addspecs .php on line 8

    and my code is

    Code:
    $as = $_POST;
    $i=1;
    while($i<=16)
    {
    $ad = $as['txtRow'.$i];
    $aw = implode(",",$ad);
    $qw = "#".$aw;
    echo $qw;
    $i++;
    }
    what is the error in this code can anybody tell me.
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    the problem is that $ad is not an array (it's the value of a POST element). further you should call implode() outside the while-loop.

    you can create arrays like
    Code:
    // repeat
    $array[] = $value;
    // or
    $array[$key] = $value;
    or use some of the array functions

    Comment

    • tokcy
      New Member
      • Sep 2008
      • 45

      #3
      hi,

      still i am not getting the solution. and how we can put the implode to outside of the while loop because all the text field name are unique and i can take the value of text fields without while loop.

      Comment

      • Dormilich
        Recognized Expert Expert
        • Aug 2008
        • 8694

        #4
        what I wanted to say is, you need to change the code structure

        Code:
        while (…)
        {
            // …
            $array[] = $_POST[$key];
        }
        $string = implode(",", $array);

        Comment

        • tokcy
          New Member
          • Sep 2008
          • 45

          #5
          Now its not giving an error, but when i am trying to print the value it showing only array like this:
          Array,Array,Arr ay,Array,Array, Array,Array,Arr ay,Array,Array, Array,Array,Arr ay,Array,Array, Array,Array,Arr ay

          Code:
          $i=0;
          while($i<=$count)
          {
          $arr[] = $_POST['txtRow'.$i];
          $i++;
          }
          $str = implode(",",$arr);
          echo $str;

          Comment

          • Dormilich
            Recognized Expert Expert
            • Aug 2008
            • 8694

            #6
            looks like $_POST['txtRow'.$i] is an array. if you set error reporting to E_ALL you should get the according notices.

            Comment

            • hsriat
              Recognized Expert Top Contributor
              • Jan 2008
              • 1653

              #7
              I too think there's some problem with $_POST thingie itself. You should see what you are actually POST'ing to the page. Use
              Code:
              echo"<pre>"; print_r($_POST); echo "</pre>";
              in the starting of the page. Then you can see what's going wrong.

              Also, if you have to iterate through the POST'ed array anyhow, then you can just concat the POST variable instead of imploding it after converting it to another array.
              Code:
              while (condition) 
              {
                  $str .= $_POST[whatever];
                  //increment thing
              }
              echo $str;

              Comment

              Working...